blob: 9fdc10406f8bf140f6e98ffb91c918891b2e5a5d [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 Welte11fa29c2009-02-19 17:24:39 +000045#include <openbsc/trau_mux.h>
Harald Welte52b1f982008-12-23 20:25:15 +000046
Harald Welte8470bf22008-12-25 23:28:35 +000047#define GSM48_ALLOC_SIZE 1024
48#define GSM48_ALLOC_HEADROOM 128
Harald Welte52b1f982008-12-23 20:25:15 +000049
Harald Welte09e38af2009-02-16 22:52:23 +000050static const struct tlv_definition rsl_att_tlvdef = {
51 .def = {
52 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
53 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
54 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
55 [GSM48_IE_UTC] = { TLV_TYPE_TV },
56 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
57 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
58
59 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
60 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
61 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
62 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
63 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
64 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
65 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
66 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
67 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
68 [GSM48_IE_CONN_NUM] = { TLV_TYPE_TLV },
69 [GSM48_IE_CONN_SUBADDR] = { TLV_TYPE_TLV },
70 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
71 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
72 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
73 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
74 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
75 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
76 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
77 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
78 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
79 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
80 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
81 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
82 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
83 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
84 /* FIXME: more elements */
85 },
86};
87
Harald Welte49f48b82009-02-17 15:29:33 +000088static inline int is_ipaccess_bts(struct gsm_bts *bts)
89{
90 switch (bts->type) {
91 case GSM_BTS_TYPE_NANOBTS_900:
92 case GSM_BTS_TYPE_NANOBTS_1800:
93 return 1;
94 default:
95 break;
96 }
97 return 0;
98}
Harald Welte09e38af2009-02-16 22:52:23 +000099
Harald Welte65e74cc2008-12-29 01:55:35 +0000100static int gsm48_tx_simple(struct gsm_lchan *lchan,
101 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000102static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000103
Harald Welte52b1f982008-12-23 20:25:15 +0000104struct gsm_lai {
105 u_int16_t mcc;
106 u_int16_t mnc;
107 u_int16_t lac;
108};
109
Holger Freyther89824fc2008-12-30 16:18:18 +0000110static int authorize_everonye = 0;
111void gsm0408_allow_everyone(int everyone)
112{
113 printf("Allowing everyone?\n");
114 authorize_everonye = everyone;
115}
116
Holger Freythere97f7fb2008-12-31 18:52:11 +0000117static int reject_cause = 0;
118void gsm0408_set_reject_cause(int cause)
119{
120 reject_cause = cause;
121}
122
Holger Freyther73487a22008-12-31 18:53:57 +0000123static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
124 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000125{
126 if (!subscriber)
127 return 0;
128
Holger Freyther73487a22008-12-31 18:53:57 +0000129 /*
130 * Do not send accept yet as more information should arrive. Some
131 * phones will not send us the information and we will have to check
132 * what we want to do with that.
133 */
134 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
135 return 0;
136
Holger Freyther89824fc2008-12-30 16:18:18 +0000137 if (authorize_everonye)
138 return 1;
139
140 return subscriber->authorized;
141}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000142
Holger Freyther73487a22008-12-31 18:53:57 +0000143static void release_loc_updating_req(struct gsm_lchan *lchan)
144{
Harald Welte179f0642008-12-31 23:59:18 +0000145 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000146 return;
147
148 del_timer(&lchan->loc_operation->updating_timer);
149 free(lchan->loc_operation);
150 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000151 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000152}
153
154static void allocate_loc_updating_req(struct gsm_lchan *lchan)
155{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000156 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000157 release_loc_updating_req(lchan);
158
159 lchan->loc_operation = (struct gsm_loc_updating_operation *)
160 malloc(sizeof(*lchan->loc_operation));
161 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
162}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000163
Harald Welte52b1f982008-12-23 20:25:15 +0000164static void to_bcd(u_int8_t *bcd, u_int16_t val)
165{
Harald Welte4b634542008-12-27 01:55:51 +0000166 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000167 val = val / 10;
168 bcd[1] = val % 10;
169 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000170 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000171 val = val / 10;
172}
173
Holger Freyther17746612008-12-28 16:32:44 +0000174void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000175 u_int16_t mnc, u_int16_t lac)
176{
177 u_int8_t bcd[3];
178
179 to_bcd(bcd, mcc);
180 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
181 lai48->digits[1] = bcd[2];
182
183 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000184 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
185#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000186 lai48->digits[1] |= bcd[2] << 4;
187 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000188#else
189 lai48->digits[1] |= 0xf << 4;
190 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
191#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000192
Harald Welte4b634542008-12-27 01:55:51 +0000193 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000194}
195
Harald Welte255539c2008-12-28 02:26:27 +0000196#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000197#define MID_TMSI_LEN (TMSI_LEN + 2)
198
Harald Welte255539c2008-12-28 02:26:27 +0000199int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000200{
Harald Welte65e74cc2008-12-29 01:55:35 +0000201 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000202
Harald Welte4b634542008-12-27 01:55:51 +0000203 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000204 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000205 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000206 *tptr = htonl(tmsi);
207
208 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000209}
210
Harald Welte09e38af2009-02-16 22:52:23 +0000211static const char bcd_num_digits[] = {
212 '0', '1', '2', '3', '4', '5', '6', '7',
213 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
214};
215
216/* decode a 'called party BCD number' as in 10.5.4.7 */
217u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
218{
219 u_int8_t in_len = bcd_lv[0];
220 int i;
221
222 if (in_len < 1)
223 return 0;
224
225 for (i = 2; i <= in_len; i++) {
226 /* lower nibble */
227 output_len--;
228 if (output_len <= 1)
229 break;
230 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
231
232 /* higher nibble */
233 output_len--;
234 if (output_len <= 1)
235 break;
236 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
237 }
238 if (output_len >= 1)
239 *output++ = '\0';
240
241 /* return number type / calling plan */
242 return bcd_lv[1] & 0x3f;
243}
244
245/* convert a single ASCII character to call-control BCD */
246static int asc_to_bcd(const char asc)
247{
248 int i;
249
250 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
251 if (bcd_num_digits[i] == asc)
252 return i;
253 }
254 return -EINVAL;
255}
256
257/* convert a ASCII phone number to 'called party BCD number' */
258int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
259 u_int8_t type, const char *input)
260{
261 int in_len = strlen(input);
262 int i;
263 u_int8_t *bcd_cur = bcd_lv + 2;
264
265 if (in_len/2 + 1 > max_len)
266 return -EIO;
267
268 /* two digits per byte, plus type byte */
269 bcd_lv[0] = in_len/2 + 1;
270 if (in_len % 2)
271 bcd_lv[0]++;
272
273 /* if the caller wants to create a valid 'calling party BCD
274 * number', then the extension bit MUST NOT be set. For the
275 * 'called party BCD number' it MUST be set. *sigh */
276 bcd_lv[1] = type;
277
278 for (i = 0; i < in_len; i++) {
279 int rc = asc_to_bcd(input[i]);
280 if (rc < 0)
281 return rc;
282 if (i % 2 == 0)
283 *bcd_cur = rc;
284 else
285 *bcd_cur++ |= (rc << 4);
286 }
287 /* append padding nibble in case of odd length */
288 if (i % 2)
289 *bcd_cur++ |= 0xf0;
290
291 /* return how many bytes we used */
292 return (bcd_cur - bcd_lv);
293}
294
Holger Freyther819dd202009-01-04 03:52:50 +0000295struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000296{
297 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
298}
299
Holger Freyther3e2c3232009-01-04 03:55:31 +0000300int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000301{
Harald Welte65e74cc2008-12-29 01:55:35 +0000302 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
303
304 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000305 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000306
Harald Welte65e74cc2008-12-29 01:55:35 +0000307 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
308 /* Send a 04.08 call control message, add transaction
309 * ID and TI flag */
310 gh->proto_discr |= msg->lchan->call.transaction_id;
311
312 /* GSM 04.07 Section 11.2.3.1.3 */
313 switch (msg->lchan->call.type) {
314 case GSM_CT_MO:
315 gh->proto_discr |= 0x80;
316 break;
317 case GSM_CT_MT:
318 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000319 case GSM_CT_NONE:
320 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000321 }
322 }
323 }
324
Harald Welte4b634542008-12-27 01:55:51 +0000325 msg->l3h = msg->data;
326
Harald Welte8470bf22008-12-25 23:28:35 +0000327 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000328}
329
Harald Welte52b1f982008-12-23 20:25:15 +0000330
Holger Freyther429e7762008-12-30 13:28:30 +0000331/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000332int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000333{
Harald Welte8470bf22008-12-25 23:28:35 +0000334 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000335 struct gsm48_hdr *gh;
336
Harald Welte8470bf22008-12-25 23:28:35 +0000337 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000338
339 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
340 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000341 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000342 gh->data[0] = cause;
343
Harald Weltedb253af2008-12-30 17:56:55 +0000344 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
345
Harald Welte65e74cc2008-12-29 01:55:35 +0000346 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000347}
348
349/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000350int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000351{
Harald Welte8470bf22008-12-25 23:28:35 +0000352 struct gsm_bts *bts = lchan->ts->trx->bts;
353 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000354 struct gsm48_hdr *gh;
355 struct gsm48_loc_area_id *lai;
356 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000357 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000358
Harald Welte8470bf22008-12-25 23:28:35 +0000359 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000360
361 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
362 gh->proto_discr = GSM48_PDISC_MM;
363 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
364
365 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000366 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000367 bts->network->network_code, bts->location_area_code);
368
369 mid = msgb_put(msg, MID_TMSI_LEN);
370 generate_mid_from_tmsi(mid, tmsi);
371
372 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
373
Harald Weltedb253af2008-12-30 17:56:55 +0000374 ret = gsm48_sendmsg(msg);
375
Harald Weltedb253af2008-12-30 17:56:55 +0000376 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000377
Holger Freyther07cc8d82008-12-29 06:23:46 +0000378 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000379}
380
Harald Weltefc977a82008-12-27 10:19:37 +0000381static char bcd2char(u_int8_t bcd)
382{
383 if (bcd < 0xa)
384 return '0' + bcd;
385 else
386 return 'A' + (bcd - 0xa);
387}
388
Harald Weltebf5e8df2009-02-03 12:59:45 +0000389/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000390static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
391{
392 int i;
393 u_int8_t mi_type;
394 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000395 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000396
397 mi_type = mi[0] & GSM_MI_TYPE_MASK;
398
399 switch (mi_type) {
400 case GSM_MI_TYPE_NONE:
401 break;
402 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000403 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
404 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
405 memcpy(&tmsi, &mi[1], 4);
406 tmsi = ntohl(tmsi);
407 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000408 }
409 break;
410 case GSM_MI_TYPE_IMSI:
411 case GSM_MI_TYPE_IMEI:
412 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000413 *str_cur++ = bcd2char(mi[0] >> 4);
414
415 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000416 if (str_cur + 2 >= string + str_len)
417 return str_cur - string;
418 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000419 /* skip last nibble in last input byte when GSM_EVEN */
420 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
421 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000422 }
423 break;
424 default:
425 break;
426 }
Harald Weltefc977a82008-12-27 10:19:37 +0000427 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000428
Harald Weltefc977a82008-12-27 10:19:37 +0000429 return str_cur - string;
430}
431
Harald Weltebf5e8df2009-02-03 12:59:45 +0000432/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000433static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
434{
435 struct msgb *msg = gsm48_msgb_alloc();
436 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000437
Harald Welte231ad4f2008-12-27 11:15:38 +0000438 msg->lchan = lchan;
439
440 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
441 gh->proto_discr = GSM48_PDISC_MM;
442 gh->msg_type = GSM48_MT_MM_ID_REQ;
443 gh->data[0] = id_type;
444
Harald Welte65e74cc2008-12-29 01:55:35 +0000445 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000446}
447
448#define MI_SIZE 32
449
Harald Weltebf5e8df2009-02-03 12:59:45 +0000450/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000451static int mm_rx_id_resp(struct msgb *msg)
452{
453 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000454 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000455 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
456 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000457 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000458
459 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000460 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000461 mi_type, mi_string);
462
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000463 /*
464 * Rogue messages could trick us but so is life
465 */
466 put_lchan(lchan);
467
Harald Welte75a983f2008-12-27 21:34:06 +0000468 switch (mi_type) {
469 case GSM_MI_TYPE_IMSI:
470 if (!lchan->subscr)
471 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000472 if (lchan->loc_operation)
473 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000474 break;
475 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000476 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000477 /* update subscribe <-> IMEI mapping */
478 if (lchan->subscr)
479 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000480 if (lchan->loc_operation)
481 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000482 break;
483 }
Holger Freyther73487a22008-12-31 18:53:57 +0000484
485 /* Check if we can let the mobile station enter */
486 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
487 db_subscriber_alloc_tmsi(lchan->subscr);
488 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
489 release_loc_updating_req(lchan);
490 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
491 }
492
Harald Welte75a983f2008-12-27 21:34:06 +0000493 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000494}
495
Harald Welte255539c2008-12-28 02:26:27 +0000496
497static void loc_upd_rej_cb(void *data)
498{
499 struct gsm_lchan *lchan = data;
500
Holger Freyther73487a22008-12-31 18:53:57 +0000501 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000502 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000503 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000504}
505
Holger Freytherb7193e42008-12-29 17:44:08 +0000506static void schedule_reject(struct gsm_lchan *lchan)
507{
Holger Freyther73487a22008-12-31 18:53:57 +0000508 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
509 lchan->loc_operation->updating_timer.data = lchan;
510 schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000511}
512
Harald Welte231ad4f2008-12-27 11:15:38 +0000513#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000514/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000515static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000516{
Harald Welte8470bf22008-12-25 23:28:35 +0000517 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000518 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000519 struct gsm48_loc_upd_req *lu;
520 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000521 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000522 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000523 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000524 char mi_string[MI_SIZE];
525 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000526
Harald Welte8470bf22008-12-25 23:28:35 +0000527 lu = (struct gsm48_loc_upd_req *) gh->data;
528
529 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000530
Harald Weltefc977a82008-12-27 10:19:37 +0000531 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
532
Harald Welte231ad4f2008-12-27 11:15:38 +0000533 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s)\n", mi_type, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000534
535 allocate_loc_updating_req(lchan);
536
Harald Welte52b1f982008-12-23 20:25:15 +0000537 switch (mi_type) {
538 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000539 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000540 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000541 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000542 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000543
Harald Welte52b1f982008-12-23 20:25:15 +0000544 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000545 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000546 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000547 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000548 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000549 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000550 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEISV);
Holger Freyther73487a22008-12-31 18:53:57 +0000551 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000552
Harald Welte52b1f982008-12-23 20:25:15 +0000553 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000554 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000555 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000556 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000557 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000558 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000559 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000560 }
561 break;
562 case GSM_MI_TYPE_IMEI:
563 case GSM_MI_TYPE_IMEISV:
564 /* no sim card... FIXME: what to do ? */
565 fprintf(stderr, "Unimplemented mobile identity type\n");
566 break;
567 default:
568 fprintf(stderr, "Unknown mobile identity type\n");
569 break;
570 }
571
Harald Welte255539c2008-12-28 02:26:27 +0000572 lchan->subscr = subscr;
573
Holger Freyther73487a22008-12-31 18:53:57 +0000574 /*
575 * Schedule the reject timer and check if we can let the
576 * subscriber into our network immediately or if we need to wait
577 * for identity responses.
578 */
579 schedule_reject(lchan);
580 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000581 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000582
Harald Welte75a983f2008-12-27 21:34:06 +0000583 db_subscriber_alloc_tmsi(subscr);
Harald Welte52b1f982008-12-23 20:25:15 +0000584 subscr_update(subscr, bts);
Harald Welte8470bf22008-12-25 23:28:35 +0000585
Harald Welte255539c2008-12-28 02:26:27 +0000586 tmsi = strtoul(subscr->tmsi, NULL, 10);
587
Holger Freyther73487a22008-12-31 18:53:57 +0000588 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000589 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000590}
591
Harald Welte7584aea2009-02-11 11:44:12 +0000592/* 9.1.5 Channel mode modify */
593int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
594{
595 struct msgb *msg = gsm48_msgb_alloc();
596 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
597 struct gsm48_chan_mode_modify *cmm =
598 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
599 u_int16_t arfcn = lchan->ts->trx->arfcn;
600
Harald Welte7ccf7782009-02-17 01:43:01 +0000601 DEBUGP(DRR, "-> CHANNEL MODE MODIFY\n");
602
Harald Welte7584aea2009-02-11 11:44:12 +0000603 msg->lchan = lchan;
604 gh->proto_discr = GSM48_PDISC_RR;
605 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
606
607 /* fill the channel information element, this code
608 * should probably be shared with rsl_rx_chan_rqd() */
609 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
610 cmm->chan_desc.h0.h = 0;
611 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
612 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
613 cmm->mode = mode;
614
615 return gsm48_sendmsg(msg);
616}
617
Harald Weltedb253af2008-12-30 17:56:55 +0000618/* Section 9.2.15a */
619int gsm48_tx_mm_info(struct gsm_lchan *lchan)
620{
621 struct msgb *msg = gsm48_msgb_alloc();
622 struct gsm48_hdr *gh;
623 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000624 u_int8_t *ptr8;
625 u_int16_t *ptr16;
626 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000627 int i;
628
629 msg->lchan = lchan;
630
631 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
632 gh->proto_discr = GSM48_PDISC_MM;
633 gh->msg_type = GSM48_MT_MM_INFO;
634
635 if (net->name_long) {
636 name_len = strlen(net->name_long);
637 /* 10.5.3.5a */
638 ptr8 = msgb_put(msg, 3);
639 ptr8[0] = GSM48_IE_NAME_LONG;
640 ptr8[1] = name_len*2 +1;
641 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
642
643 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
644 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000645 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000646
647 /* FIXME: Use Cell Broadcast, not UCS-2, since
648 * UCS-2 is only supported by later revisions of the spec */
649 }
650
651 if (net->name_short) {
652 name_len = strlen(net->name_short);
653 /* 10.5.3.5a */
654 ptr8 = (u_int8_t *) msgb_put(msg, 3);
655 ptr8[0] = GSM48_IE_NAME_LONG;
656 ptr8[1] = name_len*2 + 1;
657 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
658
Harald Weltee872cb12009-01-01 00:33:37 +0000659 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000660 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000661 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000662 }
663
664#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000665 /* move back to the top */
666 time_t cur_t;
667 struct tm* cur_time;
668 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000669 /* Section 10.5.3.9 */
670 cur_t = time(NULL);
671 cur_time = gmtime(cur_t);
672 ptr8 = msgb_put(msg, 8);
673 ptr8[0] = GSM48_IE_NET_TIME_TZ;
674 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
675 ptr8[2] = to_bcd8(cur_time->tm_mon);
676 ptr8[3] = to_bcd8(cur_time->tm_mday);
677 ptr8[4] = to_bcd8(cur_time->tm_hour);
678 ptr8[5] = to_bcd8(cur_time->tm_min);
679 ptr8[6] = to_bcd8(cur_time->tm_sec);
680 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
681 tz15min = (cur_time->tm_gmtoff)/(60*15);
682 ptr8[6] = to_bcd8(tz15min);
683 if (tz15min < 0)
684 ptr8[6] |= 0x80;
685#endif
686
687 return gsm48_sendmsg(msg);
688}
689
Harald Welte4b634542008-12-27 01:55:51 +0000690static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
691{
Harald Welte4b634542008-12-27 01:55:51 +0000692 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000693 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000694}
Harald Welteba4cf162009-01-10 01:49:35 +0000695
696/* 9.2.6 CM service reject */
697static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
698 enum gsm48_reject_value value)
699{
700 struct msgb *msg = gsm48_msgb_alloc();
701 struct gsm48_hdr *gh;
702
703 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
704
705 msg->lchan = lchan;
706 use_lchan(lchan);
707
708 gh->proto_discr = GSM48_PDISC_MM;
709 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
710 gh->data[0] = value;
711 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
712
713 return gsm48_sendmsg(msg);
714}
715
Harald Welte4ed0e922009-01-10 03:17:30 +0000716
717/*
718 * Handle CM Service Requests
719 * a) Verify that the packet is long enough to contain the information
720 * we require otherwsie reject with INCORRECT_MESSAGE
721 * b) Try to parse the TMSI. If we do not have one reject
722 * c) Check that we know the subscriber with the TMSI otherwise reject
723 * with a HLR cause
724 * d) Set the subscriber on the gsm_lchan and accept
725 */
Harald Welte4b634542008-12-27 01:55:51 +0000726static int gsm48_rx_mm_serv_req(struct msgb *msg)
727{
Harald Welteba4cf162009-01-10 01:49:35 +0000728 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000729 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000730
Harald Welteba4cf162009-01-10 01:49:35 +0000731 struct gsm_subscriber *subscr;
732 struct gsm48_hdr *gh = msgb_l3(msg);
733 struct gsm48_service_request *req =
734 (struct gsm48_service_request *)gh->data;
735
736 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
737 DEBUGP(DMM, "<- CM SERVICE REQUEST wrong sized message\n");
738 return gsm48_tx_mm_serv_rej(msg->lchan,
739 GSM48_REJECT_INCORRECT_MESSAGE);
740 }
741
742 if (msg->data_len < req->mi_len + 6) {
743 DEBUGP(DMM, "<- CM SERVICE REQUEST MI does not fit in package\n");
744 return gsm48_tx_mm_serv_rej(msg->lchan,
745 GSM48_REJECT_INCORRECT_MESSAGE);
746 }
747
748 mi_type = req->mi[0] & GSM_MI_TYPE_MASK;
749 if (mi_type != GSM_MI_TYPE_TMSI) {
750 DEBUGP(DMM, "<- CM SERVICE REQUEST mi type is not TMSI: %d\n", mi_type);
751 return gsm48_tx_mm_serv_rej(msg->lchan,
752 GSM48_REJECT_INCORRECT_MESSAGE);
753 }
754
Harald Welte4ed0e922009-01-10 03:17:30 +0000755 mi_to_string(mi_string, sizeof(mi_string), req->mi, req->mi_len);
756 subscr = subscr_get_by_tmsi(mi_string);
757 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x mi_type=0x%02x M(%s)\n",
758 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000759
Harald Welte4ed0e922009-01-10 03:17:30 +0000760 if (!subscr)
761 return gsm48_tx_mm_serv_rej(msg->lchan,
762 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
763
764 if (!msg->lchan->subscr)
765 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000766 else if (msg->lchan->subscr != subscr) {
767 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
768 subscr_put(subscr);
769 }
770
Harald Welte4b634542008-12-27 01:55:51 +0000771 return gsm48_tx_mm_serv_ack(msg->lchan);
772}
773
Harald Weltebf5e8df2009-02-03 12:59:45 +0000774/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000775static int gsm0408_rcv_mm(struct msgb *msg)
776{
777 struct gsm48_hdr *gh = msgb_l3(msg);
778 int rc;
779
780 switch (gh->msg_type & 0xbf) {
781 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000782 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000783 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000784 break;
785 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000786 rc = mm_rx_id_resp(msg);
787 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000788 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000789 rc = gsm48_rx_mm_serv_req(msg);
790 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000791 case GSM48_MT_MM_STATUS:
792 DEBUGP(DMM, "MM STATUS: FIXME parse error cond.\n");
793 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000794 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000795 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
796 msg->lchan->subscr ?
797 msg->lchan->subscr->imsi :
798 "unknown subscriber");
799 break;
800 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte231ad4f2008-12-27 11:15:38 +0000801 case GSM48_MT_MM_AUTH_RESP:
802 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte52b1f982008-12-23 20:25:15 +0000803 fprintf(stderr, "Unimplemented GSM 04.08 MM msg type 0x%02x\n",
804 gh->msg_type);
805 break;
806 default:
807 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
808 gh->msg_type);
809 break;
810 }
811
812 return rc;
813}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000814
Harald Welte2d35ae62009-02-06 12:02:13 +0000815/* Receive a PAGING RESPONSE message from the MS */
816static int gsm48_rr_rx_pag_resp(struct msgb *msg)
817{
818 struct gsm48_hdr *gh = msgb_l3(msg);
819 struct gsm48_paging_response *pr =
820 (struct gsm48_paging_response *) gh->data;
821 u_int8_t mi_type = pr->mi[0] & GSM_MI_TYPE_MASK;
822 char mi_string[MI_SIZE];
823 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000824 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000825 int rc = 0;
826
827 mi_to_string(mi_string, sizeof(mi_string), &pr->mi[0], pr->mi_len);
828 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
829 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000830 switch (mi_type) {
831 case GSM_MI_TYPE_TMSI:
832 subscr = subscr_get_by_tmsi(mi_string);
833 break;
834 case GSM_MI_TYPE_IMSI:
835 subscr = subscr_get_by_imsi(mi_string);
836 break;
837 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000838
839 if (!subscr) {
840 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000841 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000842 return -EINVAL;
843 }
844 DEBUGP(DRR, "<- Channel was requested by %s\n",
845 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000846
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000847 if (!msg->lchan->subscr)
848 msg->lchan->subscr = subscr;
849 else if (msg->lchan->subscr != subscr) {
850 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
851 subscr_put(subscr);
852 }
853
Harald Welte595ad7b2009-02-16 22:05:44 +0000854 sig_data.subscr = subscr;
855 sig_data.bts = msg->lchan->ts->trx->bts;
856 sig_data.lchan = msg->lchan;
857
858 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +0000859 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +0000860
Harald Welte7584aea2009-02-11 11:44:12 +0000861 /* FIXME: somehow signal the completion of the PAGING to
862 * the entity that requested the paging */
863
Harald Welte2d35ae62009-02-06 12:02:13 +0000864 return rc;
865}
866
Harald Weltebf5e8df2009-02-03 12:59:45 +0000867/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000868static int gsm0408_rcv_rr(struct msgb *msg)
869{
870 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000871 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000872
873 switch (gh->msg_type) {
874 case GSM48_MT_RR_CLSM_CHG:
875 DEBUGP(DRR, "CLASSMARK CHANGE\n");
876 /* FIXME: what to do ?!? */
877 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000878 case GSM48_MT_RR_GPRS_SUSP_REQ:
879 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
880 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000881 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000882 rc = gsm48_rr_rx_pag_resp(msg);
883 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000884 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
885 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +0000886 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +0000887 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000888 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000889 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000890 gh->msg_type);
891 break;
892 }
893
Harald Welte2d35ae62009-02-06 12:02:13 +0000894 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000895}
896
Holger Freythere64a7a32009-02-06 21:55:37 +0000897/* 7.1.7 and 9.1.7 Channel release*/
898int gsm48_send_rr_release(struct gsm_lchan *lchan)
899{
900 struct msgb *msg = gsm48_msgb_alloc();
901 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
902 u_int8_t *cause;
903
904 msg->lchan = lchan;
905 gh->proto_discr = GSM48_PDISC_RR;
906 gh->msg_type = GSM48_MT_RR_CHAN_REL;
907
908 cause = msgb_put(msg, 1);
909 cause[0] = GSM48_RR_CAUSE_NORMAL;
910
911 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
912 lchan->nr, lchan->type);
913
914 return gsm48_sendmsg(msg);
915}
916
Harald Welte4bc90a12008-12-27 16:32:52 +0000917/* Call Control */
918
Harald Welte7584aea2009-02-11 11:44:12 +0000919/* The entire call control code is written in accordance with Figure 7.10c
920 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
921 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
922 * it for voice */
923
Harald Welte4bc90a12008-12-27 16:32:52 +0000924static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
925{
926 struct msgb *msg = gsm48_msgb_alloc();
927 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
928 u_int8_t *cause, *call_state;
929
Harald Welte65e74cc2008-12-29 01:55:35 +0000930 gh->proto_discr = GSM48_PDISC_CC;
931
Harald Welte4bc90a12008-12-27 16:32:52 +0000932 msg->lchan = lchan;
933
Harald Welte4bc90a12008-12-27 16:32:52 +0000934 gh->msg_type = GSM48_MT_CC_STATUS;
935
936 cause = msgb_put(msg, 3);
937 cause[0] = 2;
938 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
939 cause[2] = 0x80 | 30; /* response to status inquiry */
940
941 call_state = msgb_put(msg, 1);
942 call_state[0] = 0xc0 | 0x00;
943
Harald Welte65e74cc2008-12-29 01:55:35 +0000944 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000945}
946
Harald Welte6f4b7532008-12-29 00:39:37 +0000947static int gsm48_tx_simple(struct gsm_lchan *lchan,
948 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +0000949{
950 struct msgb *msg = gsm48_msgb_alloc();
951 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
952
953 msg->lchan = lchan;
954
Harald Welte6f4b7532008-12-29 00:39:37 +0000955 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +0000956 gh->msg_type = msg_type;
957
Harald Welte65e74cc2008-12-29 01:55:35 +0000958 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +0000959}
960
Harald Welte09e38af2009-02-16 22:52:23 +0000961/* call-back from paging the B-end of the connection */
962static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +0000963 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +0000964{
Harald Welte7ccf7782009-02-17 01:43:01 +0000965 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +0000966 struct gsm_call *remote_call = param;
967 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +0000968 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000969
970 if (hooknum != GSM_HOOK_RR_PAGING)
971 return -EINVAL;
972
973 switch (event) {
974 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +0000975 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +0000976 remote_call->remote_lchan = lchan;
977 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +0000978 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +0000979 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
980 if (is_ipaccess_bts(lchan->ts->trx->bts))
981 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +0000982 break;
983 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +0000984 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000985 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +0000986 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +0000987 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +0000988 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +0000989 break;
990 }
Harald Welte7ccf7782009-02-17 01:43:01 +0000991 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +0000992}
993
Harald Welte4bc90a12008-12-27 16:32:52 +0000994static int gsm48_cc_rx_status_enq(struct msgb *msg)
995{
996 return gsm48_cc_tx_status(msg->lchan);
997}
998
999static int gsm48_cc_rx_setup(struct msgb *msg)
1000{
Harald Welte7584aea2009-02-11 11:44:12 +00001001 struct gsm_call *call = &msg->lchan->call;
1002 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001003 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001004 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001005 char called_number[(43-2)*2 + 1] = "\0";
1006 struct tlv_parsed tp;
1007 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +00001008 int ret;
1009
1010 if (call->state == GSM_CSTATE_NULL ||
1011 call->state == GSM_CSTATE_RELEASE_REQ)
1012 use_lchan(msg->lchan);
1013
1014 call->type = GSM_CT_MO;
1015 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001016 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001017 call->transaction_id = gh->proto_discr & 0xf0;
1018
Harald Welte09e38af2009-02-16 22:52:23 +00001019 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
1020 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1021 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001022
Harald Welte09e38af2009-02-16 22:52:23 +00001023 /* Parse the number that was dialed and lookup subscriber */
1024 num_type = decode_bcd_number(called_number, sizeof(called_number),
1025 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001026
Harald Welte4b458152009-02-17 13:23:06 +00001027 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001028 called_number);
1029
Harald Welte09e38af2009-02-16 22:52:23 +00001030 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001031 if (!called_subscr) {
1032 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1033 put_lchan(msg->lchan);
1034 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001035 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001036 }
Harald Welte5a065df2009-02-22 21:13:18 +00001037 if (called_subscr == msg->lchan->subscr) {
1038 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1039 put_lchan(msg->lchan);
1040 subscr_put(called_subscr);
1041 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1042 GSM48_MT_CC_RELEASE_COMPL);
1043 }
Harald Welte7584aea2009-02-11 11:44:12 +00001044
Harald Welte09e38af2009-02-16 22:52:23 +00001045 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001046 call->called_subscr = called_subscr;
1047
Harald Welte7584aea2009-02-11 11:44:12 +00001048 /* start paging of the receiving end of the call */
Harald Welte09e38af2009-02-16 22:52:23 +00001049 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1050 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001051
1052 /* send a CALL PROCEEDING message to the MO */
1053 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1054 GSM48_MT_CC_CALL_PROC);
1055
Harald Welte49f48b82009-02-17 15:29:33 +00001056 if (is_ipaccess_bts(msg->trx->bts))
1057 rsl_ipacc_bind(msg->lchan);
1058
Harald Welte7584aea2009-02-11 11:44:12 +00001059 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001060 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001061
1062err:
1063 /* FIXME: send some kind of RELEASE */
1064 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001065}
Harald Welte7584aea2009-02-11 11:44:12 +00001066
Harald Welte7ccf7782009-02-17 01:43:01 +00001067static int gsm48_cc_rx_alerting(struct msgb *msg)
1068{
1069 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001070
Harald Welte4b458152009-02-17 13:23:06 +00001071 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001072
1073 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001074 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001075 return -EIO;
1076
Harald Welte4b458152009-02-17 13:23:06 +00001077 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001078 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001079 GSM48_MT_CC_ALERTING);
1080}
1081
Harald Welte49f48b82009-02-17 15:29:33 +00001082/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001083static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001084{
Harald Welte11fa29c2009-02-19 17:24:39 +00001085 struct gsm_bts *bts = lchan->ts->trx->bts;
1086 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001087 struct gsm_bts_trx_ts *ts;
1088
Harald Welte11fa29c2009-02-19 17:24:39 +00001089 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1090 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1091 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1092
1093 if (bts->type != remote_bts->type) {
1094 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1095 return -EINVAL;
1096 }
Harald Welte49f48b82009-02-17 15:29:33 +00001097
Harald Welte11fa29c2009-02-19 17:24:39 +00001098 switch (bts->type) {
1099 case GSM_BTS_TYPE_NANOBTS_900:
1100 case GSM_BTS_TYPE_NANOBTS_1800:
1101 ts = remote_lchan->ts;
1102 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1103 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1104
1105 ts = lchan->ts;
1106 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1107 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1108 break;
1109 case GSM_BTS_TYPE_BS11:
1110 trau_mux_map_lchan(lchan, remote_lchan);
1111 break;
1112 default:
1113 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1114 break;
1115 }
Harald Welte49f48b82009-02-17 15:29:33 +00001116
1117 return 0;
1118}
1119
Harald Welte7ccf7782009-02-17 01:43:01 +00001120static int gsm48_cc_rx_connect(struct msgb *msg)
1121{
1122 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001123 int rc;
1124
Harald Welte4b458152009-02-17 13:23:06 +00001125 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001126
1127 rc = tch_map(msg->lchan, call->remote_lchan);
1128 if (rc)
1129 return -EIO;
1130
1131 if (!call->remote_lchan)
1132 return -EIO;
1133
Harald Welte4b458152009-02-17 13:23:06 +00001134 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001135 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1136 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001137 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001138
Harald Welte49f48b82009-02-17 15:29:33 +00001139
1140 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001141 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001142 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001143 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001144}
1145
1146static int gsm48_cc_rx_disconnect(struct msgb *msg)
1147{
1148 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001149 int rc;
1150
1151
1152 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001153 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001154 call->state = GSM_CSTATE_RELEASE_REQ;
1155 if (call->state != GSM_CSTATE_NULL)
1156 put_lchan(msg->lchan);
1157 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001158 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001159 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1160 GSM48_MT_CC_RELEASE);
1161
1162 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001163 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001164 return -EIO;
1165
Harald Welte4b458152009-02-17 13:23:06 +00001166 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001167 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001168 GSM48_MT_CC_DISCONNECT);
1169}
1170
1171static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1172
Harald Welte49f48b82009-02-17 15:29:33 +00001173int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1174 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001175{
1176 struct msgb *msg = gsm48_msgb_alloc();
1177 struct gsm48_hdr *gh;
1178 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001179 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001180
Harald Welte7ccf7782009-02-17 01:43:01 +00001181 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001182
1183 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001184
1185 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001186 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001187
1188 gh->proto_discr = GSM48_PDISC_CC;
1189 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001190
Harald Welte7584aea2009-02-11 11:44:12 +00001191 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001192 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001193 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001194 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001195 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1196 bcd_lv[0], bcd_lv+1);
1197 }
Harald Welte49f48b82009-02-17 15:29:33 +00001198 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001199 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001200 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001201 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1202 bcd_lv[0], bcd_lv+1);
1203 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001204
Harald Welte4b458152009-02-17 13:23:06 +00001205 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001206
1207 return gsm48_sendmsg(msg);
1208}
1209
Harald Welte4bc90a12008-12-27 16:32:52 +00001210static int gsm0408_rcv_cc(struct msgb *msg)
1211{
1212 struct gsm48_hdr *gh = msgb_l3(msg);
1213 u_int8_t msg_type = gh->msg_type & 0xbf;
1214 struct gsm_call *call = &msg->lchan->call;
1215 int rc = 0;
1216
1217 switch (msg_type) {
1218 case GSM48_MT_CC_CALL_CONF:
1219 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001220 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001221 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001222 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001223 break;
1224 case GSM48_MT_CC_RELEASE_COMPL:
1225 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001226 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001227 call->state = GSM_CSTATE_NULL;
1228 break;
1229 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001230 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001231 break;
1232 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001233 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001234 break;
1235 case GSM48_MT_CC_CONNECT_ACK:
1236 /* MO: Answer to CONNECT */
1237 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001238 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001239 break;
1240 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001241 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001242 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001243 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001244 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1245 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte49f48b82009-02-17 15:29:33 +00001246 put_lchan(msg->lchan);
1247 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001248 break;
1249 case GSM48_MT_CC_STATUS_ENQ:
1250 rc = gsm48_cc_rx_status_enq(msg);
1251 break;
1252 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001253 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001254 break;
1255 case GSM48_MT_CC_SETUP:
Harald Welte7584aea2009-02-11 11:44:12 +00001256 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001257 break;
1258 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001259 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001260 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1261 break;
1262 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001263 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001264 msg_type);
1265 break;
1266 }
1267
1268 return rc;
1269}
1270
Harald Welte52b1f982008-12-23 20:25:15 +00001271/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1272int gsm0408_rcvmsg(struct msgb *msg)
1273{
1274 struct gsm48_hdr *gh = msgb_l3(msg);
1275 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001276 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001277
1278 switch (pdisc) {
1279 case GSM48_PDISC_CC:
1280 rc = gsm0408_rcv_cc(msg);
1281 break;
1282 case GSM48_PDISC_MM:
1283 rc = gsm0408_rcv_mm(msg);
1284 break;
1285 case GSM48_PDISC_RR:
1286 rc = gsm0408_rcv_rr(msg);
1287 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001288 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001289 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001290 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001291 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001292 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001293 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1294 pdisc);
1295 break;
1296 default:
1297 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1298 pdisc);
1299 break;
1300 }
1301
1302 return rc;
1303}
Harald Welte8470bf22008-12-25 23:28:35 +00001304
1305enum chreq_type {
1306 CHREQ_T_EMERG_CALL,
1307 CHREQ_T_CALL_REEST_TCH_F,
1308 CHREQ_T_CALL_REEST_TCH_H,
1309 CHREQ_T_CALL_REEST_TCH_H_DBL,
1310 CHREQ_T_SDCCH,
1311 CHREQ_T_TCH_F,
1312 CHREQ_T_VOICE_CALL_TCH_H,
1313 CHREQ_T_DATA_CALL_TCH_H,
1314 CHREQ_T_LOCATION_UPD,
1315 CHREQ_T_PAG_R_ANY,
1316 CHREQ_T_PAG_R_TCH_F,
1317 CHREQ_T_PAG_R_TCH_FH,
1318};
1319
1320/* Section 9.1.8 / Table 9.9 */
1321struct chreq {
1322 u_int8_t val;
1323 u_int8_t mask;
1324 enum chreq_type type;
1325};
1326
1327/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1328static const struct chreq chreq_type_neci1[] = {
1329 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1330 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1331 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1332 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1333 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1334 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1335 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1336 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1337 { 0x10, 0xf0, CHREQ_T_SDCCH },
1338 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1339 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1340 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1341};
1342
1343/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1344static const struct chreq chreq_type_neci0[] = {
1345 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1346 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1347 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1348 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1349 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1350 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1351 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1352 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1353};
1354
1355static const enum gsm_chan_t ctype_by_chreq[] = {
1356 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1357 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1358 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1359 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1360 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1361 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1362 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1363 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1364 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1365 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1366 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1367 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1368};
1369
Harald Weltee14a57c2008-12-29 04:08:28 +00001370static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1371 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1372 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1373 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1374 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1375 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1376 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1377 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1378 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1379 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1380 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1381 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1382 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1383};
1384
Harald Welte8470bf22008-12-25 23:28:35 +00001385enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1386{
1387 int i;
1388 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1389
1390 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1391 const struct chreq *chr = &chreq_type_neci0[i];
1392 if ((ra & chr->mask) == chr->val)
1393 return ctype_by_chreq[chr->type];
1394 }
1395 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1396 return GSM_LCHAN_SDCCH;
1397}
Harald Weltee14a57c2008-12-29 04:08:28 +00001398
1399enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1400{
1401 int i;
1402 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1403
1404 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1405 const struct chreq *chr = &chreq_type_neci0[i];
1406 if ((ra & chr->mask) == chr->val)
1407 return reason_by_chreq[chr->type];
1408 }
1409 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1410 return GSM_CHREQ_REASON_OTHER;
1411}