blob: ad7bba687641b2461c9fe264499f43dbea971c55 [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};
Harald Weltecf5b3592009-05-01 18:28:42 +000087
88static const char *rr_cause_names[] = {
89 [GSM48_RR_CAUSE_NORMAL] = "Normal event",
90 [GSM48_RR_CAUSE_ABNORMAL_UNSPEC] = "Abnormal release, unspecified",
91 [GSM48_RR_CAUSE_ABNORMAL_UNACCT] = "Abnormal release, channel unacceptable",
92 [GSM48_RR_CAUSE_ABNORMAL_TIMER] = "Abnormal release, timer expired",
93 [GSM48_RR_CAUSE_ABNORMAL_NOACT] = "Abnormal release, no activity on radio path",
94 [GSM48_RR_CAUSE_PREMPTIVE_REL] = "Preemptive release",
95 [GSM48_RR_CAUSE_HNDOVER_IMP] = "Handover impossible, timing advance out of range",
96 [GSM48_RR_CAUSE_CHAN_MODE_UNACCT] = "Channel mode unacceptable",
97 [GSM48_RR_CAUSE_FREQ_NOT_IMPL] = "Frequency not implemented",
98 [GSM48_RR_CAUSE_CALL_CLEARED] = "Call already cleared",
99 [GSM48_RR_CAUSE_SEMANT_INCORR] = "Semantically incorrect message",
100 [GSM48_RR_CAUSE_INVALID_MAND_INF] = "Invalid mandatory information",
101 [GSM48_RR_CAUSE_MSG_TYPE_N] = "Message type non-existant or not implemented",
102 [GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT] = "Message type not compatible with protocol state",
103 [GSM48_RR_CAUSE_COND_IE_ERROR] = "Conditional IE error",
104 [GSM48_RR_CAUSE_NO_CELL_ALLOC_A] = "No cell allocation available",
105 [GSM48_RR_CAUSE_PROT_ERROR_UNSPC] = "Protocol error unspecified",
106};
107
108static char strbuf[64];
109
110static const char *rr_cause_name(u_int8_t cause)
111{
112 if (cause < ARRAY_SIZE(rr_cause_names) &&
113 rr_cause_names[cause])
114 return rr_cause_names[cause];
115
116 snprintf(strbuf, sizeof(strbuf), "0x%02x", cause);
117 return strbuf;
118}
119
Harald Welte65e74cc2008-12-29 01:55:35 +0000120static int gsm48_tx_simple(struct gsm_lchan *lchan,
121 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +0000122static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +0000123
Harald Welte52b1f982008-12-23 20:25:15 +0000124struct gsm_lai {
125 u_int16_t mcc;
126 u_int16_t mnc;
127 u_int16_t lac;
128};
129
Holger Freyther89824fc2008-12-30 16:18:18 +0000130static int authorize_everonye = 0;
131void gsm0408_allow_everyone(int everyone)
132{
133 printf("Allowing everyone?\n");
134 authorize_everonye = everyone;
135}
136
Holger Freythere97f7fb2008-12-31 18:52:11 +0000137static int reject_cause = 0;
138void gsm0408_set_reject_cause(int cause)
139{
140 reject_cause = cause;
141}
142
Holger Freyther73487a22008-12-31 18:53:57 +0000143static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
144 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000145{
146 if (!subscriber)
147 return 0;
148
Holger Freyther73487a22008-12-31 18:53:57 +0000149 /*
150 * Do not send accept yet as more information should arrive. Some
151 * phones will not send us the information and we will have to check
152 * what we want to do with that.
153 */
154 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
155 return 0;
156
Holger Freyther89824fc2008-12-30 16:18:18 +0000157 if (authorize_everonye)
158 return 1;
159
160 return subscriber->authorized;
161}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000162
Holger Freyther73487a22008-12-31 18:53:57 +0000163static void release_loc_updating_req(struct gsm_lchan *lchan)
164{
Harald Welte179f0642008-12-31 23:59:18 +0000165 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000166 return;
167
Harald Welteff117a82009-05-23 05:22:08 +0000168 bsc_del_timer(&lchan->loc_operation->updating_timer);
Holger Freyther73487a22008-12-31 18:53:57 +0000169 free(lchan->loc_operation);
170 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000171 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000172}
173
174static void allocate_loc_updating_req(struct gsm_lchan *lchan)
175{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000176 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000177 release_loc_updating_req(lchan);
178
179 lchan->loc_operation = (struct gsm_loc_updating_operation *)
180 malloc(sizeof(*lchan->loc_operation));
181 memset(lchan->loc_operation, 0, sizeof(*lchan->loc_operation));
182}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000183
Harald Welte52b1f982008-12-23 20:25:15 +0000184static void to_bcd(u_int8_t *bcd, u_int16_t val)
185{
Harald Welte4b634542008-12-27 01:55:51 +0000186 bcd[2] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000187 val = val / 10;
188 bcd[1] = val % 10;
189 val = val / 10;
Harald Welte4b634542008-12-27 01:55:51 +0000190 bcd[0] = val % 10;
Harald Welte52b1f982008-12-23 20:25:15 +0000191 val = val / 10;
192}
193
Holger Freyther17746612008-12-28 16:32:44 +0000194void gsm0408_generate_lai(struct gsm48_loc_area_id *lai48, u_int16_t mcc,
Harald Welte52b1f982008-12-23 20:25:15 +0000195 u_int16_t mnc, u_int16_t lac)
196{
197 u_int8_t bcd[3];
198
199 to_bcd(bcd, mcc);
200 lai48->digits[0] = bcd[0] | (bcd[1] << 4);
201 lai48->digits[1] = bcd[2];
202
203 to_bcd(bcd, mnc);
Harald Welte4b634542008-12-27 01:55:51 +0000204 /* FIXME: do we need three-digit MNC? See Table 10.5.3 */
205#if 0
Harald Welte8470bf22008-12-25 23:28:35 +0000206 lai48->digits[1] |= bcd[2] << 4;
207 lai48->digits[2] = bcd[0] | (bcd[1] << 4);
Harald Welte4b634542008-12-27 01:55:51 +0000208#else
209 lai48->digits[1] |= 0xf << 4;
210 lai48->digits[2] = bcd[1] | (bcd[2] << 4);
211#endif
Harald Welte52b1f982008-12-23 20:25:15 +0000212
Harald Welte4b634542008-12-27 01:55:51 +0000213 lai48->lac = htons(lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000214}
215
Harald Welte255539c2008-12-28 02:26:27 +0000216#define TMSI_LEN 5
Harald Welte52b1f982008-12-23 20:25:15 +0000217#define MID_TMSI_LEN (TMSI_LEN + 2)
218
Harald Welte255539c2008-12-28 02:26:27 +0000219int generate_mid_from_tmsi(u_int8_t *buf, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000220{
Harald Welte65e74cc2008-12-29 01:55:35 +0000221 u_int32_t *tptr = (u_int32_t *) &buf[3];
Harald Welte255539c2008-12-28 02:26:27 +0000222
Harald Welte4b634542008-12-27 01:55:51 +0000223 buf[0] = GSM48_IE_MOBILE_ID;
Harald Welte1a412182008-12-27 22:13:43 +0000224 buf[1] = TMSI_LEN;
Harald Welte4b634542008-12-27 01:55:51 +0000225 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Harald Welte255539c2008-12-28 02:26:27 +0000226 *tptr = htonl(tmsi);
227
228 return 7;
Harald Welte52b1f982008-12-23 20:25:15 +0000229}
230
Harald Welte09e38af2009-02-16 22:52:23 +0000231static const char bcd_num_digits[] = {
232 '0', '1', '2', '3', '4', '5', '6', '7',
233 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
234};
235
236/* decode a 'called party BCD number' as in 10.5.4.7 */
237u_int8_t decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv)
238{
239 u_int8_t in_len = bcd_lv[0];
240 int i;
241
242 if (in_len < 1)
243 return 0;
244
245 for (i = 2; i <= in_len; i++) {
246 /* lower nibble */
247 output_len--;
248 if (output_len <= 1)
249 break;
250 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
251
252 /* higher nibble */
253 output_len--;
254 if (output_len <= 1)
255 break;
256 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
257 }
258 if (output_len >= 1)
259 *output++ = '\0';
260
261 /* return number type / calling plan */
262 return bcd_lv[1] & 0x3f;
263}
264
265/* convert a single ASCII character to call-control BCD */
266static int asc_to_bcd(const char asc)
267{
268 int i;
269
270 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
271 if (bcd_num_digits[i] == asc)
272 return i;
273 }
274 return -EINVAL;
275}
276
277/* convert a ASCII phone number to 'called party BCD number' */
278int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
279 u_int8_t type, const char *input)
280{
281 int in_len = strlen(input);
282 int i;
283 u_int8_t *bcd_cur = bcd_lv + 2;
284
285 if (in_len/2 + 1 > max_len)
286 return -EIO;
287
288 /* two digits per byte, plus type byte */
289 bcd_lv[0] = in_len/2 + 1;
290 if (in_len % 2)
291 bcd_lv[0]++;
292
293 /* if the caller wants to create a valid 'calling party BCD
294 * number', then the extension bit MUST NOT be set. For the
295 * 'called party BCD number' it MUST be set. *sigh */
296 bcd_lv[1] = type;
297
298 for (i = 0; i < in_len; i++) {
299 int rc = asc_to_bcd(input[i]);
300 if (rc < 0)
301 return rc;
302 if (i % 2 == 0)
303 *bcd_cur = rc;
304 else
305 *bcd_cur++ |= (rc << 4);
306 }
307 /* append padding nibble in case of odd length */
308 if (i % 2)
309 *bcd_cur++ |= 0xf0;
310
311 /* return how many bytes we used */
312 return (bcd_cur - bcd_lv);
313}
314
Holger Freyther819dd202009-01-04 03:52:50 +0000315struct msgb *gsm48_msgb_alloc(void)
Harald Welte8470bf22008-12-25 23:28:35 +0000316{
317 return msgb_alloc_headroom(GSM48_ALLOC_SIZE, GSM48_ALLOC_HEADROOM);
318}
319
Holger Freyther3e2c3232009-01-04 03:55:31 +0000320int gsm48_sendmsg(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000321{
Harald Welte65e74cc2008-12-29 01:55:35 +0000322 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
323
324 if (msg->lchan) {
Harald Welte8470bf22008-12-25 23:28:35 +0000325 msg->trx = msg->lchan->ts->trx;
Harald Welte52b1f982008-12-23 20:25:15 +0000326
Harald Welte65e74cc2008-12-29 01:55:35 +0000327 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC) {
328 /* Send a 04.08 call control message, add transaction
329 * ID and TI flag */
330 gh->proto_discr |= msg->lchan->call.transaction_id;
331
332 /* GSM 04.07 Section 11.2.3.1.3 */
333 switch (msg->lchan->call.type) {
334 case GSM_CT_MO:
335 gh->proto_discr |= 0x80;
336 break;
337 case GSM_CT_MT:
338 break;
Holger Freytherca362a62009-01-04 21:05:01 +0000339 case GSM_CT_NONE:
340 break;
Harald Welte65e74cc2008-12-29 01:55:35 +0000341 }
342 }
343 }
344
Harald Welte4b634542008-12-27 01:55:51 +0000345 msg->l3h = msg->data;
346
Harald Welte8470bf22008-12-25 23:28:35 +0000347 return rsl_data_request(msg, 0);
Harald Welte52b1f982008-12-23 20:25:15 +0000348}
349
Harald Welte52b1f982008-12-23 20:25:15 +0000350
Holger Freyther429e7762008-12-30 13:28:30 +0000351/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000352int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000353{
Harald Welte8470bf22008-12-25 23:28:35 +0000354 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000355 struct gsm48_hdr *gh;
356
Harald Welte8470bf22008-12-25 23:28:35 +0000357 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000358
359 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
360 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000361 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000362 gh->data[0] = cause;
363
Harald Weltedb253af2008-12-30 17:56:55 +0000364 DEBUGP(DMM, "-> LOCATION UPDATING REJECT on channel: %d\n", lchan->nr);
365
Harald Welte65e74cc2008-12-29 01:55:35 +0000366 return gsm48_sendmsg(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000367}
368
369/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000370int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000371{
Harald Welte8470bf22008-12-25 23:28:35 +0000372 struct gsm_bts *bts = lchan->ts->trx->bts;
373 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000374 struct gsm48_hdr *gh;
375 struct gsm48_loc_area_id *lai;
376 u_int8_t *mid;
Holger Freyther07cc8d82008-12-29 06:23:46 +0000377 int ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000378
Harald Welte8470bf22008-12-25 23:28:35 +0000379 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000380
381 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
382 gh->proto_discr = GSM48_PDISC_MM;
383 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
384
385 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Holger Freyther17746612008-12-28 16:32:44 +0000386 gsm0408_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000387 bts->network->network_code, bts->location_area_code);
388
389 mid = msgb_put(msg, MID_TMSI_LEN);
390 generate_mid_from_tmsi(mid, tmsi);
391
392 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
393
Harald Weltedb253af2008-12-30 17:56:55 +0000394 ret = gsm48_sendmsg(msg);
395
Harald Weltedb253af2008-12-30 17:56:55 +0000396 ret = gsm48_tx_mm_info(lchan);
Harald Weltedb253af2008-12-30 17:56:55 +0000397
Holger Freyther07cc8d82008-12-29 06:23:46 +0000398 return ret;
Harald Welte52b1f982008-12-23 20:25:15 +0000399}
400
Harald Weltefc977a82008-12-27 10:19:37 +0000401static char bcd2char(u_int8_t bcd)
402{
403 if (bcd < 0xa)
404 return '0' + bcd;
405 else
406 return 'A' + (bcd - 0xa);
407}
408
Harald Weltebf5e8df2009-02-03 12:59:45 +0000409/* Convert Mobile Identity (10.5.1.4) to string */
Harald Weltefc977a82008-12-27 10:19:37 +0000410static int mi_to_string(char *string, int str_len, u_int8_t *mi, int mi_len)
411{
412 int i;
413 u_int8_t mi_type;
414 char *str_cur = string;
Harald Welte4ed0e922009-01-10 03:17:30 +0000415 u_int32_t tmsi;
Harald Weltefc977a82008-12-27 10:19:37 +0000416
417 mi_type = mi[0] & GSM_MI_TYPE_MASK;
418
419 switch (mi_type) {
420 case GSM_MI_TYPE_NONE:
421 break;
422 case GSM_MI_TYPE_TMSI:
Harald Welte4ed0e922009-01-10 03:17:30 +0000423 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
424 if (mi_len == TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
425 memcpy(&tmsi, &mi[1], 4);
426 tmsi = ntohl(tmsi);
427 return snprintf(string, str_len, "%u", tmsi);
Harald Weltefc977a82008-12-27 10:19:37 +0000428 }
429 break;
430 case GSM_MI_TYPE_IMSI:
431 case GSM_MI_TYPE_IMEI:
432 case GSM_MI_TYPE_IMEISV:
Harald Weltedb253af2008-12-30 17:56:55 +0000433 *str_cur++ = bcd2char(mi[0] >> 4);
434
435 for (i = 1; i < mi_len; i++) {
Harald Weltefc977a82008-12-27 10:19:37 +0000436 if (str_cur + 2 >= string + str_len)
437 return str_cur - string;
438 *str_cur++ = bcd2char(mi[i] & 0xf);
Harald Weltedb253af2008-12-30 17:56:55 +0000439 /* skip last nibble in last input byte when GSM_EVEN */
440 if( (i != mi_len-1) || (mi[0] & GSM_MI_ODD))
441 *str_cur++ = bcd2char(mi[i] >> 4);
Harald Weltefc977a82008-12-27 10:19:37 +0000442 }
443 break;
444 default:
445 break;
446 }
Harald Weltefc977a82008-12-27 10:19:37 +0000447 *str_cur++ = '\0';
Harald Weltedb253af2008-12-30 17:56:55 +0000448
Harald Weltefc977a82008-12-27 10:19:37 +0000449 return str_cur - string;
450}
451
Harald Weltebf5e8df2009-02-03 12:59:45 +0000452/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000453static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
454{
455 struct msgb *msg = gsm48_msgb_alloc();
456 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000457
Harald Welte231ad4f2008-12-27 11:15:38 +0000458 msg->lchan = lchan;
459
460 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
461 gh->proto_discr = GSM48_PDISC_MM;
462 gh->msg_type = GSM48_MT_MM_ID_REQ;
463 gh->data[0] = id_type;
464
Harald Welte65e74cc2008-12-29 01:55:35 +0000465 return gsm48_sendmsg(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000466}
467
468#define MI_SIZE 32
469
Harald Weltebf5e8df2009-02-03 12:59:45 +0000470/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000471static int mm_rx_id_resp(struct msgb *msg)
472{
473 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000474 struct gsm_lchan *lchan = msg->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000475 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
476 char mi_string[MI_SIZE];
Harald Welte75a983f2008-12-27 21:34:06 +0000477 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000478
479 mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000480 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000481 mi_type, mi_string);
482
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000483 /*
484 * Rogue messages could trick us but so is life
485 */
486 put_lchan(lchan);
487
Harald Welte75a983f2008-12-27 21:34:06 +0000488 switch (mi_type) {
489 case GSM_MI_TYPE_IMSI:
490 if (!lchan->subscr)
491 lchan->subscr = db_create_subscriber(mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000492 if (lchan->loc_operation)
493 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000494 break;
495 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000496 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000497 /* update subscribe <-> IMEI mapping */
498 if (lchan->subscr)
499 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Holger Freyther73487a22008-12-31 18:53:57 +0000500 if (lchan->loc_operation)
501 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000502 break;
503 }
Holger Freyther73487a22008-12-31 18:53:57 +0000504
505 /* Check if we can let the mobile station enter */
506 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
507 db_subscriber_alloc_tmsi(lchan->subscr);
508 tmsi = strtoul(lchan->subscr->tmsi, NULL, 10);
509 release_loc_updating_req(lchan);
510 return gsm0408_loc_upd_acc(msg->lchan, tmsi);
511 }
512
Harald Welte75a983f2008-12-27 21:34:06 +0000513 return 0;
Harald Welte231ad4f2008-12-27 11:15:38 +0000514}
515
Harald Welte255539c2008-12-28 02:26:27 +0000516
517static void loc_upd_rej_cb(void *data)
518{
519 struct gsm_lchan *lchan = data;
520
Holger Freyther73487a22008-12-31 18:53:57 +0000521 release_loc_updating_req(lchan);
Holger Freythere97f7fb2008-12-31 18:52:11 +0000522 gsm0408_loc_upd_rej(lchan, reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000523 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000524}
525
Holger Freytherb7193e42008-12-29 17:44:08 +0000526static void schedule_reject(struct gsm_lchan *lchan)
527{
Holger Freyther73487a22008-12-31 18:53:57 +0000528 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
529 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000530 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000531}
532
Harald Welte2a139372009-02-22 21:14:55 +0000533static const char *lupd_name(u_int8_t type)
534{
535 switch (type) {
536 case GSM48_LUPD_NORMAL:
537 return "NORMAL";
538 case GSM48_LUPD_PERIODIC:
539 return "PEROIDOC";
540 case GSM48_LUPD_IMSI_ATT:
541 return "IMSI ATTACH";
542 default:
543 return "UNKNOWN";
544 }
545}
546
Harald Welte231ad4f2008-12-27 11:15:38 +0000547#define MI_SIZE 32
Harald Weltebf5e8df2009-02-03 12:59:45 +0000548/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000549static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000550{
Harald Welte8470bf22008-12-25 23:28:35 +0000551 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte702d8702008-12-26 20:25:35 +0000552 struct gsm_bts *bts = msg->trx->bts;
Harald Welte52b1f982008-12-23 20:25:15 +0000553 struct gsm48_loc_upd_req *lu;
554 struct gsm_subscriber *subscr;
Harald Welte255539c2008-12-28 02:26:27 +0000555 struct gsm_lchan *lchan = msg->lchan;
Harald Welte8470bf22008-12-25 23:28:35 +0000556 u_int8_t mi_type;
Harald Welte75a983f2008-12-27 21:34:06 +0000557 u_int32_t tmsi;
Harald Welte231ad4f2008-12-27 11:15:38 +0000558 char mi_string[MI_SIZE];
559 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000560
Harald Welte8470bf22008-12-25 23:28:35 +0000561 lu = (struct gsm48_loc_upd_req *) gh->data;
562
563 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000564
Harald Weltefc977a82008-12-27 10:19:37 +0000565 mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
566
Harald Welte2a139372009-02-22 21:14:55 +0000567 DEBUGP(DMM, "LUPDREQ: mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
568 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000569
570 allocate_loc_updating_req(lchan);
571
Harald Welte52b1f982008-12-23 20:25:15 +0000572 switch (mi_type) {
573 case GSM_MI_TYPE_IMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000574 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000575 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000576 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000577 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000578
Harald Welte52b1f982008-12-23 20:25:15 +0000579 /* look up subscriber based on IMSI */
Harald Welte75a983f2008-12-27 21:34:06 +0000580 subscr = db_create_subscriber(mi_string);
Harald Welte4b634542008-12-27 01:55:51 +0000581 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000582 case GSM_MI_TYPE_TMSI:
Harald Welte231ad4f2008-12-27 11:15:38 +0000583 /* we always want the IMEI, too */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000584 use_lchan(lchan);
Harald Welte015b9ad2009-02-28 18:22:03 +0000585 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000586 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000587
Harald Welte52b1f982008-12-23 20:25:15 +0000588 /* look up the subscriber based on TMSI, request IMSI if it fails */
Harald Welteba4cf162009-01-10 01:49:35 +0000589 subscr = subscr_get_by_tmsi(mi_string);
Harald Welte52b1f982008-12-23 20:25:15 +0000590 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000591 /* send IDENTITY REQUEST message to get IMSI */
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000592 use_lchan(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000593 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000594 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000595 }
596 break;
597 case GSM_MI_TYPE_IMEI:
598 case GSM_MI_TYPE_IMEISV:
599 /* no sim card... FIXME: what to do ? */
Harald Welte2a139372009-02-22 21:14:55 +0000600 DEBUGP(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000601 break;
602 default:
Harald Welte2a139372009-02-22 21:14:55 +0000603 DEBUGP(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000604 break;
605 }
606
Harald Welte255539c2008-12-28 02:26:27 +0000607 lchan->subscr = subscr;
608
Holger Freyther73487a22008-12-31 18:53:57 +0000609 /*
610 * Schedule the reject timer and check if we can let the
611 * subscriber into our network immediately or if we need to wait
612 * for identity responses.
613 */
614 schedule_reject(lchan);
615 if (!authorize_subscriber(lchan->loc_operation, subscr))
Harald Weltee872cb12009-01-01 00:33:37 +0000616 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000617
Harald Welte75a983f2008-12-27 21:34:06 +0000618 db_subscriber_alloc_tmsi(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000619 subscr_update(subscr, bts, GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welte8470bf22008-12-25 23:28:35 +0000620
Harald Welte255539c2008-12-28 02:26:27 +0000621 tmsi = strtoul(subscr->tmsi, NULL, 10);
622
Holger Freyther73487a22008-12-31 18:53:57 +0000623 release_loc_updating_req(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000624 return gsm0408_loc_upd_acc(lchan, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000625}
626
Harald Welte7584aea2009-02-11 11:44:12 +0000627/* 9.1.5 Channel mode modify */
628int gsm48_tx_chan_mode_modify(struct gsm_lchan *lchan, u_int8_t mode)
629{
630 struct msgb *msg = gsm48_msgb_alloc();
631 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
632 struct gsm48_chan_mode_modify *cmm =
633 (struct gsm48_chan_mode_modify *) msgb_put(msg, sizeof(*cmm));
Harald Welte4a543e82009-02-28 13:17:55 +0000634 u_int16_t arfcn = lchan->ts->trx->arfcn & 0x3ff;
Harald Welte7584aea2009-02-11 11:44:12 +0000635
Harald Welte4a543e82009-02-28 13:17:55 +0000636 DEBUGP(DRR, "-> CHANNEL MODE MODIFY mode=0x%02x\n", mode);
Harald Welte7ccf7782009-02-17 01:43:01 +0000637
Harald Welte7584aea2009-02-11 11:44:12 +0000638 msg->lchan = lchan;
639 gh->proto_discr = GSM48_PDISC_RR;
640 gh->msg_type = GSM48_MT_RR_CHAN_MODE_MODIF;
641
642 /* fill the channel information element, this code
643 * should probably be shared with rsl_rx_chan_rqd() */
644 cmm->chan_desc.chan_nr = lchan2chan_nr(lchan);
Harald Welte02b0e092009-02-28 13:11:07 +0000645 cmm->chan_desc.h0.tsc = lchan->ts->trx->bts->tsc;
Harald Welte7584aea2009-02-11 11:44:12 +0000646 cmm->chan_desc.h0.h = 0;
647 cmm->chan_desc.h0.arfcn_high = arfcn >> 8;
648 cmm->chan_desc.h0.arfcn_low = arfcn & 0xff;
649 cmm->mode = mode;
650
651 return gsm48_sendmsg(msg);
652}
653
Harald Weltedb253af2008-12-30 17:56:55 +0000654/* Section 9.2.15a */
655int gsm48_tx_mm_info(struct gsm_lchan *lchan)
656{
657 struct msgb *msg = gsm48_msgb_alloc();
658 struct gsm48_hdr *gh;
659 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000660 u_int8_t *ptr8;
661 u_int16_t *ptr16;
662 int name_len;
Harald Weltedb253af2008-12-30 17:56:55 +0000663 int i;
664
665 msg->lchan = lchan;
666
667 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
668 gh->proto_discr = GSM48_PDISC_MM;
669 gh->msg_type = GSM48_MT_MM_INFO;
670
671 if (net->name_long) {
672 name_len = strlen(net->name_long);
673 /* 10.5.3.5a */
674 ptr8 = msgb_put(msg, 3);
675 ptr8[0] = GSM48_IE_NAME_LONG;
676 ptr8[1] = name_len*2 +1;
677 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
678
679 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
680 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000681 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000682
683 /* FIXME: Use Cell Broadcast, not UCS-2, since
684 * UCS-2 is only supported by later revisions of the spec */
685 }
686
687 if (net->name_short) {
688 name_len = strlen(net->name_short);
689 /* 10.5.3.5a */
690 ptr8 = (u_int8_t *) msgb_put(msg, 3);
691 ptr8[0] = GSM48_IE_NAME_LONG;
692 ptr8[1] = name_len*2 + 1;
693 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
694
Harald Weltee872cb12009-01-01 00:33:37 +0000695 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000696 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000697 ptr16[i] = htons(net->name_short[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000698 }
699
700#if 0
Holger Freytherca362a62009-01-04 21:05:01 +0000701 /* move back to the top */
702 time_t cur_t;
703 struct tm* cur_time;
704 int tz15min;
Harald Weltedb253af2008-12-30 17:56:55 +0000705 /* Section 10.5.3.9 */
706 cur_t = time(NULL);
707 cur_time = gmtime(cur_t);
708 ptr8 = msgb_put(msg, 8);
709 ptr8[0] = GSM48_IE_NET_TIME_TZ;
710 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
711 ptr8[2] = to_bcd8(cur_time->tm_mon);
712 ptr8[3] = to_bcd8(cur_time->tm_mday);
713 ptr8[4] = to_bcd8(cur_time->tm_hour);
714 ptr8[5] = to_bcd8(cur_time->tm_min);
715 ptr8[6] = to_bcd8(cur_time->tm_sec);
716 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
717 tz15min = (cur_time->tm_gmtoff)/(60*15);
718 ptr8[6] = to_bcd8(tz15min);
719 if (tz15min < 0)
720 ptr8[6] |= 0x80;
721#endif
722
723 return gsm48_sendmsg(msg);
724}
725
Harald Welte4b634542008-12-27 01:55:51 +0000726static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
727{
Harald Welte4b634542008-12-27 01:55:51 +0000728 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000729 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000730}
Harald Welteba4cf162009-01-10 01:49:35 +0000731
732/* 9.2.6 CM service reject */
733static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
734 enum gsm48_reject_value value)
735{
736 struct msgb *msg = gsm48_msgb_alloc();
737 struct gsm48_hdr *gh;
738
739 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
740
741 msg->lchan = lchan;
742 use_lchan(lchan);
743
744 gh->proto_discr = GSM48_PDISC_MM;
745 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
746 gh->data[0] = value;
747 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
748
749 return gsm48_sendmsg(msg);
750}
751
Harald Welte4ed0e922009-01-10 03:17:30 +0000752
753/*
754 * Handle CM Service Requests
755 * a) Verify that the packet is long enough to contain the information
756 * we require otherwsie reject with INCORRECT_MESSAGE
757 * b) Try to parse the TMSI. If we do not have one reject
758 * c) Check that we know the subscriber with the TMSI otherwise reject
759 * with a HLR cause
760 * d) Set the subscriber on the gsm_lchan and accept
761 */
Harald Welte4b634542008-12-27 01:55:51 +0000762static int gsm48_rx_mm_serv_req(struct msgb *msg)
763{
Harald Welteba4cf162009-01-10 01:49:35 +0000764 u_int8_t mi_type;
Harald Welte4ed0e922009-01-10 03:17:30 +0000765 char mi_string[MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000766
Harald Welteba4cf162009-01-10 01:49:35 +0000767 struct gsm_subscriber *subscr;
768 struct gsm48_hdr *gh = msgb_l3(msg);
769 struct gsm48_service_request *req =
770 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +0000771 /* unfortunately in Phase1 the classmar2 length is variable */
772 u_int8_t classmark2_len = gh->data[1];
773 u_int8_t *classmark2 = gh->data+2;
774 u_int8_t mi_len = *(classmark2 + classmark2_len);
775 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000776
Harald Weltec9e02182009-05-01 19:07:53 +0000777 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000778 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000779 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000780 return gsm48_tx_mm_serv_rej(msg->lchan,
781 GSM48_REJECT_INCORRECT_MESSAGE);
782 }
783
784 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000785 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +0000786 return gsm48_tx_mm_serv_rej(msg->lchan,
787 GSM48_REJECT_INCORRECT_MESSAGE);
788 }
789
Harald Weltec9e02182009-05-01 19:07:53 +0000790 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000791 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000792 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +0000793 return gsm48_tx_mm_serv_rej(msg->lchan,
794 GSM48_REJECT_INCORRECT_MESSAGE);
795 }
796
Harald Weltec9e02182009-05-01 19:07:53 +0000797 mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte4ed0e922009-01-10 03:17:30 +0000798 subscr = subscr_get_by_tmsi(mi_string);
Harald Weltec9e02182009-05-01 19:07:53 +0000799 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000800 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000801
Harald Welte2a139372009-02-22 21:14:55 +0000802 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000803 if (!subscr)
804 return gsm48_tx_mm_serv_rej(msg->lchan,
805 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
806
807 if (!msg->lchan->subscr)
808 msg->lchan->subscr = subscr;
Harald Welte9bb7c702009-01-10 03:21:41 +0000809 else if (msg->lchan->subscr != subscr) {
810 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
811 subscr_put(subscr);
812 }
813
Harald Welte4b634542008-12-27 01:55:51 +0000814 return gsm48_tx_mm_serv_ack(msg->lchan);
815}
816
Harald Welte2a139372009-02-22 21:14:55 +0000817static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
818{
819 struct gsm48_hdr *gh = msgb_l3(msg);
820 struct gsm48_imsi_detach_ind *idi =
821 (struct gsm48_imsi_detach_ind *) gh->data;
822 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
823 char mi_string[MI_SIZE];
824 struct gsm_subscriber *subscr;
825
826 mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
827 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
828 mi_type, mi_string);
829
830 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 case GSM_MI_TYPE_IMEI:
838 case GSM_MI_TYPE_IMEISV:
839 /* no sim card... FIXME: what to do ? */
840 fprintf(stderr, "Unimplemented mobile identity type\n");
841 break;
842 default:
843 fprintf(stderr, "Unknown mobile identity type\n");
844 break;
845 }
846
Holger Freyther4a49e772009-04-12 05:37:29 +0000847 if (subscr) {
848 subscr_update(subscr, msg->trx->bts,
849 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2a139372009-02-22 21:14:55 +0000850 DEBUGP(DMM, "Subscriber: %s\n",
851 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther4a49e772009-04-12 05:37:29 +0000852 } else
Harald Welte2a139372009-02-22 21:14:55 +0000853 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
854
855 put_lchan(msg->lchan);
856
857 return 0;
858}
859
Harald Weltebf5e8df2009-02-03 12:59:45 +0000860/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000861static int gsm0408_rcv_mm(struct msgb *msg)
862{
863 struct gsm48_hdr *gh = msgb_l3(msg);
864 int rc;
865
866 switch (gh->msg_type & 0xbf) {
867 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Freyther429e7762008-12-30 13:28:30 +0000868 DEBUGP(DMM, "LOCATION UPDATING REQUEST\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000869 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000870 break;
871 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000872 rc = mm_rx_id_resp(msg);
873 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000874 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000875 rc = gsm48_rx_mm_serv_req(msg);
876 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000877 case GSM48_MT_MM_STATUS:
Harald Welte2a139372009-02-22 21:14:55 +0000878 DEBUGP(DMM, "STATUS: FIXME parse error cond.\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000879 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000880 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000881 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
882 msg->lchan->subscr ?
883 msg->lchan->subscr->imsi :
884 "unknown subscriber");
885 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000886 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000887 rc = gsm48_rx_mm_imsi_detach_ind(msg);
888 break;
889 case GSM48_MT_MM_CM_REEST_REQ:
890 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
891 break;
892 case GSM48_MT_MM_AUTH_RESP:
893 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000894 break;
895 default:
896 fprintf(stderr, "Unknown GSM 04.08 MM msg type 0x%02x\n",
897 gh->msg_type);
898 break;
899 }
900
901 return rc;
902}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000903
Harald Welte2d35ae62009-02-06 12:02:13 +0000904/* Receive a PAGING RESPONSE message from the MS */
905static int gsm48_rr_rx_pag_resp(struct msgb *msg)
906{
907 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +0000908 u_int8_t *classmark2_lv = gh->data + 1;
909 u_int8_t *mi_lv = gh->data + 2 + *classmark2_lv;
910 u_int8_t mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Harald Welte2d35ae62009-02-06 12:02:13 +0000911 char mi_string[MI_SIZE];
912 struct gsm_subscriber *subscr;
Harald Welte595ad7b2009-02-16 22:05:44 +0000913 struct paging_signal_data sig_data;
Harald Welte2d35ae62009-02-06 12:02:13 +0000914 int rc = 0;
915
Harald Welte61548982009-02-22 21:26:29 +0000916 mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, *mi_lv);
Harald Welte2d35ae62009-02-06 12:02:13 +0000917 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
918 mi_type, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000919 switch (mi_type) {
920 case GSM_MI_TYPE_TMSI:
921 subscr = subscr_get_by_tmsi(mi_string);
922 break;
923 case GSM_MI_TYPE_IMSI:
924 subscr = subscr_get_by_imsi(mi_string);
925 break;
926 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000927
928 if (!subscr) {
929 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000930 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000931 return -EINVAL;
932 }
933 DEBUGP(DRR, "<- Channel was requested by %s\n",
934 subscr->name ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000935
Holger Freyther2fa4cb52009-02-14 23:53:15 +0000936 if (!msg->lchan->subscr)
937 msg->lchan->subscr = subscr;
938 else if (msg->lchan->subscr != subscr) {
939 DEBUGP(DRR, "<- Channel already owned by someone else?\n");
940 subscr_put(subscr);
941 }
942
Harald Welte595ad7b2009-02-16 22:05:44 +0000943 sig_data.subscr = subscr;
944 sig_data.bts = msg->lchan->ts->trx->bts;
945 sig_data.lchan = msg->lchan;
946
947 dispatch_signal(SS_PAGING, S_PAGING_COMPLETED, &sig_data);
Harald Welte7ccf7782009-02-17 01:43:01 +0000948 paging_request_stop(msg->trx->bts, subscr, msg->lchan);
Harald Welte2d35ae62009-02-06 12:02:13 +0000949
Harald Welte7584aea2009-02-11 11:44:12 +0000950 /* FIXME: somehow signal the completion of the PAGING to
951 * the entity that requested the paging */
952
Harald Welte2d35ae62009-02-06 12:02:13 +0000953 return rc;
954}
955
Harald Weltecf5b3592009-05-01 18:28:42 +0000956static int gsm48_rx_rr_status(struct msgb *msg)
957{
958 struct gsm48_hdr *gh = msgb_l3(msg);
959
960 DEBUGP(DRR, "STATUS rr_cause = %s\n",
961 rr_cause_name(gh->data[0]));
962
963 return 0;
964}
965
Harald Weltebf5e8df2009-02-03 12:59:45 +0000966/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000967static int gsm0408_rcv_rr(struct msgb *msg)
968{
969 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +0000970 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000971
972 switch (gh->msg_type) {
973 case GSM48_MT_RR_CLSM_CHG:
974 DEBUGP(DRR, "CLASSMARK CHANGE\n");
975 /* FIXME: what to do ?!? */
976 break;
Harald Weltefc977a82008-12-27 10:19:37 +0000977 case GSM48_MT_RR_GPRS_SUSP_REQ:
978 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
979 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000980 case GSM48_MT_RR_PAG_RESP:
Harald Welte2d35ae62009-02-06 12:02:13 +0000981 rc = gsm48_rr_rx_pag_resp(msg);
982 break;
Harald Welte7ccf7782009-02-17 01:43:01 +0000983 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
984 DEBUGP(DRR, "CHANNEL MODE MODIFY ACK\n");
Harald Welte2c38aa82009-02-18 03:44:24 +0000985 rc = rsl_chan_mode_modify_req(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +0000986 break;
Harald Weltecf5b3592009-05-01 18:28:42 +0000987 case GSM48_MT_RR_STATUS:
988 rc = gsm48_rx_rr_status(msg);
989 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000990 default:
Harald Welte2d35ae62009-02-06 12:02:13 +0000991 fprintf(stderr, "Unimplemented GSM 04.08 RR msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000992 gh->msg_type);
993 break;
994 }
995
Harald Welte2d35ae62009-02-06 12:02:13 +0000996 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000997}
998
Holger Freythere64a7a32009-02-06 21:55:37 +0000999/* 7.1.7 and 9.1.7 Channel release*/
1000int gsm48_send_rr_release(struct gsm_lchan *lchan)
1001{
1002 struct msgb *msg = gsm48_msgb_alloc();
1003 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1004 u_int8_t *cause;
1005
1006 msg->lchan = lchan;
1007 gh->proto_discr = GSM48_PDISC_RR;
1008 gh->msg_type = GSM48_MT_RR_CHAN_REL;
1009
1010 cause = msgb_put(msg, 1);
1011 cause[0] = GSM48_RR_CAUSE_NORMAL;
1012
1013 DEBUGP(DRR, "Sending Channel Release: Chan: Number: %d Type: %d\n",
1014 lchan->nr, lchan->type);
1015
1016 return gsm48_sendmsg(msg);
1017}
1018
Harald Welte4bc90a12008-12-27 16:32:52 +00001019/* Call Control */
1020
Harald Welte7584aea2009-02-11 11:44:12 +00001021/* The entire call control code is written in accordance with Figure 7.10c
1022 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1023 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1024 * it for voice */
1025
Harald Welte4bc90a12008-12-27 16:32:52 +00001026static int gsm48_cc_tx_status(struct gsm_lchan *lchan)
1027{
1028 struct msgb *msg = gsm48_msgb_alloc();
1029 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1030 u_int8_t *cause, *call_state;
1031
Harald Welte65e74cc2008-12-29 01:55:35 +00001032 gh->proto_discr = GSM48_PDISC_CC;
1033
Harald Welte4bc90a12008-12-27 16:32:52 +00001034 msg->lchan = lchan;
1035
Harald Welte4bc90a12008-12-27 16:32:52 +00001036 gh->msg_type = GSM48_MT_CC_STATUS;
1037
1038 cause = msgb_put(msg, 3);
1039 cause[0] = 2;
1040 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1041 cause[2] = 0x80 | 30; /* response to status inquiry */
1042
1043 call_state = msgb_put(msg, 1);
1044 call_state[0] = 0xc0 | 0x00;
1045
Harald Welte65e74cc2008-12-29 01:55:35 +00001046 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001047}
1048
Harald Welte6f4b7532008-12-29 00:39:37 +00001049static int gsm48_tx_simple(struct gsm_lchan *lchan,
1050 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001051{
1052 struct msgb *msg = gsm48_msgb_alloc();
1053 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1054
1055 msg->lchan = lchan;
1056
Harald Welte6f4b7532008-12-29 00:39:37 +00001057 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001058 gh->msg_type = msg_type;
1059
Harald Welte65e74cc2008-12-29 01:55:35 +00001060 return gsm48_sendmsg(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001061}
1062
Harald Welte09e38af2009-02-16 22:52:23 +00001063/* call-back from paging the B-end of the connection */
1064static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001065 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001066{
Harald Welte7ccf7782009-02-17 01:43:01 +00001067 struct gsm_lchan *lchan = _lchan;
Harald Welte49f48b82009-02-17 15:29:33 +00001068 struct gsm_call *remote_call = param;
1069 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001070 int rc = 0;
Harald Welte09e38af2009-02-16 22:52:23 +00001071
1072 if (hooknum != GSM_HOOK_RR_PAGING)
1073 return -EINVAL;
1074
1075 switch (event) {
1076 case GSM_PAGING_SUCCEEDED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001077 DEBUGP(DCC, "paging succeeded!\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001078 remote_call->remote_lchan = lchan;
1079 call->remote_lchan = remote_call->local_lchan;
Harald Welte09e38af2009-02-16 22:52:23 +00001080 /* send SETUP request to called party */
Harald Welte49f48b82009-02-17 15:29:33 +00001081 rc = gsm48_cc_tx_setup(lchan, call->remote_lchan->subscr);
1082 if (is_ipaccess_bts(lchan->ts->trx->bts))
1083 rsl_ipacc_bind(lchan);
Harald Welte09e38af2009-02-16 22:52:23 +00001084 break;
1085 case GSM_PAGING_EXPIRED:
Harald Welte7ccf7782009-02-17 01:43:01 +00001086 DEBUGP(DCC, "paging expired!\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001087 /* notify caller that we cannot reach called party */
Harald Welte7ccf7782009-02-17 01:43:01 +00001088 /* FIXME: correct cause, etc */
Harald Weltebfaf0972009-02-17 17:43:33 +00001089 rc = gsm48_tx_simple(remote_call->local_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001090 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte09e38af2009-02-16 22:52:23 +00001091 break;
1092 }
Harald Welte7ccf7782009-02-17 01:43:01 +00001093 return rc;
Harald Welte09e38af2009-02-16 22:52:23 +00001094}
1095
Harald Welte4bc90a12008-12-27 16:32:52 +00001096static int gsm48_cc_rx_status_enq(struct msgb *msg)
1097{
Harald Welte4a543e82009-02-28 13:17:55 +00001098 DEBUGP(DCC, "-> STATUS ENQ\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001099 return gsm48_cc_tx_status(msg->lchan);
1100}
1101
1102static int gsm48_cc_rx_setup(struct msgb *msg)
1103{
Harald Welte7584aea2009-02-11 11:44:12 +00001104 struct gsm_call *call = &msg->lchan->call;
1105 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte09e38af2009-02-16 22:52:23 +00001106 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Harald Welte7584aea2009-02-11 11:44:12 +00001107 struct gsm_subscriber *called_subscr;
Harald Welte09e38af2009-02-16 22:52:23 +00001108 char called_number[(43-2)*2 + 1] = "\0";
1109 struct tlv_parsed tp;
1110 u_int8_t num_type;
Harald Welte7584aea2009-02-11 11:44:12 +00001111 int ret;
1112
1113 if (call->state == GSM_CSTATE_NULL ||
1114 call->state == GSM_CSTATE_RELEASE_REQ)
1115 use_lchan(msg->lchan);
1116
1117 call->type = GSM_CT_MO;
1118 call->state = GSM_CSTATE_INITIATED;
Harald Welte49f48b82009-02-17 15:29:33 +00001119 call->local_lchan = msg->lchan;
Harald Welte7584aea2009-02-11 11:44:12 +00001120 call->transaction_id = gh->proto_discr & 0xf0;
1121
Harald Welte09e38af2009-02-16 22:52:23 +00001122 tlv_parse(&tp, &rsl_att_tlvdef, gh->data, payload_len);
1123 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD))
1124 goto err;
Harald Welte7584aea2009-02-11 11:44:12 +00001125
Harald Welte09e38af2009-02-16 22:52:23 +00001126 /* Parse the number that was dialed and lookup subscriber */
1127 num_type = decode_bcd_number(called_number, sizeof(called_number),
1128 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
Harald Welte7ccf7782009-02-17 01:43:01 +00001129
Harald Welte4b458152009-02-17 13:23:06 +00001130 DEBUGP(DCC, "A -> SETUP(tid=0x%02x,number='%s')\n", call->transaction_id,
Harald Welte7ccf7782009-02-17 01:43:01 +00001131 called_number);
1132
Harald Welte09e38af2009-02-16 22:52:23 +00001133 called_subscr = subscr_get_by_extension(called_number);
Harald Welte7584aea2009-02-11 11:44:12 +00001134 if (!called_subscr) {
1135 DEBUGP(DCC, "could not find subscriber, RELEASE\n");
1136 put_lchan(msg->lchan);
1137 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001138 GSM48_MT_CC_RELEASE_COMPL);
Harald Welte7584aea2009-02-11 11:44:12 +00001139 }
Harald Welte5a065df2009-02-22 21:13:18 +00001140 if (called_subscr == msg->lchan->subscr) {
1141 DEBUGP(DCC, "subscriber calling himself ?!?\n");
1142 put_lchan(msg->lchan);
1143 subscr_put(called_subscr);
1144 return gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1145 GSM48_MT_CC_RELEASE_COMPL);
1146 }
Harald Welte7584aea2009-02-11 11:44:12 +00001147
Harald Welte09e38af2009-02-16 22:52:23 +00001148 subscr_get(msg->lchan->subscr);
Harald Welte09e38af2009-02-16 22:52:23 +00001149 call->called_subscr = called_subscr;
1150
Harald Welte7584aea2009-02-11 11:44:12 +00001151 /* start paging of the receiving end of the call */
Harald Weltee7b452a2009-05-23 06:34:37 +00001152 /* FIXME: we're assuming that the receiver is at the same BTS
1153 * than we are, which is obviously a wrong assumption in multi-BTS
1154 * case */
Harald Welte09e38af2009-02-16 22:52:23 +00001155 paging_request(msg->trx->bts, called_subscr, RSL_CHANNEED_TCH_F,
1156 setup_trig_pag_evt, call);
Harald Welte7584aea2009-02-11 11:44:12 +00001157
1158 /* send a CALL PROCEEDING message to the MO */
1159 ret = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1160 GSM48_MT_CC_CALL_PROC);
1161
Harald Welte49f48b82009-02-17 15:29:33 +00001162 if (is_ipaccess_bts(msg->trx->bts))
1163 rsl_ipacc_bind(msg->lchan);
1164
Harald Welte7584aea2009-02-11 11:44:12 +00001165 /* change TCH/F mode to voice */
Harald Welte2c38aa82009-02-18 03:44:24 +00001166 return gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte09e38af2009-02-16 22:52:23 +00001167
1168err:
1169 /* FIXME: send some kind of RELEASE */
1170 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001171}
Harald Welte7584aea2009-02-11 11:44:12 +00001172
Harald Welte7ccf7782009-02-17 01:43:01 +00001173static int gsm48_cc_rx_alerting(struct msgb *msg)
1174{
1175 struct gsm_call *call = &msg->lchan->call;
Harald Welte4bc90a12008-12-27 16:32:52 +00001176
Harald Welte4b458152009-02-17 13:23:06 +00001177 DEBUGP(DCC, "A -> ALERTING\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001178
1179 /* forward ALERTING to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001180 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001181 return -EIO;
1182
Harald Welte4b458152009-02-17 13:23:06 +00001183 DEBUGP(DCC, "B <- ALERTING\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001184 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001185 GSM48_MT_CC_ALERTING);
1186}
1187
Harald Welte49f48b82009-02-17 15:29:33 +00001188/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001189static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001190{
Harald Welte11fa29c2009-02-19 17:24:39 +00001191 struct gsm_bts *bts = lchan->ts->trx->bts;
1192 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte49f48b82009-02-17 15:29:33 +00001193 struct gsm_bts_trx_ts *ts;
1194
Harald Welte11fa29c2009-02-19 17:24:39 +00001195 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1196 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1197 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1198
1199 if (bts->type != remote_bts->type) {
1200 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1201 return -EINVAL;
1202 }
Harald Welte49f48b82009-02-17 15:29:33 +00001203
Harald Welte11fa29c2009-02-19 17:24:39 +00001204 switch (bts->type) {
1205 case GSM_BTS_TYPE_NANOBTS_900:
1206 case GSM_BTS_TYPE_NANOBTS_1800:
1207 ts = remote_lchan->ts;
1208 rsl_ipacc_connect(lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1209 lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1210
1211 ts = lchan->ts;
1212 rsl_ipacc_connect(remote_lchan, ts->abis_ip.bound_ip, ts->abis_ip.bound_port,
1213 remote_lchan->ts->abis_ip.attr_f8, ts->abis_ip.attr_fc);
1214 break;
1215 case GSM_BTS_TYPE_BS11:
1216 trau_mux_map_lchan(lchan, remote_lchan);
1217 break;
1218 default:
1219 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1220 break;
1221 }
Harald Welte49f48b82009-02-17 15:29:33 +00001222
1223 return 0;
1224}
1225
Harald Welte7ccf7782009-02-17 01:43:01 +00001226static int gsm48_cc_rx_connect(struct msgb *msg)
1227{
1228 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001229 int rc;
1230
Harald Welte4b458152009-02-17 13:23:06 +00001231 DEBUGP(DCC, "A -> CONNECT\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001232
1233 rc = tch_map(msg->lchan, call->remote_lchan);
1234 if (rc)
1235 return -EIO;
1236
1237 if (!call->remote_lchan)
1238 return -EIO;
1239
Harald Welte4b458152009-02-17 13:23:06 +00001240 DEBUGP(DCC, "A <- CONNECT ACK\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001241 /* MT+MO: need to respond with CONNECT_ACK and pass on */
1242 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001243 GSM48_MT_CC_CONNECT_ACK);
Harald Welte7ccf7782009-02-17 01:43:01 +00001244
Harald Welte49f48b82009-02-17 15:29:33 +00001245
1246 /* forward CONNECT to other party */
Harald Welte4b458152009-02-17 13:23:06 +00001247 DEBUGP(DCC, "B <- CONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001248 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte4b458152009-02-17 13:23:06 +00001249 GSM48_MT_CC_CONNECT);
Harald Welte7ccf7782009-02-17 01:43:01 +00001250}
1251
1252static int gsm48_cc_rx_disconnect(struct msgb *msg)
1253{
1254 struct gsm_call *call = &msg->lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001255 int rc;
1256
1257
1258 /* Section 5.4.3.2 */
Harald Welte4b458152009-02-17 13:23:06 +00001259 DEBUGP(DCC, "A -> DISCONNECT (state->RELEASE_REQ)\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001260 call->state = GSM_CSTATE_RELEASE_REQ;
Harald Welte7ccf7782009-02-17 01:43:01 +00001261 /* FIXME: clear the network connection */
Harald Welte4b458152009-02-17 13:23:06 +00001262 DEBUGP(DCC, "A <- RELEASE\n");
Harald Welte7ccf7782009-02-17 01:43:01 +00001263 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1264 GSM48_MT_CC_RELEASE);
Holger Freythera1f92f02009-04-12 05:37:52 +00001265
1266 /*
1267 * FIXME: This looks wrong! We should have a single
1268 * place to do MMCC-REL-CNF/-REQ/-IND and then switch
1269 * to the NULL state and relase the call
1270 */
1271 subscr_put_channel(msg->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001272
1273 /* forward DISCONNECT to other party */
Harald Welte49f48b82009-02-17 15:29:33 +00001274 if (!call->remote_lchan)
Harald Welte7ccf7782009-02-17 01:43:01 +00001275 return -EIO;
1276
Harald Welte4b458152009-02-17 13:23:06 +00001277 DEBUGP(DCC, "B <- DISCONNECT\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001278 return gsm48_tx_simple(call->remote_lchan, GSM48_PDISC_CC,
Harald Welte7ccf7782009-02-17 01:43:01 +00001279 GSM48_MT_CC_DISCONNECT);
1280}
1281
1282static const u_int8_t calling_bcd[] = { 0xb9, 0x32, 0x24 };
1283
Harald Welte49f48b82009-02-17 15:29:33 +00001284int gsm48_cc_tx_setup(struct gsm_lchan *lchan,
1285 struct gsm_subscriber *calling_subscr)
Harald Welte65e74cc2008-12-29 01:55:35 +00001286{
1287 struct msgb *msg = gsm48_msgb_alloc();
1288 struct gsm48_hdr *gh;
1289 struct gsm_call *call = &lchan->call;
Harald Welte7ccf7782009-02-17 01:43:01 +00001290 u_int8_t bcd_lv[19];
Harald Welte65e74cc2008-12-29 01:55:35 +00001291
Harald Welte7ccf7782009-02-17 01:43:01 +00001292 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001293
1294 call->type = GSM_CT_MT;
Harald Welte49f48b82009-02-17 15:29:33 +00001295
1296 call->local_lchan = msg->lchan = lchan;
Harald Welte498b0bb2009-01-09 21:27:43 +00001297 use_lchan(lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +00001298
1299 gh->proto_discr = GSM48_PDISC_CC;
1300 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001301
Harald Welte7584aea2009-02-11 11:44:12 +00001302 msgb_tv_put(msg, GSM48_IE_SIGNAL, GSM48_SIGNAL_DIALTONE);
Harald Welte49f48b82009-02-17 15:29:33 +00001303 if (calling_subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001304 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001305 calling_subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001306 msgb_tlv_put(msg, GSM48_IE_CALLING_BCD,
1307 bcd_lv[0], bcd_lv+1);
1308 }
Harald Welte49f48b82009-02-17 15:29:33 +00001309 if (lchan->subscr) {
Harald Welte7ccf7782009-02-17 01:43:01 +00001310 encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0xb9,
Harald Welte49f48b82009-02-17 15:29:33 +00001311 lchan->subscr->extension);
Harald Welte7ccf7782009-02-17 01:43:01 +00001312 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD,
1313 bcd_lv[0], bcd_lv+1);
1314 }
Harald Welte65e74cc2008-12-29 01:55:35 +00001315
Harald Welte4b458152009-02-17 13:23:06 +00001316 DEBUGP(DCC, "B <- SETUP\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001317
1318 return gsm48_sendmsg(msg);
1319}
1320
Harald Welte4bc90a12008-12-27 16:32:52 +00001321static int gsm0408_rcv_cc(struct msgb *msg)
1322{
1323 struct gsm48_hdr *gh = msgb_l3(msg);
1324 u_int8_t msg_type = gh->msg_type & 0xbf;
1325 struct gsm_call *call = &msg->lchan->call;
1326 int rc = 0;
1327
1328 switch (msg_type) {
1329 case GSM48_MT_CC_CALL_CONF:
1330 /* Response to SETUP */
Harald Welte7ccf7782009-02-17 01:43:01 +00001331 DEBUGP(DCC, "-> CALL CONFIRM\n");
Harald Welte4b458152009-02-17 13:23:06 +00001332 /* we now need to MODIFY the channel */
Harald Welte2c38aa82009-02-18 03:44:24 +00001333 rc = gsm48_tx_chan_mode_modify(msg->lchan, GSM48_CMODE_SPEECH_EFR);
Harald Welte4bc90a12008-12-27 16:32:52 +00001334 break;
1335 case GSM48_MT_CC_RELEASE_COMPL:
1336 /* Answer from MS to RELEASE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001337 DEBUGP(DCC, "-> RELEASE COMPLETE (state->NULL)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001338 call->state = GSM_CSTATE_NULL;
1339 break;
1340 case GSM48_MT_CC_ALERTING:
Harald Welte7ccf7782009-02-17 01:43:01 +00001341 rc = gsm48_cc_rx_alerting(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001342 break;
1343 case GSM48_MT_CC_CONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001344 rc = gsm48_cc_rx_connect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001345 break;
1346 case GSM48_MT_CC_CONNECT_ACK:
1347 /* MO: Answer to CONNECT */
1348 call->state = GSM_CSTATE_ACTIVE;
Harald Welte7ccf7782009-02-17 01:43:01 +00001349 DEBUGP(DCC, "-> CONNECT_ACK (state->ACTIVE)\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001350 break;
1351 case GSM48_MT_CC_RELEASE:
Harald Welte7ccf7782009-02-17 01:43:01 +00001352 DEBUGP(DCC, "-> RELEASE\n");
Harald Welte49f48b82009-02-17 15:29:33 +00001353 DEBUGP(DCC, "<- RELEASE_COMPLETE\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001354 /* need to respond with RELEASE_COMPLETE */
Harald Welte7ccf7782009-02-17 01:43:01 +00001355 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1356 GSM48_MT_CC_RELEASE_COMPL);
Holger Freythera1f92f02009-04-12 05:37:52 +00001357 subscr_put_channel(msg->lchan);
Harald Welte49f48b82009-02-17 15:29:33 +00001358 call->state = GSM_CSTATE_NULL;
Harald Welte4bc90a12008-12-27 16:32:52 +00001359 break;
1360 case GSM48_MT_CC_STATUS_ENQ:
1361 rc = gsm48_cc_rx_status_enq(msg);
1362 break;
1363 case GSM48_MT_CC_DISCONNECT:
Harald Welte7ccf7782009-02-17 01:43:01 +00001364 rc = gsm48_cc_rx_disconnect(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001365 break;
1366 case GSM48_MT_CC_SETUP:
Harald Welte4a543e82009-02-28 13:17:55 +00001367 /* MO: wants to establish a call */
Harald Welte7584aea2009-02-11 11:44:12 +00001368 rc = gsm48_cc_rx_setup(msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00001369 break;
1370 case GSM48_MT_CC_EMERG_SETUP:
Harald Welte7ccf7782009-02-17 01:43:01 +00001371 DEBUGP(DCC, "-> EMERGENCY SETUP\n");
Harald Welte4bc90a12008-12-27 16:32:52 +00001372 /* FIXME: continue with CALL_PROCEEDING, ALERTING, CONNECT, RELEASE_COMPLETE */
1373 break;
Harald Welte4a543e82009-02-28 13:17:55 +00001374 case GSM48_MT_CC_HOLD:
1375 DEBUGP(DCC, "-> HOLD (rejecting)\n");
1376 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1377 GSM48_MT_CC_HOLD_REJ);
1378 break;
1379 case GSM48_MT_CC_RETR:
1380 DEBUGP(DCC, "-> RETR (rejecting)\n");
1381 rc = gsm48_tx_simple(msg->lchan, GSM48_PDISC_CC,
1382 GSM48_MT_CC_RETR_REJ);
1383 break;
Harald Welte4bc90a12008-12-27 16:32:52 +00001384 default:
Harald Welte2d35ae62009-02-06 12:02:13 +00001385 fprintf(stderr, "Unimplemented GSM 04.08 CC msg type 0x%02x\n",
Harald Welte4bc90a12008-12-27 16:32:52 +00001386 msg_type);
1387 break;
1388 }
1389
1390 return rc;
1391}
1392
Harald Welte52b1f982008-12-23 20:25:15 +00001393/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
1394int gsm0408_rcvmsg(struct msgb *msg)
1395{
1396 struct gsm48_hdr *gh = msgb_l3(msg);
1397 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00001398 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001399
1400 switch (pdisc) {
1401 case GSM48_PDISC_CC:
1402 rc = gsm0408_rcv_cc(msg);
1403 break;
1404 case GSM48_PDISC_MM:
1405 rc = gsm0408_rcv_mm(msg);
1406 break;
1407 case GSM48_PDISC_RR:
1408 rc = gsm0408_rcv_rr(msg);
1409 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001410 case GSM48_PDISC_SMS:
Daniel Willmann8b3390e2008-12-28 00:31:09 +00001411 rc = gsm0411_rcv_sms(msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001412 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001413 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001414 case GSM48_PDISC_SM_GPRS:
Harald Welte52b1f982008-12-23 20:25:15 +00001415 fprintf(stderr, "Unimplemented GSM 04.08 discriminator 0x%02d\n",
1416 pdisc);
1417 break;
1418 default:
1419 fprintf(stderr, "Unknown GSM 04.08 discriminator 0x%02d\n",
1420 pdisc);
1421 break;
1422 }
1423
1424 return rc;
1425}
Harald Welte8470bf22008-12-25 23:28:35 +00001426
1427enum chreq_type {
1428 CHREQ_T_EMERG_CALL,
1429 CHREQ_T_CALL_REEST_TCH_F,
1430 CHREQ_T_CALL_REEST_TCH_H,
1431 CHREQ_T_CALL_REEST_TCH_H_DBL,
1432 CHREQ_T_SDCCH,
1433 CHREQ_T_TCH_F,
1434 CHREQ_T_VOICE_CALL_TCH_H,
1435 CHREQ_T_DATA_CALL_TCH_H,
1436 CHREQ_T_LOCATION_UPD,
1437 CHREQ_T_PAG_R_ANY,
1438 CHREQ_T_PAG_R_TCH_F,
1439 CHREQ_T_PAG_R_TCH_FH,
1440};
1441
1442/* Section 9.1.8 / Table 9.9 */
1443struct chreq {
1444 u_int8_t val;
1445 u_int8_t mask;
1446 enum chreq_type type;
1447};
1448
1449/* If SYSTEM INFORMATION TYPE 4 NECI bit == 1 */
1450static const struct chreq chreq_type_neci1[] = {
1451 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1452 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_F },
1453 { 0x68, 0xfc, CHREQ_T_CALL_REEST_TCH_H },
1454 { 0x6c, 0xfc, CHREQ_T_CALL_REEST_TCH_H_DBL },
1455 { 0xe0, 0xe0, CHREQ_T_SDCCH },
1456 { 0x40, 0xf0, CHREQ_T_VOICE_CALL_TCH_H },
1457 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1458 { 0x00, 0xf0, CHREQ_T_LOCATION_UPD },
1459 { 0x10, 0xf0, CHREQ_T_SDCCH },
1460 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1461 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1462 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1463};
1464
1465/* If SYSTEM INFORMATION TYPE 4 NECI bit == 0 */
1466static const struct chreq chreq_type_neci0[] = {
1467 { 0xa0, 0xe0, CHREQ_T_EMERG_CALL },
1468 { 0xc0, 0xe0, CHREQ_T_CALL_REEST_TCH_H },
1469 { 0xe0, 0xe0, CHREQ_T_TCH_F },
1470 { 0x50, 0xf0, CHREQ_T_DATA_CALL_TCH_H },
1471 { 0x00, 0xe0, CHREQ_T_LOCATION_UPD },
1472 { 0x80, 0xe0, CHREQ_T_PAG_R_ANY },
1473 { 0x20, 0xf0, CHREQ_T_PAG_R_TCH_F },
1474 { 0x30, 0xf0, CHREQ_T_PAG_R_TCH_FH },
1475};
1476
1477static const enum gsm_chan_t ctype_by_chreq[] = {
1478 [CHREQ_T_EMERG_CALL] = GSM_LCHAN_TCH_F,
1479 [CHREQ_T_CALL_REEST_TCH_F] = GSM_LCHAN_TCH_F,
1480 [CHREQ_T_CALL_REEST_TCH_H] = GSM_LCHAN_TCH_H,
1481 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_LCHAN_TCH_H,
1482 [CHREQ_T_SDCCH] = GSM_LCHAN_SDCCH,
1483 [CHREQ_T_TCH_F] = GSM_LCHAN_TCH_F,
1484 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1485 [CHREQ_T_DATA_CALL_TCH_H] = GSM_LCHAN_TCH_H,
1486 [CHREQ_T_LOCATION_UPD] = GSM_LCHAN_SDCCH,
1487 [CHREQ_T_PAG_R_ANY] = GSM_LCHAN_SDCCH,
1488 [CHREQ_T_PAG_R_TCH_F] = GSM_LCHAN_TCH_F,
1489 [CHREQ_T_PAG_R_TCH_FH] = GSM_LCHAN_TCH_F,
1490};
1491
Harald Weltee14a57c2008-12-29 04:08:28 +00001492static const enum gsm_chreq_reason_t reason_by_chreq[] = {
1493 [CHREQ_T_EMERG_CALL] = GSM_CHREQ_REASON_EMERG,
1494 [CHREQ_T_CALL_REEST_TCH_F] = GSM_CHREQ_REASON_CALL,
1495 [CHREQ_T_CALL_REEST_TCH_H] = GSM_CHREQ_REASON_CALL,
1496 [CHREQ_T_CALL_REEST_TCH_H_DBL] = GSM_CHREQ_REASON_CALL,
1497 [CHREQ_T_SDCCH] = GSM_CHREQ_REASON_OTHER,
1498 [CHREQ_T_TCH_F] = GSM_CHREQ_REASON_OTHER,
1499 [CHREQ_T_VOICE_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1500 [CHREQ_T_DATA_CALL_TCH_H] = GSM_CHREQ_REASON_OTHER,
1501 [CHREQ_T_LOCATION_UPD] = GSM_CHREQ_REASON_LOCATION_UPD,
1502 [CHREQ_T_PAG_R_ANY] = GSM_CHREQ_REASON_PAG,
1503 [CHREQ_T_PAG_R_TCH_F] = GSM_CHREQ_REASON_PAG,
1504 [CHREQ_T_PAG_R_TCH_FH] = GSM_CHREQ_REASON_PAG,
1505};
1506
Harald Welte8470bf22008-12-25 23:28:35 +00001507enum gsm_chan_t get_ctype_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1508{
1509 int i;
1510 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1511
1512 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1513 const struct chreq *chr = &chreq_type_neci0[i];
1514 if ((ra & chr->mask) == chr->val)
1515 return ctype_by_chreq[chr->type];
1516 }
1517 fprintf(stderr, "Unknown CHANNEL REQUEST RQD 0x%02x\n", ra);
1518 return GSM_LCHAN_SDCCH;
1519}
Harald Weltee14a57c2008-12-29 04:08:28 +00001520
1521enum gsm_chreq_reason_t get_reason_by_chreq(struct gsm_bts *bts, u_int8_t ra)
1522{
1523 int i;
1524 /* FIXME: determine if we set NECI = 0 in the BTS SI4 */
1525
1526 for (i = 0; i < ARRAY_SIZE(chreq_type_neci0); i++) {
1527 const struct chreq *chr = &chreq_type_neci0[i];
1528 if ((ra & chr->mask) == chr->val)
1529 return reason_by_chreq[chr->type];
1530 }
1531 fprintf(stderr, "Unknown CHANNEL REQUEST REASON 0x%02x\n", ra);
1532 return GSM_CHREQ_REASON_OTHER;
1533}