blob: a41420c18a7748c49fd01a625b5f8e62604c4a5e [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 Weltedfe6c7d2010-02-20 16:24:02 +010034#include <osmocore/msgb.h>
35#include <osmocore/bitvec.h>
36#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000037#include <openbsc/debug.h>
38#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010039#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000041#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/gsm_04_08.h>
43#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000044#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000045#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000046#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000047#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000048#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020049#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010050#include <osmocore/talloc.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020051#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020052#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010053#include <openbsc/silent_call.h>
Harald Welte52b1f982008-12-23 20:25:15 +000054
Harald Welte0c389302009-06-10 12:08:54 +080055#define GSM_MAX_FACILITY 128
56#define GSM_MAX_SSVERSION 128
57#define GSM_MAX_USERUSER 128
58
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020059void *tall_locop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020060
Holger Freytherd51524f2009-06-09 08:27:07 +000061int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000062static int gsm48_tx_simple(struct gsm_lchan *lchan,
63 u_int8_t pdisc, u_int8_t msg_type);
Holger Freytherb7193e42008-12-29 17:44:08 +000064static void schedule_reject(struct gsm_lchan *lchan);
Harald Welte65e74cc2008-12-29 01:55:35 +000065
Harald Welte52b1f982008-12-23 20:25:15 +000066struct gsm_lai {
67 u_int16_t mcc;
68 u_int16_t mnc;
69 u_int16_t lac;
70};
71
Harald Welte4bfdfe72009-06-10 23:11:52 +080072static u_int32_t new_callref = 0x80000001;
73
Holger Freyther73487a22008-12-31 18:53:57 +000074static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
75 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +000076{
77 if (!subscriber)
78 return 0;
79
Holger Freyther73487a22008-12-31 18:53:57 +000080 /*
81 * Do not send accept yet as more information should arrive. Some
82 * phones will not send us the information and we will have to check
83 * what we want to do with that.
84 */
85 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
86 return 0;
87
Jan Luebbe06513f22009-08-12 12:48:00 +020088 switch (subscriber->net->auth_policy) {
89 case GSM_AUTH_POLICY_CLOSED:
90 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +020091 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +020092 if (subscriber->authorized)
93 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +020094 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +020095 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +000096 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +020097 default:
98 return 0;
99 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000100}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000101
Holger Freyther73487a22008-12-31 18:53:57 +0000102static void release_loc_updating_req(struct gsm_lchan *lchan)
103{
Harald Welte179f0642008-12-31 23:59:18 +0000104 if (!lchan->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000105 return;
106
Harald Welteff117a82009-05-23 05:22:08 +0000107 bsc_del_timer(&lchan->loc_operation->updating_timer);
Harald Welte2cf161b2009-06-20 22:36:41 +0200108 talloc_free(lchan->loc_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000109 lchan->loc_operation = 0;
Holger Freyther3eaa7922009-01-01 02:59:03 +0000110 put_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000111}
112
113static void allocate_loc_updating_req(struct gsm_lchan *lchan)
114{
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000115 use_lchan(lchan);
Holger Freyther73487a22008-12-31 18:53:57 +0000116 release_loc_updating_req(lchan);
117
Harald Welte470ec292009-06-26 20:25:23 +0200118 lchan->loc_operation = talloc_zero(tall_locop_ctx,
119 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000120}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000121
Holger Freytherd51524f2009-06-09 08:27:07 +0000122static int gsm0408_authorize(struct gsm_lchan *lchan, struct msgb *msg)
123{
Holger Freytherd51524f2009-06-09 08:27:07 +0000124 if (authorize_subscriber(lchan->loc_operation, lchan->subscr)) {
Harald Welteee5ad162009-08-09 19:07:00 +0200125 int rc;
126
Holger Freytherd51524f2009-06-09 08:27:07 +0000127 db_subscriber_alloc_tmsi(lchan->subscr);
Holger Freytherd51524f2009-06-09 08:27:07 +0000128 release_loc_updating_req(lchan);
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200129 rc = gsm0408_loc_upd_acc(msg->lchan, lchan->subscr->tmsi);
Harald Welte648b6ce2009-12-14 09:00:24 +0100130 if (lchan->ts->trx->bts->network->send_mm_info) {
131 /* send MM INFO with network name */
132 rc = gsm48_tx_mm_info(msg->lchan);
133 }
Harald Welteb83d9382009-12-12 21:00:48 +0100134
Harald Welteee5ad162009-08-09 19:07:00 +0200135 /* call subscr_update after putting the loc_upd_acc
136 * in the transmit queue, since S_SUBSCR_ATTACHED might
137 * trigger further action like SMS delivery */
138 subscr_update(lchan->subscr, msg->trx->bts,
139 GSM_SUBSCRIBER_UPDATE_ATTACHED);
Harald Welteb83d9382009-12-12 21:00:48 +0100140 /* try to close channel ASAP */
141 lchan_auto_release(lchan);
Harald Welteee5ad162009-08-09 19:07:00 +0200142 return rc;
Holger Freytherd51524f2009-06-09 08:27:07 +0000143 }
144
145 return 0;
146}
147
Holger Freyther7c19f742009-06-06 13:54:35 +0000148static int gsm0408_handle_lchan_signal(unsigned int subsys, unsigned int signal,
149 void *handler_data, void *signal_data)
150{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800151 struct gsm_trans *trans, *temp;
152
Holger Freyther7c19f742009-06-06 13:54:35 +0000153 if (subsys != SS_LCHAN || signal != S_LCHAN_UNEXPECTED_RELEASE)
154 return 0;
155
156 /*
157 * Cancel any outstanding location updating request
158 * operation taking place on the lchan.
159 */
Harald Welte1a5c6bd2009-07-04 09:35:21 +0200160 struct gsm_lchan *lchan = (struct gsm_lchan *)signal_data;
Harald Weltec05677b2009-06-26 20:17:06 +0200161 if (!lchan)
162 return 0;
163
Holger Freyther7c19f742009-06-06 13:54:35 +0000164 release_loc_updating_req(lchan);
165
Harald Welte4bfdfe72009-06-10 23:11:52 +0800166 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200167 /* FIXME: this is not neccessarily the right thing to do, we should
168 * only set trans->lchan to NULL and wait for another lchan to be
169 * established to the same MM entity (phone/subscriber) */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800170 llist_for_each_entry_safe(trans, temp, &lchan->ts->trx->bts->network->trans_list, entry) {
171 if (trans->lchan == lchan)
Harald Weltedcaf5652009-07-23 18:56:43 +0200172 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800173 }
174
Holger Freyther7c19f742009-06-06 13:54:35 +0000175 return 0;
176}
177
Harald Welte09e38af2009-02-16 22:52:23 +0000178static const char bcd_num_digits[] = {
179 '0', '1', '2', '3', '4', '5', '6', '7',
180 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
181};
182
Harald Welte0c389302009-06-10 12:08:54 +0800183/* decode a 'called/calling/connect party BCD number' as in 10.5.4.7 */
184int decode_bcd_number(char *output, int output_len, const u_int8_t *bcd_lv,
185 int h_len)
Harald Welte09e38af2009-02-16 22:52:23 +0000186{
187 u_int8_t in_len = bcd_lv[0];
188 int i;
189
Harald Welte0c389302009-06-10 12:08:54 +0800190 for (i = 1 + h_len; i <= in_len; i++) {
Harald Welte09e38af2009-02-16 22:52:23 +0000191 /* lower nibble */
192 output_len--;
193 if (output_len <= 1)
194 break;
195 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
196
197 /* higher nibble */
198 output_len--;
199 if (output_len <= 1)
200 break;
201 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
202 }
203 if (output_len >= 1)
204 *output++ = '\0';
205
Harald Welte0c389302009-06-10 12:08:54 +0800206 return 0;
Harald Welte09e38af2009-02-16 22:52:23 +0000207}
208
209/* convert a single ASCII character to call-control BCD */
210static int asc_to_bcd(const char asc)
211{
212 int i;
213
214 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
215 if (bcd_num_digits[i] == asc)
216 return i;
217 }
218 return -EINVAL;
219}
220
Harald Welte0c389302009-06-10 12:08:54 +0800221/* convert a ASCII phone number to 'called/calling/connect party BCD number' */
Harald Welte09e38af2009-02-16 22:52:23 +0000222int encode_bcd_number(u_int8_t *bcd_lv, u_int8_t max_len,
Harald Welte0c389302009-06-10 12:08:54 +0800223 int h_len, const char *input)
Harald Welte09e38af2009-02-16 22:52:23 +0000224{
225 int in_len = strlen(input);
226 int i;
Harald Welte0c389302009-06-10 12:08:54 +0800227 u_int8_t *bcd_cur = bcd_lv + 1 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000228
229 /* two digits per byte, plus type byte */
Harald Welte0c389302009-06-10 12:08:54 +0800230 bcd_lv[0] = in_len/2 + h_len;
Harald Welte09e38af2009-02-16 22:52:23 +0000231 if (in_len % 2)
232 bcd_lv[0]++;
233
Harald Welte0c389302009-06-10 12:08:54 +0800234 if (bcd_lv[0] > max_len)
235 return -EIO;
Harald Welte09e38af2009-02-16 22:52:23 +0000236
237 for (i = 0; i < in_len; i++) {
238 int rc = asc_to_bcd(input[i]);
239 if (rc < 0)
240 return rc;
241 if (i % 2 == 0)
242 *bcd_cur = rc;
243 else
244 *bcd_cur++ |= (rc << 4);
245 }
246 /* append padding nibble in case of odd length */
247 if (i % 2)
248 *bcd_cur++ |= 0xf0;
249
250 /* return how many bytes we used */
251 return (bcd_cur - bcd_lv);
252}
253
Harald Welte0c389302009-06-10 12:08:54 +0800254/* decode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800255static int decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
Harald Welte0c389302009-06-10 12:08:54 +0800256 const u_int8_t *lv)
257{
258 u_int8_t in_len = lv[0];
259 int i, s;
260
261 if (in_len < 1)
262 return -EINVAL;
263
Harald Welte4bfdfe72009-06-10 23:11:52 +0800264 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
Harald Welte0c389302009-06-10 12:08:54 +0800265
266 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800267 bcap->transfer = lv[1] & 0x07;
268 bcap->mode = (lv[1] & 0x08) >> 3;
269 bcap->coding = (lv[1] & 0x10) >> 4;
270 bcap->radio = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800271
Harald Welte09b7e7f2009-12-12 21:36:53 +0100272 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
273 i = 1;
274 s = 0;
275 while(!(lv[i] & 0x80)) {
276 i++; /* octet 3a etc */
277 if (in_len < i)
278 return 0;
279 bcap->speech_ver[s++] = lv[i] & 0x0f;
280 bcap->speech_ver[s] = -1; /* end of list */
281 if (i == 2) /* octet 3a */
282 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
283 if (s == 7) /* maximum speech versions + end of list */
284 return 0;
285 }
286 } else {
287 i = 1;
288 while (!(lv[i] & 0x80)) {
289 i++; /* octet 3a etc */
290 if (in_len < i)
291 return 0;
292 /* ignore them */
293 }
294 /* FIXME: implement OCTET 4+ parsing */
Harald Welte0c389302009-06-10 12:08:54 +0800295 }
296
297 return 0;
298}
299
300/* encode 'bearer capability' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800301static int encode_bearer_cap(struct msgb *msg, int lv_only,
302 const struct gsm_mncc_bearer_cap *bcap)
Harald Welte0c389302009-06-10 12:08:54 +0800303{
304 u_int8_t lv[32 + 1];
Harald Welte09b7e7f2009-12-12 21:36:53 +0100305 int i = 1, s;
Harald Welte0c389302009-06-10 12:08:54 +0800306
Harald Welte4bfdfe72009-06-10 23:11:52 +0800307 lv[1] = bcap->transfer;
308 lv[1] |= bcap->mode << 3;
309 lv[1] |= bcap->coding << 4;
310 lv[1] |= bcap->radio << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800311
Harald Welte09b7e7f2009-12-12 21:36:53 +0100312 if (bcap->transfer == GSM_MNCC_BCAP_SPEECH) {
313 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
314 i++; /* octet 3a etc */
315 lv[i] = bcap->speech_ver[s];
316 if (i == 2) /* octet 3a */
317 lv[i] |= bcap->speech_ctm << 5;
318 }
319 lv[i] |= 0x80; /* last IE of octet 3 etc */
320 } else {
321 /* FIXME: implement OCTET 4+ encoding */
Harald Welte0c389302009-06-10 12:08:54 +0800322 }
Harald Welte0c389302009-06-10 12:08:54 +0800323
324 lv[0] = i;
325 if (lv_only)
326 msgb_lv_put(msg, lv[0], lv+1);
327 else
328 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
329
330 return 0;
331}
332
333/* decode 'call control cap' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800334static int decode_cccap(struct gsm_mncc_cccap *ccap, const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800335{
336 u_int8_t in_len = lv[0];
337
338 if (in_len < 1)
339 return -EINVAL;
340
341 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800342 ccap->dtmf = lv[1] & 0x01;
343 ccap->pcp = (lv[1] & 0x02) >> 1;
Harald Welte0c389302009-06-10 12:08:54 +0800344
345 return 0;
346}
347
348/* decode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800349static int decode_called(struct gsm_mncc_number *called,
350 const u_int8_t *lv)
Harald Welte0c389302009-06-10 12:08:54 +0800351{
352 u_int8_t in_len = lv[0];
353
354 if (in_len < 1)
355 return -EINVAL;
356
357 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800358 called->plan = lv[1] & 0x0f;
359 called->type = (lv[1] & 0x70) >> 4;
Harald Welte0c389302009-06-10 12:08:54 +0800360
361 /* octet 4..N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800362 decode_bcd_number(called->number, sizeof(called->number), lv, 1);
Harald Welte0c389302009-06-10 12:08:54 +0800363
364 return 0;
365}
366
367/* encode 'called party BCD number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800368static int encode_called(struct msgb *msg,
369 const struct gsm_mncc_number *called)
Harald Welte0c389302009-06-10 12:08:54 +0800370{
371 u_int8_t lv[18];
372 int ret;
373
374 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800375 lv[1] = called->plan;
376 lv[1] |= called->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800377
378 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800379 ret = encode_bcd_number(lv, sizeof(lv), 1, called->number);
Harald Welte0c389302009-06-10 12:08:54 +0800380 if (ret < 0)
381 return ret;
382
383 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
384
385 return 0;
386}
387
388/* encode callerid of various IEs */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800389static int encode_callerid(struct msgb *msg, int ie,
390 const struct gsm_mncc_number *callerid)
Harald Welte0c389302009-06-10 12:08:54 +0800391{
392 u_int8_t lv[13];
393 int h_len = 1;
394 int ret;
395
396 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800397 lv[1] = callerid->plan;
398 lv[1] |= callerid->type << 4;
Harald Welte0c389302009-06-10 12:08:54 +0800399
Harald Welte4bfdfe72009-06-10 23:11:52 +0800400 if (callerid->present || callerid->screen) {
Harald Welte0c389302009-06-10 12:08:54 +0800401 /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800402 lv[2] = callerid->screen;
403 lv[2] |= callerid->present << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800404 lv[2] |= 0x80;
405 h_len++;
406 } else
407 lv[1] |= 0x80;
408
409 /* octet 4..N, octet 2 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800410 ret = encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
Harald Welte0c389302009-06-10 12:08:54 +0800411 if (ret < 0)
412 return ret;
413
414 msgb_tlv_put(msg, ie, lv[0], lv+1);
415
416 return 0;
417}
418
419/* decode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800420static int decode_cause(struct gsm_mncc_cause *cause,
Harald Welte0c389302009-06-10 12:08:54 +0800421 const u_int8_t *lv)
422{
423 u_int8_t in_len = lv[0];
424 int i;
425
426 if (in_len < 2)
427 return -EINVAL;
428
Harald Welte4bfdfe72009-06-10 23:11:52 +0800429 cause->diag_len = 0;
Harald Welte0c389302009-06-10 12:08:54 +0800430
431 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800432 cause->location = lv[1] & 0x0f;
433 cause->coding = (lv[1] & 0x60) >> 5;
Harald Welte0c389302009-06-10 12:08:54 +0800434
435 i = 1;
436 if (!(lv[i] & 0x80)) {
437 i++; /* octet 3a */
438 if (in_len < i+1)
439 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800440 cause->rec = 1;
441 cause->rec_val = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800442
443 }
444 i++;
445
446 /* octet 4 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447 cause->value = lv[i] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800448 i++;
449
450 if (in_len < i) /* no diag */
451 return 0;
452
453 if (in_len - (i-1) > 32) /* maximum 32 octets */
454 return 0;
455
456 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800457 memcpy(cause->diag, lv + i, in_len - (i-1));
458 cause->diag_len = in_len - (i-1);
Harald Welte0c389302009-06-10 12:08:54 +0800459
460 return 0;
461}
462
463/* encode 'cause' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800464static int encode_cause(struct msgb *msg, int lv_only,
465 const struct gsm_mncc_cause *cause)
Harald Welte0c389302009-06-10 12:08:54 +0800466{
467 u_int8_t lv[32+4];
468 int i;
469
Harald Welte4bfdfe72009-06-10 23:11:52 +0800470 if (cause->diag_len > 32)
Harald Welte0c389302009-06-10 12:08:54 +0800471 return -EINVAL;
472
473 /* octet 3 */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800474 lv[1] = cause->location;
475 lv[1] |= cause->coding << 5;
Harald Welte0c389302009-06-10 12:08:54 +0800476
477 i = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800478 if (cause->rec) {
Harald Welte0c389302009-06-10 12:08:54 +0800479 i++; /* octet 3a */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800480 lv[i] = cause->rec_val;
Harald Welte0c389302009-06-10 12:08:54 +0800481 }
482 lv[i] |= 0x80; /* end of octet 3 */
483
484 /* octet 4 */
485 i++;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800486 lv[i] = 0x80 | cause->value;
Harald Welte0c389302009-06-10 12:08:54 +0800487
488 /* octet 5-N */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800489 if (cause->diag_len) {
490 memcpy(lv + i, cause->diag, cause->diag_len);
491 i += cause->diag_len;
Harald Welte0c389302009-06-10 12:08:54 +0800492 }
493
494 lv[0] = i;
495 if (lv_only)
496 msgb_lv_put(msg, lv[0], lv+1);
497 else
498 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
499
500 return 0;
501}
502
503/* encode 'calling number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800504static int encode_calling(struct msgb *msg,
505 const struct gsm_mncc_number *calling)
Harald Welte0c389302009-06-10 12:08:54 +0800506{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800507 return encode_callerid(msg, GSM48_IE_CALLING_BCD, calling);
Harald Welte0c389302009-06-10 12:08:54 +0800508}
509
510/* encode 'connected number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800511static int encode_connected(struct msgb *msg,
512 const struct gsm_mncc_number *connected)
Harald Welte0c389302009-06-10 12:08:54 +0800513{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800514 return encode_callerid(msg, GSM48_IE_CONN_BCD, connected);
Harald Welte0c389302009-06-10 12:08:54 +0800515}
516
517/* encode 'redirecting number' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800518static int encode_redirecting(struct msgb *msg,
519 const struct gsm_mncc_number *redirecting)
Harald Welte0c389302009-06-10 12:08:54 +0800520{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800521 return encode_callerid(msg, GSM48_IE_REDIR_BCD, redirecting);
Harald Welte0c389302009-06-10 12:08:54 +0800522}
523
524/* decode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800525static int decode_facility(struct gsm_mncc_facility *facility,
Harald Welte0c389302009-06-10 12:08:54 +0800526 const u_int8_t *lv)
527{
528 u_int8_t in_len = lv[0];
529
530 if (in_len < 1)
531 return -EINVAL;
532
Harald Welte4bfdfe72009-06-10 23:11:52 +0800533 if (in_len > sizeof(facility->info))
Harald Welte0c389302009-06-10 12:08:54 +0800534 return -EINVAL;
535
Harald Welte4bfdfe72009-06-10 23:11:52 +0800536 memcpy(facility->info, lv+1, in_len);
537 facility->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800538
539 return 0;
540}
541
542/* encode 'facility' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800543static int encode_facility(struct msgb *msg, int lv_only,
544 const struct gsm_mncc_facility *facility)
Harald Welte0c389302009-06-10 12:08:54 +0800545{
546 u_int8_t lv[GSM_MAX_FACILITY + 1];
547
Harald Welte4bfdfe72009-06-10 23:11:52 +0800548 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
Harald Welte0c389302009-06-10 12:08:54 +0800549 return -EINVAL;
550
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 memcpy(lv+1, facility->info, facility->len);
552 lv[0] = facility->len;
Harald Welte0c389302009-06-10 12:08:54 +0800553 if (lv_only)
554 msgb_lv_put(msg, lv[0], lv+1);
555 else
556 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
557
558 return 0;
559}
560
561/* decode 'notify' */
562static int decode_notify(int *notify, const u_int8_t *v)
563{
564 *notify = v[0] & 0x7f;
565
566 return 0;
567}
568
569/* encode 'notify' */
570static int encode_notify(struct msgb *msg, int notify)
571{
572 msgb_v_put(msg, notify | 0x80);
573
574 return 0;
575}
576
577/* encode 'signal' */
578static int encode_signal(struct msgb *msg, int signal)
579{
580 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
581
582 return 0;
583}
584
585/* decode 'keypad' */
586static int decode_keypad(int *keypad, const u_int8_t *lv)
587{
588 u_int8_t in_len = lv[0];
589
590 if (in_len < 1)
591 return -EINVAL;
592
593 *keypad = lv[1] & 0x7f;
594
595 return 0;
596}
597
598/* encode 'keypad' */
599static int encode_keypad(struct msgb *msg, int keypad)
600{
601 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
602
603 return 0;
604}
605
606/* decode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800607static int decode_progress(struct gsm_mncc_progress *progress,
Harald Welte0c389302009-06-10 12:08:54 +0800608 const u_int8_t *lv)
609{
610 u_int8_t in_len = lv[0];
611
612 if (in_len < 2)
613 return -EINVAL;
614
Harald Welte4bfdfe72009-06-10 23:11:52 +0800615 progress->coding = (lv[1] & 0x60) >> 5;
616 progress->location = lv[1] & 0x0f;
617 progress->descr = lv[2] & 0x7f;
Harald Welte0c389302009-06-10 12:08:54 +0800618
619 return 0;
620}
621
622/* encode 'progress' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800623static int encode_progress(struct msgb *msg, int lv_only,
624 const struct gsm_mncc_progress *p)
Harald Welte0c389302009-06-10 12:08:54 +0800625{
626 u_int8_t lv[3];
627
628 lv[0] = 2;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800629 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
630 lv[2] = 0x80 | (p->descr & 0x7f);
Harald Welte0c389302009-06-10 12:08:54 +0800631 if (lv_only)
632 msgb_lv_put(msg, lv[0], lv+1);
633 else
634 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
635
636 return 0;
637}
638
639/* decode 'user-user' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800640static int decode_useruser(struct gsm_mncc_useruser *uu,
Harald Welte0c389302009-06-10 12:08:54 +0800641 const u_int8_t *lv)
642{
643 u_int8_t in_len = lv[0];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644 char *info = uu->info;
645 int info_len = sizeof(uu->info);
Harald Welte0c389302009-06-10 12:08:54 +0800646 int i;
647
648 if (in_len < 1)
649 return -EINVAL;
650
Harald Welte4bfdfe72009-06-10 23:11:52 +0800651 uu->proto = lv[1];
Harald Welte0c389302009-06-10 12:08:54 +0800652
653 for (i = 2; i <= in_len; i++) {
654 info_len--;
655 if (info_len <= 1)
656 break;
657 *info++ = lv[i];
658 }
659 if (info_len >= 1)
660 *info++ = '\0';
661
662 return 0;
663}
664
665/* encode 'useruser' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800666static int encode_useruser(struct msgb *msg, int lv_only,
667 const struct gsm_mncc_useruser *uu)
Harald Welte0c389302009-06-10 12:08:54 +0800668{
669 u_int8_t lv[GSM_MAX_USERUSER + 2];
670
Harald Welte4bfdfe72009-06-10 23:11:52 +0800671 if (strlen(uu->info) > GSM_MAX_USERUSER)
Harald Welte0c389302009-06-10 12:08:54 +0800672 return -EINVAL;
673
Harald Welte4bfdfe72009-06-10 23:11:52 +0800674 lv[0] = 1 + strlen(uu->info);
675 lv[1] = uu->proto;
676 memcpy(lv + 2, uu->info, strlen(uu->info));
Harald Welte0c389302009-06-10 12:08:54 +0800677 if (lv_only)
678 msgb_lv_put(msg, lv[0], lv+1);
679 else
680 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
681
682 return 0;
683}
684
685/* decode 'ss version' */
Harald Welte4bfdfe72009-06-10 23:11:52 +0800686static int decode_ssversion(struct gsm_mncc_ssversion *ssv,
Harald Welte0c389302009-06-10 12:08:54 +0800687 const u_int8_t *lv)
688{
689 u_int8_t in_len = lv[0];
690
Harald Welte4bfdfe72009-06-10 23:11:52 +0800691 if (in_len < 1 || in_len < sizeof(ssv->info))
Harald Welte0c389302009-06-10 12:08:54 +0800692 return -EINVAL;
693
Harald Welte4bfdfe72009-06-10 23:11:52 +0800694 memcpy(ssv->info, lv + 1, in_len);
695 ssv->len = in_len;
Harald Welte0c389302009-06-10 12:08:54 +0800696
697 return 0;
698}
699
700/* encode 'more data' */
701static int encode_more(struct msgb *msg)
702{
703 u_int8_t *ie;
704
705 ie = msgb_put(msg, 1);
706 ie[0] = GSM48_IE_MORE_DATA;
707
708 return 0;
709}
710
Holger Freyther429e7762008-12-30 13:28:30 +0000711/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000712int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000713{
Harald Welte24ff6ee2009-12-22 00:41:05 +0100714 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000715 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000716 struct gsm48_hdr *gh;
717
Harald Welte8470bf22008-12-25 23:28:35 +0000718 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000719
720 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
721 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000722 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000723 gh->data[0] = cause;
724
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100725 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
726 "LAC=%u BTS=%u\n", lchan->subscr ?
727 subscr_name(lchan->subscr) : "unknown",
728 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100729
Harald Welteffa55a42009-12-22 19:07:32 +0100730 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000731
Harald Welte39e2ead2009-07-23 21:13:03 +0200732 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000733}
734
735/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000736int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000737{
Harald Welte8470bf22008-12-25 23:28:35 +0000738 struct gsm_bts *bts = lchan->ts->trx->bts;
739 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000740 struct gsm48_hdr *gh;
741 struct gsm48_loc_area_id *lai;
742 u_int8_t *mid;
743
Harald Welte8470bf22008-12-25 23:28:35 +0000744 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000745
746 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
747 gh->proto_discr = GSM48_PDISC_MM;
748 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
749
750 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100751 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000752 bts->network->network_code, bts->location_area_code);
753
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200754 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200755 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000756
757 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
758
Harald Welteffa55a42009-12-22 19:07:32 +0100759 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100760
Harald Welteb83d9382009-12-12 21:00:48 +0100761 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000762}
763
Harald Weltebf5e8df2009-02-03 12:59:45 +0000764/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000765static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
766{
767 struct msgb *msg = gsm48_msgb_alloc();
768 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000769
Harald Welte231ad4f2008-12-27 11:15:38 +0000770 msg->lchan = lchan;
771
772 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
773 gh->proto_discr = GSM48_PDISC_MM;
774 gh->msg_type = GSM48_MT_MM_ID_REQ;
775 gh->data[0] = id_type;
776
Harald Welte39e2ead2009-07-23 21:13:03 +0200777 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000778}
779
Harald Welte231ad4f2008-12-27 11:15:38 +0000780
Harald Weltebf5e8df2009-02-03 12:59:45 +0000781/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000782static int mm_rx_id_resp(struct msgb *msg)
783{
784 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000785 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200786 struct gsm_bts *bts = lchan->ts->trx->bts;
787 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000788 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200789 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000790
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200791 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000792 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000793 mi_type, mi_string);
794
Harald Welte7659de12009-12-13 12:39:18 +0100795 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
796
Harald Welte75a983f2008-12-27 21:34:06 +0000797 switch (mi_type) {
798 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200799 /* look up subscriber based on IMSI, create if not found */
800 if (!lchan->subscr) {
801 lchan->subscr = subscr_get_by_imsi(net, mi_string);
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200802 if (!lchan->subscr)
803 lchan->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200804 }
Holger Freyther73487a22008-12-31 18:53:57 +0000805 if (lchan->loc_operation)
806 lchan->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000807 break;
808 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000809 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000810 /* update subscribe <-> IMEI mapping */
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200811 if (lchan->subscr) {
Harald Welte75a983f2008-12-27 21:34:06 +0000812 db_subscriber_assoc_imei(lchan->subscr, mi_string);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200813 db_sync_equipment(&lchan->subscr->equipment);
814 }
Holger Freyther73487a22008-12-31 18:53:57 +0000815 if (lchan->loc_operation)
816 lchan->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000817 break;
818 }
Holger Freyther73487a22008-12-31 18:53:57 +0000819
820 /* Check if we can let the mobile station enter */
Holger Freytherd51524f2009-06-09 08:27:07 +0000821 return gsm0408_authorize(lchan, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000822}
823
Harald Welte255539c2008-12-28 02:26:27 +0000824
825static void loc_upd_rej_cb(void *data)
826{
827 struct gsm_lchan *lchan = data;
Harald Welte1085c092009-11-18 20:33:19 +0100828 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000829
Holger Freyther73487a22008-12-31 18:53:57 +0000830 release_loc_updating_req(lchan);
Harald Welte1085c092009-11-18 20:33:19 +0100831 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000832 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000833}
834
Holger Freytherb7193e42008-12-29 17:44:08 +0000835static void schedule_reject(struct gsm_lchan *lchan)
836{
Holger Freyther73487a22008-12-31 18:53:57 +0000837 lchan->loc_operation->updating_timer.cb = loc_upd_rej_cb;
838 lchan->loc_operation->updating_timer.data = lchan;
Harald Welteff117a82009-05-23 05:22:08 +0000839 bsc_schedule_timer(&lchan->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000840}
841
Harald Welte2a139372009-02-22 21:14:55 +0000842static const char *lupd_name(u_int8_t type)
843{
844 switch (type) {
845 case GSM48_LUPD_NORMAL:
846 return "NORMAL";
847 case GSM48_LUPD_PERIODIC:
848 return "PEROIDOC";
849 case GSM48_LUPD_IMSI_ATT:
850 return "IMSI ATTACH";
851 default:
852 return "UNKNOWN";
853 }
854}
855
Harald Weltebf5e8df2009-02-03 12:59:45 +0000856/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000857static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000858{
Harald Welte8470bf22008-12-25 23:28:35 +0000859 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000860 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800861 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000862 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200863 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000864 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200865 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000866 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000867
Harald Welte8470bf22008-12-25 23:28:35 +0000868 lu = (struct gsm48_loc_upd_req *) gh->data;
869
870 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000871
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200872 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000873
Harald Weltea0368542009-06-27 02:58:43 +0200874 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000875 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000876
Harald Welte7659de12009-12-13 12:39:18 +0100877 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
878
Harald Welte24ff6ee2009-12-22 00:41:05 +0100879 switch (lu->type) {
880 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100881 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100882 break;
883 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100884 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100885 break;
886 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100887 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100888 break;
889 }
890
Holger Freythereaf04692009-06-06 13:54:44 +0000891 /*
892 * Pseudo Spoof detection: Just drop a second/concurrent
893 * location updating request.
894 */
895 if (lchan->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200896 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Freythereaf04692009-06-06 13:54:44 +0000897 lchan->loc_operation);
898 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
899 return 0;
900 }
901
Holger Freyther73487a22008-12-31 18:53:57 +0000902 allocate_loc_updating_req(lchan);
903
Harald Welte52b1f982008-12-23 20:25:15 +0000904 switch (mi_type) {
905 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200906 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000907 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000908 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000909 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000910
Jan Luebbe370b41d2009-08-12 10:19:34 +0200911 /* look up subscriber based on IMSI, create if not found */
912 subscr = subscr_get_by_imsi(bts->network, mi_string);
913 if (!subscr) {
914 subscr = db_create_subscriber(bts->network, mi_string);
915 }
Harald Welte4b634542008-12-27 01:55:51 +0000916 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000917 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200918 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000919 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000920 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Freyther73487a22008-12-31 18:53:57 +0000921 lchan->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000922
Harald Welte52b1f982008-12-23 20:25:15 +0000923 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200924 subscr = subscr_get_by_tmsi(bts->network,
925 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000926 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000927 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000928 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Freyther73487a22008-12-31 18:53:57 +0000929 lchan->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000930 }
931 break;
932 case GSM_MI_TYPE_IMEI:
933 case GSM_MI_TYPE_IMEISV:
934 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200935 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000936 break;
937 default:
Harald Weltea0368542009-06-27 02:58:43 +0200938 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000939 break;
940 }
941
Harald Welte24516ea2009-07-04 10:18:00 +0200942 /* schedule the reject timer */
943 schedule_reject(lchan);
944
Harald Welte4bfdfe72009-06-10 23:11:52 +0800945 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200946 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800947 /* FIXME: request id? close channel? */
948 return -EINVAL;
949 }
950
Harald Welte255539c2008-12-28 02:26:27 +0000951 lchan->subscr = subscr;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200952 lchan->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000953
Harald Welte24516ea2009-07-04 10:18:00 +0200954 /* check if we can let the subscriber into our network immediately
955 * or if we need to wait for identity responses. */
Holger Freytherd51524f2009-06-09 08:27:07 +0000956 return gsm0408_authorize(lchan, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000957}
958
Harald Welte4bfdfe72009-06-10 23:11:52 +0800959#if 0
960static u_int8_t to_bcd8(u_int8_t val)
961{
962 return ((val / 10) << 4) | (val % 10);
963}
964#endif
965
Harald Weltedb253af2008-12-30 17:56:55 +0000966/* Section 9.2.15a */
967int gsm48_tx_mm_info(struct gsm_lchan *lchan)
968{
969 struct msgb *msg = gsm48_msgb_alloc();
970 struct gsm48_hdr *gh;
971 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000972 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200973 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800974#if 0
975 time_t cur_t;
976 struct tm* cur_time;
977 int tz15min;
978#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000979
980 msg->lchan = lchan;
981
982 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
983 gh->proto_discr = GSM48_PDISC_MM;
984 gh->msg_type = GSM48_MT_MM_INFO;
985
986 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200987#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000988 name_len = strlen(net->name_long);
989 /* 10.5.3.5a */
990 ptr8 = msgb_put(msg, 3);
991 ptr8[0] = GSM48_IE_NAME_LONG;
992 ptr8[1] = name_len*2 +1;
993 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
994
995 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
996 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000997 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000998
999 /* FIXME: Use Cell Broadcast, not UCS-2, since
1000 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +02001001#endif
1002 name_len = (strlen(net->name_long)*7)/8;
1003 name_pad = (8 - strlen(net->name_long)*7)%8;
1004 if (name_pad > 0)
1005 name_len++;
1006 /* 10.5.3.5a */
1007 ptr8 = msgb_put(msg, 3);
1008 ptr8[0] = GSM48_IE_NAME_LONG;
1009 ptr8[1] = name_len +1;
1010 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1011
1012 ptr8 = msgb_put(msg, name_len);
1013 gsm_7bit_encode(ptr8, net->name_long);
1014
Harald Weltedb253af2008-12-30 17:56:55 +00001015 }
1016
1017 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +02001018#if 0
Harald Weltedb253af2008-12-30 17:56:55 +00001019 name_len = strlen(net->name_short);
1020 /* 10.5.3.5a */
1021 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +02001022 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +00001023 ptr8[1] = name_len*2 + 1;
1024 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
1025
Harald Weltee872cb12009-01-01 00:33:37 +00001026 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +00001027 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +00001028 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +02001029#endif
1030 name_len = (strlen(net->name_short)*7)/8;
1031 name_pad = (8 - strlen(net->name_short)*7)%8;
1032 if (name_pad > 0)
1033 name_len++;
1034 /* 10.5.3.5a */
1035 ptr8 = (u_int8_t *) msgb_put(msg, 3);
1036 ptr8[0] = GSM48_IE_NAME_SHORT;
1037 ptr8[1] = name_len +1;
1038 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
1039
1040 ptr8 = msgb_put(msg, name_len);
1041 gsm_7bit_encode(ptr8, net->name_short);
1042
Harald Weltedb253af2008-12-30 17:56:55 +00001043 }
1044
1045#if 0
1046 /* Section 10.5.3.9 */
1047 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001048 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +00001049 ptr8 = msgb_put(msg, 8);
1050 ptr8[0] = GSM48_IE_NET_TIME_TZ;
1051 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
1052 ptr8[2] = to_bcd8(cur_time->tm_mon);
1053 ptr8[3] = to_bcd8(cur_time->tm_mday);
1054 ptr8[4] = to_bcd8(cur_time->tm_hour);
1055 ptr8[5] = to_bcd8(cur_time->tm_min);
1056 ptr8[6] = to_bcd8(cur_time->tm_sec);
1057 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
1058 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001059 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +00001060 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001061 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +00001062#endif
1063
Daniel Willmanneea93372009-08-13 03:42:07 +02001064 DEBUGP(DMM, "-> MM INFO\n");
1065
Harald Welte39e2ead2009-07-23 21:13:03 +02001066 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +00001067}
1068
Harald Welte7984d5c2009-08-12 22:56:50 +02001069/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +01001070int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +02001071{
1072 struct msgb *msg = gsm48_msgb_alloc();
1073 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +02001074 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +02001075
Sylvain Munaut8608e7c2009-12-24 00:24:29 +01001076 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +02001077
1078 msg->lchan = lchan;
1079 gh->proto_discr = GSM48_PDISC_MM;
1080 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
1081
Sylvain Munautbd55a6d2009-12-24 00:23:46 +01001082 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +02001083
Harald Welte7984d5c2009-08-12 22:56:50 +02001084 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +02001085 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +02001086 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +02001087
1088 return gsm48_sendmsg(msg, NULL);
1089}
1090
1091/* Section 9.2.1 */
1092int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
1093{
1094 DEBUGP(DMM, "-> AUTH REJECT\n");
1095 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
1096}
1097
Harald Welte4b634542008-12-27 01:55:51 +00001098static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
1099{
Harald Welte4b634542008-12-27 01:55:51 +00001100 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +00001101 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +00001102}
Harald Welteba4cf162009-01-10 01:49:35 +00001103
1104/* 9.2.6 CM service reject */
1105static int gsm48_tx_mm_serv_rej(struct gsm_lchan *lchan,
1106 enum gsm48_reject_value value)
1107{
1108 struct msgb *msg = gsm48_msgb_alloc();
1109 struct gsm48_hdr *gh;
1110
1111 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1112
1113 msg->lchan = lchan;
1114 use_lchan(lchan);
1115
1116 gh->proto_discr = GSM48_PDISC_MM;
1117 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1118 gh->data[0] = value;
1119 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
1120
Harald Welte39e2ead2009-07-23 21:13:03 +02001121 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +00001122}
1123
Harald Welte4ed0e922009-01-10 03:17:30 +00001124/*
1125 * Handle CM Service Requests
1126 * a) Verify that the packet is long enough to contain the information
1127 * we require otherwsie reject with INCORRECT_MESSAGE
1128 * b) Try to parse the TMSI. If we do not have one reject
1129 * c) Check that we know the subscriber with the TMSI otherwise reject
1130 * with a HLR cause
1131 * d) Set the subscriber on the gsm_lchan and accept
1132 */
Harald Welte4b634542008-12-27 01:55:51 +00001133static int gsm48_rx_mm_serv_req(struct msgb *msg)
1134{
Harald Welteba4cf162009-01-10 01:49:35 +00001135 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001136 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +00001137
Harald Welte9176bd42009-07-23 18:46:00 +02001138 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +00001139 struct gsm_subscriber *subscr;
1140 struct gsm48_hdr *gh = msgb_l3(msg);
1141 struct gsm48_service_request *req =
1142 (struct gsm48_service_request *)gh->data;
Harald Weltec9e02182009-05-01 19:07:53 +00001143 /* unfortunately in Phase1 the classmar2 length is variable */
1144 u_int8_t classmark2_len = gh->data[1];
1145 u_int8_t *classmark2 = gh->data+2;
1146 u_int8_t mi_len = *(classmark2 + classmark2_len);
1147 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +00001148
Harald Weltec9e02182009-05-01 19:07:53 +00001149 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +00001150 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +00001151 DEBUGPC(DMM, "wrong sized message\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001152 return gsm48_tx_mm_serv_rej(msg->lchan,
1153 GSM48_REJECT_INCORRECT_MESSAGE);
1154 }
1155
1156 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +00001157 DEBUGPC(DMM, "does not fit in packet\n");
Harald Welteba4cf162009-01-10 01:49:35 +00001158 return gsm48_tx_mm_serv_rej(msg->lchan,
1159 GSM48_REJECT_INCORRECT_MESSAGE);
1160 }
1161
Harald Weltec9e02182009-05-01 19:07:53 +00001162 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +00001163 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +00001164 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Harald Welteba4cf162009-01-10 01:49:35 +00001165 return gsm48_tx_mm_serv_rej(msg->lchan,
1166 GSM48_REJECT_INCORRECT_MESSAGE);
1167 }
1168
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001169 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +00001170 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +00001171 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +00001172
Harald Welte7659de12009-12-13 12:39:18 +01001173 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
1174
Harald Welte3ac7f102009-08-10 10:12:45 +02001175 if (is_siemens_bts(bts))
1176 send_siemens_mrpci(msg->lchan, classmark2-1);
1177
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001178 subscr = subscr_get_by_tmsi(bts->network,
1179 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +00001180
Harald Welte2a139372009-02-22 21:14:55 +00001181 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +00001182 if (!subscr)
1183 return gsm48_tx_mm_serv_rej(msg->lchan,
1184 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
1185
1186 if (!msg->lchan->subscr)
1187 msg->lchan->subscr = subscr;
Sylvain Munautea3f6742009-12-18 18:28:10 +01001188 else if (msg->lchan->subscr == subscr)
1189 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1190 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001191 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1192 subscr_put(subscr);
1193 }
1194
Harald Weltec2e302d2009-07-05 14:08:13 +02001195 subscr->equipment.classmark2_len = classmark2_len;
1196 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1197 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001198
Harald Welte4b634542008-12-27 01:55:51 +00001199 return gsm48_tx_mm_serv_ack(msg->lchan);
1200}
1201
Harald Welte2a139372009-02-22 21:14:55 +00001202static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
1203{
Harald Welte9176bd42009-07-23 18:46:00 +02001204 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001205 struct gsm48_hdr *gh = msgb_l3(msg);
1206 struct gsm48_imsi_detach_ind *idi =
1207 (struct gsm48_imsi_detach_ind *) gh->data;
1208 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001209 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001210 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001211
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001212 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +00001213 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
1214 mi_type, mi_string);
1215
Harald Welteffa55a42009-12-22 19:07:32 +01001216 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001217
Harald Welte2a139372009-02-22 21:14:55 +00001218 switch (mi_type) {
1219 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001220 subscr = subscr_get_by_tmsi(bts->network,
1221 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001222 break;
1223 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001224 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001225 break;
1226 case GSM_MI_TYPE_IMEI:
1227 case GSM_MI_TYPE_IMEISV:
1228 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +00001229 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001230 break;
1231 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +00001232 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001233 break;
1234 }
1235
Holger Freyther4a49e772009-04-12 05:37:29 +00001236 if (subscr) {
1237 subscr_update(subscr, msg->trx->bts,
1238 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001239 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001240
1241 subscr->equipment.classmark1 = idi->classmark1;
1242 db_sync_equipment(&subscr->equipment);
1243
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001244 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001245 } else
Harald Welte2a139372009-02-22 21:14:55 +00001246 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1247
Harald Welte31981a02009-12-20 09:58:40 +01001248 /* FIXME: iterate over all transactions and release them,
1249 * imagine an IMSI DETACH happening during an active call! */
1250
Harald Welteb83d9382009-12-12 21:00:48 +01001251 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +01001252 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +01001253
Harald Welte2a139372009-02-22 21:14:55 +00001254 return 0;
1255}
1256
Harald Welted2a7f5a2009-06-05 20:08:20 +00001257static int gsm48_rx_mm_status(struct msgb *msg)
1258{
1259 struct gsm48_hdr *gh = msgb_l3(msg);
1260
1261 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1262
1263 return 0;
1264}
1265
Harald Weltebf5e8df2009-02-03 12:59:45 +00001266/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001267static int gsm0408_rcv_mm(struct msgb *msg)
1268{
1269 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001270 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001271
1272 switch (gh->msg_type & 0xbf) {
1273 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001274 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +00001275 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001276 break;
1277 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +00001278 rc = mm_rx_id_resp(msg);
1279 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001280 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +00001281 rc = gsm48_rx_mm_serv_req(msg);
1282 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001283 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001284 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001285 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001286 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001287 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1288 msg->lchan->subscr ?
Harald Welte2e6d4682009-12-24 14:50:24 +01001289 subscr_name(msg->lchan->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001290 "unknown subscriber");
1291 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001292 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001293 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1294 break;
1295 case GSM48_MT_MM_CM_REEST_REQ:
1296 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1297 break;
1298 case GSM48_MT_MM_AUTH_RESP:
1299 DEBUGP(DMM, "AUTHENTICATION RESPONSE: Not implemented\n");
Harald Welte52b1f982008-12-23 20:25:15 +00001300 break;
1301 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001302 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001303 gh->msg_type);
1304 break;
1305 }
1306
1307 return rc;
1308}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001309
Harald Welte2d35ae62009-02-06 12:02:13 +00001310/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001311static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001312{
Harald Welte9176bd42009-07-23 18:46:00 +02001313 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001314 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte61548982009-02-22 21:26:29 +00001315 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001316 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001317 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001319 int rc = 0;
1320
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001321 gsm48_paging_extract_mi(msg, mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001322 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1323 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001324
Harald Weltefe18d8f2009-02-22 21:14:24 +00001325 switch (mi_type) {
1326 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001327 subscr = subscr_get_by_tmsi(bts->network,
1328 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001329 break;
1330 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001331 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001332 break;
1333 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001334
1335 if (!subscr) {
1336 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001337 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001338 return -EINVAL;
1339 }
1340 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001341 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001342
Harald Weltec2e302d2009-07-05 14:08:13 +02001343 subscr->equipment.classmark2_len = *classmark2_lv;
1344 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1345 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001346
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001347 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001348 return rc;
1349}
1350
Harald Weltef7c43522009-06-09 20:24:21 +00001351static int gsm48_rx_rr_classmark(struct msgb *msg)
1352{
1353 struct gsm48_hdr *gh = msgb_l3(msg);
1354 struct gsm_subscriber *subscr = msg->lchan->subscr;
1355 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1356 u_int8_t cm2_len, cm3_len = 0;
1357 u_int8_t *cm2, *cm3 = NULL;
1358
1359 DEBUGP(DRR, "CLASSMARK CHANGE ");
1360
1361 /* classmark 2 */
1362 cm2_len = gh->data[0];
1363 cm2 = &gh->data[1];
1364 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1365
1366 if (payload_len > cm2_len + 1) {
1367 /* we must have a classmark3 */
1368 if (gh->data[cm2_len+1] != 0x20) {
1369 DEBUGPC(DRR, "ERR CM3 TAG\n");
1370 return -EINVAL;
1371 }
1372 if (cm2_len > 3) {
1373 DEBUGPC(DRR, "CM2 too long!\n");
1374 return -EINVAL;
1375 }
1376
1377 cm3_len = gh->data[cm2_len+2];
1378 cm3 = &gh->data[cm2_len+3];
1379 if (cm3_len > 14) {
1380 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1381 return -EINVAL;
1382 }
1383 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1384 }
1385 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001386 subscr->equipment.classmark2_len = cm2_len;
1387 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001388 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001389 subscr->equipment.classmark3_len = cm3_len;
1390 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001391 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001392 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001393 }
1394
Harald Weltef7c43522009-06-09 20:24:21 +00001395 return 0;
1396}
1397
Harald Weltecf5b3592009-05-01 18:28:42 +00001398static int gsm48_rx_rr_status(struct msgb *msg)
1399{
1400 struct gsm48_hdr *gh = msgb_l3(msg);
1401
1402 DEBUGP(DRR, "STATUS rr_cause = %s\n",
1403 rr_cause_name(gh->data[0]));
1404
1405 return 0;
1406}
1407
Harald Weltef7c43522009-06-09 20:24:21 +00001408static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1409{
Harald Welted12b0fd2009-12-15 21:36:05 +01001410 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001411
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001412 /* This shouldn't actually end up here, as RSL treats
1413 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1414 * directly into gsm48_parse_meas_rep */
1415 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001416 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001417
1418 return 0;
1419}
1420
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001421static int gsm48_rx_rr_app_info(struct msgb *msg)
1422{
1423 struct gsm48_hdr *gh = msgb_l3(msg);
1424 u_int8_t apdu_id_flags;
1425 u_int8_t apdu_len;
1426 u_int8_t *apdu_data;
1427
1428 apdu_id_flags = gh->data[0];
1429 apdu_len = gh->data[1];
1430 apdu_data = gh->data+2;
1431
1432 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1433 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1434
Harald Welte (local)026531e2009-08-16 10:40:10 +02001435 return db_apdu_blob_store(msg->lchan->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001436}
1437
Harald Welted011e8b2009-11-29 22:45:52 +01001438/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001439static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001440{
1441 struct gsm48_hdr *gh = msgb_l3(msg);
1442
1443 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1444 rr_cause_name(gh->data[0]));
1445
1446 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1447 /* FIXME: release old channel */
1448
1449 return 0;
1450}
1451
1452/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001453static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001454{
1455 struct gsm48_hdr *gh = msgb_l3(msg);
1456
1457 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1458 rr_cause_name(gh->data[0]));
1459
1460 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1461 /* FIXME: release allocated new channel */
1462
1463 return 0;
1464}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001465
Harald Weltebf5e8df2009-02-03 12:59:45 +00001466/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001467static int gsm0408_rcv_rr(struct msgb *msg)
1468{
1469 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001470 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001471
1472 switch (gh->msg_type) {
1473 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001474 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001475 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001476 case GSM48_MT_RR_GPRS_SUSP_REQ:
1477 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1478 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001479 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001480 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001481 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001482 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001483 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001484 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001485 case GSM48_MT_RR_STATUS:
1486 rc = gsm48_rx_rr_status(msg);
1487 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001488 case GSM48_MT_RR_MEAS_REP:
1489 rc = gsm48_rx_rr_meas_rep(msg);
1490 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001491 case GSM48_MT_RR_APP_INFO:
1492 rc = gsm48_rx_rr_app_info(msg);
1493 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001494 case GSM48_MT_RR_CIPH_M_COMPL:
1495 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1496 /* FIXME: check for MI (if any) */
1497 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001498 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001499 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001500 break;
1501 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001502 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001503 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001504 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001505 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1506 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001507 break;
1508 }
1509
Harald Welte2d35ae62009-02-06 12:02:13 +00001510 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001511}
1512
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001513int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001514 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001515{
1516 struct msgb *msg = gsm48_msgb_alloc();
1517 struct gsm48_hdr *gh;
1518
1519 msg->lchan = lchan;
1520
1521 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1522 apdu_id, apdu_len);
1523
1524 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1525 gh->proto_discr = GSM48_PDISC_RR;
1526 gh->msg_type = GSM48_MT_RR_APP_INFO;
1527 gh->data[0] = apdu_id;
1528 gh->data[1] = apdu_len;
1529 memcpy(gh->data+2, apdu, apdu_len);
1530
1531 return gsm48_sendmsg(msg, NULL);
1532}
1533
Harald Welte4bc90a12008-12-27 16:32:52 +00001534/* Call Control */
1535
Harald Welte7584aea2009-02-11 11:44:12 +00001536/* The entire call control code is written in accordance with Figure 7.10c
1537 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1538 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1539 * it for voice */
1540
Harald Welte4bfdfe72009-06-10 23:11:52 +08001541static void new_cc_state(struct gsm_trans *trans, int state)
1542{
1543 if (state > 31 || state < 0)
1544 return;
1545
1546 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltedcaf5652009-07-23 18:56:43 +02001547 cc_state_names[trans->cc.state], cc_state_names[state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001548
Harald Weltedcaf5652009-07-23 18:56:43 +02001549 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001550}
1551
1552static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001553{
1554 struct msgb *msg = gsm48_msgb_alloc();
1555 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1556 u_int8_t *cause, *call_state;
1557
Harald Welte4bc90a12008-12-27 16:32:52 +00001558 gh->msg_type = GSM48_MT_CC_STATUS;
1559
1560 cause = msgb_put(msg, 3);
1561 cause[0] = 2;
1562 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1563 cause[2] = 0x80 | 30; /* response to status inquiry */
1564
1565 call_state = msgb_put(msg, 1);
1566 call_state[0] = 0xc0 | 0x00;
1567
Harald Welte39e2ead2009-07-23 21:13:03 +02001568 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001569}
1570
Harald Welte6f4b7532008-12-29 00:39:37 +00001571static int gsm48_tx_simple(struct gsm_lchan *lchan,
1572 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001573{
1574 struct msgb *msg = gsm48_msgb_alloc();
1575 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1576
1577 msg->lchan = lchan;
1578
Harald Welte6f4b7532008-12-29 00:39:37 +00001579 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001580 gh->msg_type = msg_type;
1581
Harald Welte39e2ead2009-07-23 21:13:03 +02001582 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001583}
1584
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1586{
Harald Weltedcaf5652009-07-23 18:56:43 +02001587 if (bsc_timer_pending(&trans->cc.timer)) {
1588 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1589 bsc_del_timer(&trans->cc.timer);
1590 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001591 }
1592}
1593
1594static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1595 int msg_type, struct gsm_mncc *mncc)
1596{
1597 struct msgb *msg;
1598
1599 if (trans)
1600 if (trans->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001601 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001602 "Sending '%s' to MNCC.\n",
1603 trans->lchan->ts->trx->bts->nr,
1604 trans->lchan->ts->trx->nr,
1605 trans->lchan->ts->nr, trans->transaction_id,
1606 (trans->subscr)?(trans->subscr->extension):"-",
1607 get_mncc_name(msg_type));
1608 else
1609 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1610 "Sending '%s' to MNCC.\n",
1611 (trans->subscr)?(trans->subscr->extension):"-",
1612 get_mncc_name(msg_type));
1613 else
1614 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1615 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1616
1617 mncc->msg_type = msg_type;
1618
Harald Welte966636f2009-06-26 19:39:35 +02001619 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001620 if (!msg)
1621 return -ENOMEM;
1622 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1623 msgb_enqueue(&net->upqueue, msg);
1624
1625 return 0;
1626}
1627
1628int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1629 u_int32_t callref, int location, int value)
1630{
1631 struct gsm_mncc rel;
1632
Harald Welte92f70c52009-06-12 01:54:08 +08001633 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001635 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001636 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1637}
1638
Harald Weltedcaf5652009-07-23 18:56:43 +02001639/* Call Control Specific transaction release.
1640 * gets called by trans_free, DO NOT CALL YOURSELF! */
1641void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001643 gsm48_stop_cc_timer(trans);
1644
1645 /* send release to L4, if callref still exists */
1646 if (trans->callref) {
1647 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001648 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001649 GSM48_CAUSE_LOC_PRN_S_LU,
1650 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001652 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Weltedcaf5652009-07-23 18:56:43 +02001654 if (trans->lchan)
1655 trau_mux_unmap(&trans->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656}
1657
1658static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
1659
Harald Welte09e38af2009-02-16 22:52:23 +00001660/* call-back from paging the B-end of the connection */
1661static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001662 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001663{
Harald Welte7ccf7782009-02-17 01:43:01 +00001664 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665 struct gsm_subscriber *subscr = param;
1666 struct gsm_trans *transt, *tmp;
1667 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001668
Harald Welte09e38af2009-02-16 22:52:23 +00001669 if (hooknum != GSM_HOOK_RR_PAGING)
1670 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671
1672 if (!subscr)
1673 return -EINVAL;
1674 net = subscr->net;
1675 if (!net) {
1676 DEBUGP(DCC, "Error Network not set!\n");
1677 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001678 }
Harald Welte7584aea2009-02-11 11:44:12 +00001679
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 /* check all tranactions (without lchan) for subscriber */
1681 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
1682 if (transt->subscr != subscr || transt->lchan)
1683 continue;
1684 switch (event) {
1685 case GSM_PAGING_SUCCEEDED:
1686 if (!lchan) // paranoid
1687 break;
1688 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1689 subscr->extension);
1690 /* Assign lchan */
1691 if (!transt->lchan) {
1692 transt->lchan = lchan;
1693 use_lchan(lchan);
1694 }
1695 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001696 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 break;
1698 case GSM_PAGING_EXPIRED:
1699 DEBUGP(DCC, "Paging subscr %s expired!\n",
1700 subscr->extension);
1701 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001702 mncc_release_ind(transt->subscr->net, transt,
1703 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001704 GSM48_CAUSE_LOC_PRN_S_LU,
1705 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001707 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 break;
1709 }
1710 }
Harald Welte09e38af2009-02-16 22:52:23 +00001711 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001712}
Harald Welte7584aea2009-02-11 11:44:12 +00001713
Harald Welteda7ab742009-12-19 22:23:05 +01001714static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1715
Harald Welte805f6442009-07-28 18:25:29 +02001716/* some other part of the code sends us a signal */
1717static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1718 void *handler_data, void *signal_data)
1719{
1720 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001721 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001722 struct gsm_network *net;
1723 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001724
1725 if (subsys != SS_ABISIP)
1726 return 0;
1727
1728 /* in case we use direct BTS-to-BTS RTP */
1729 if (ipacc_rtp_direct)
1730 return 0;
1731
Harald Welte805f6442009-07-28 18:25:29 +02001732 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001733 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001734 /* check if any transactions on this lchan still have
1735 * a tch_recv_mncc request pending */
1736 net = lchan->ts->trx->bts->network;
1737 llist_for_each_entry(trans, &net->trans_list, entry) {
1738 if (trans->lchan == lchan && trans->tch_recv) {
1739 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1740 tch_recv_mncc(net, trans->callref, 1);
1741 }
1742 }
Harald Welte805f6442009-07-28 18:25:29 +02001743 break;
Harald Welte805f6442009-07-28 18:25:29 +02001744 }
1745
1746 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001747}
1748
Harald Welte49f48b82009-02-17 15:29:33 +00001749/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001750static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001751{
Harald Welte11fa29c2009-02-19 17:24:39 +00001752 struct gsm_bts *bts = lchan->ts->trx->bts;
1753 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001754 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001755
Harald Welte11fa29c2009-02-19 17:24:39 +00001756 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1757 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1758 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1759
1760 if (bts->type != remote_bts->type) {
1761 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1762 return -EINVAL;
1763 }
Harald Welteda7ab742009-12-19 22:23:05 +01001764
1765 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001766 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001767 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001768 if (!ipacc_rtp_direct) {
1769 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001770 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001771 if (rc < 0)
1772 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001773 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001774#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001775
1776 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301777 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1778 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001779 } else {
1780 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301781 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1782 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301783 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001784 if (rc < 0)
1785 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301786 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1787 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301788 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001789 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001790 break;
1791 case GSM_BTS_TYPE_BS11:
1792 trau_mux_map_lchan(lchan, remote_lchan);
1793 break;
1794 default:
1795 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001796 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001797 }
Harald Welte49f48b82009-02-17 15:29:33 +00001798
1799 return 0;
1800}
1801
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802/* bridge channels of two transactions */
1803static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001804{
Harald Weltedcaf5652009-07-23 18:56:43 +02001805 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1806 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001807
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001809 return -EIO;
1810
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 if (!trans1->lchan || !trans2->lchan)
1812 return -EIO;
1813
1814 /* through-connect channel */
1815 return tch_map(trans1->lchan, trans2->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001816}
1817
Harald Welteda7ab742009-12-19 22:23:05 +01001818/* enable receive of channels to MNCC upqueue */
1819static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820{
1821 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001822 struct gsm_lchan *lchan;
1823 struct gsm_bts *bts;
1824 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001825
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001827 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001828 if (!trans)
1829 return -EIO;
1830 if (!trans->lchan)
1831 return 0;
Harald Welteda7ab742009-12-19 22:23:05 +01001832 lchan = trans->lchan;
1833 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834
Harald Welteda7ab742009-12-19 22:23:05 +01001835 switch (bts->type) {
1836 case GSM_BTS_TYPE_NANOBTS:
1837 if (ipacc_rtp_direct) {
1838 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1839 return -EINVAL;
1840 }
1841 /* in case, we don't have a RTP socket yet, we note this
1842 * in the transaction and try later */
1843 if (!lchan->abis_ip.rtp_socket) {
1844 trans->tch_recv = enable;
1845 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1846 return 0;
1847 }
1848 if (enable) {
1849 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001850 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001851 if (rc < 0)
1852 return rc;
1853 /* assign socket to application interface */
1854 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1855 net, callref);
1856 } else
1857 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1858 net, 0);
1859 break;
1860 case GSM_BTS_TYPE_BS11:
1861 if (enable)
1862 return trau_recv_lchan(lchan, callref);
1863 return trau_mux_unmap(NULL, callref);
1864 break;
1865 default:
1866 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1867 return -EINVAL;
1868 }
1869
1870 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871}
1872
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1874{
1875 DEBUGP(DCC, "-> STATUS ENQ\n");
1876 return gsm48_cc_tx_status(trans, msg);
1877}
1878
1879static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1880static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1881
1882static void gsm48_cc_timeout(void *arg)
1883{
1884 struct gsm_trans *trans = arg;
1885 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001886 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1887 int mo_location = GSM48_CAUSE_LOC_USER;
1888 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1889 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 struct gsm_mncc mo_rel, l4_rel;
1891
1892 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1893 mo_rel.callref = trans->callref;
1894 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1895 l4_rel.callref = trans->callref;
1896
Harald Weltedcaf5652009-07-23 18:56:43 +02001897 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898 case 0x303:
1899 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001900 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 break;
1902 case 0x310:
1903 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001904 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 break;
1906 case 0x313:
1907 disconnect = 1;
1908 /* unknown, did not find it in the specs */
1909 break;
1910 case 0x301:
1911 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001912 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913 break;
1914 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001915 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001917 gsm48_cc_tx_release(trans, &trans->cc.msg);
1918 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 break; /* stay in release state */
1920 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001921 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 return;
1923// release = 1;
1924// l4_cause = 14;
1925// break;
1926 case 0x306:
1927 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001928 mo_cause = trans->cc.msg.cause.value;
1929 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930 break;
1931 case 0x323:
1932 disconnect = 1;
1933 break;
1934 default:
1935 release = 1;
1936 }
1937
1938 if (release && trans->callref) {
1939 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001940 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 l4_location, l4_cause);
1942 trans->callref = 0;
1943 }
1944
1945 if (disconnect && trans->callref) {
1946 /* process disconnect towards layer 4 */
1947 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001948 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 }
1950
1951 /* process disconnect towards mobile station */
1952 if (disconnect || release) {
1953 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001954 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1955 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1956 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 mo_rel.cause.diag_len = 3;
1958
1959 if (disconnect)
1960 gsm48_cc_tx_disconnect(trans, &mo_rel);
1961 if (release)
1962 gsm48_cc_tx_release(trans, &mo_rel);
1963 }
1964
1965}
1966
1967static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1968 int sec, int micro)
1969{
1970 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001971 trans->cc.timer.cb = gsm48_cc_timeout;
1972 trans->cc.timer.data = trans;
1973 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1974 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975}
1976
1977static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1978{
1979 struct gsm48_hdr *gh = msgb_l3(msg);
1980 u_int8_t msg_type = gh->msg_type & 0xbf;
1981 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1982 struct tlv_parsed tp;
1983 struct gsm_mncc setup;
1984
1985 memset(&setup, 0, sizeof(struct gsm_mncc));
1986 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001987 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* emergency setup is identified by msg_type */
1989 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1990 setup.emergency = 1;
1991
1992 /* use subscriber as calling party number */
1993 if (trans->subscr) {
1994 setup.fields |= MNCC_F_CALLING;
1995 strncpy(setup.calling.number, trans->subscr->extension,
1996 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001997 strncpy(setup.imsi, trans->subscr->imsi,
1998 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 }
2000 /* bearer capability */
2001 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2002 setup.fields |= MNCC_F_BEARER_CAP;
2003 decode_bearer_cap(&setup.bearer_cap,
2004 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2005 }
2006 /* facility */
2007 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2008 setup.fields |= MNCC_F_FACILITY;
2009 decode_facility(&setup.facility,
2010 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2011 }
2012 /* called party bcd number */
2013 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
2014 setup.fields |= MNCC_F_CALLED;
2015 decode_called(&setup.called,
2016 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
2017 }
2018 /* user-user */
2019 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2020 setup.fields |= MNCC_F_USERUSER;
2021 decode_useruser(&setup.useruser,
2022 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2023 }
2024 /* ss-version */
2025 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2026 setup.fields |= MNCC_F_SSVERSION;
2027 decode_ssversion(&setup.ssversion,
2028 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2029 }
2030 /* CLIR suppression */
2031 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
2032 setup.clir.sup = 1;
2033 /* CLIR invocation */
2034 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
2035 setup.clir.inv = 1;
2036 /* cc cap */
2037 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2038 setup.fields |= MNCC_F_CCCAP;
2039 decode_cccap(&setup.cccap,
2040 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2041 }
2042
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 new_cc_state(trans, GSM_CSTATE_INITIATED);
2044
Harald Welte (local)47df3992009-12-26 19:45:03 +01002045 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
2046 subscr_name(trans->subscr), trans->subscr->extension,
2047 setup.called.number);
2048
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02002050 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051
Harald Welte13cac662009-07-29 12:10:35 +02002052 /* MNCC code will modify the channel asynchronously, we should
2053 * ipaccess-bind only after the modification has been made to the
2054 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 return 0;
2056}
2057
2058static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00002059{
2060 struct msgb *msg = gsm48_msgb_alloc();
2061 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02002063 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00002064
Harald Welte7ccf7782009-02-17 01:43:01 +00002065 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00002066
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 /* transaction id must not be assigned */
2068 if (trans->transaction_id != 0xff) { /* unasssigned */
2069 DEBUGP(DCC, "TX Setup with assigned transaction. "
2070 "This is not allowed!\n");
2071 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02002072 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002073 GSM48_CAUSE_LOC_PRN_S_LU,
2074 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002076 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 return rc;
2078 }
2079
2080 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02002081 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
2082 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02002084 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002085 GSM48_CAUSE_LOC_PRN_S_LU,
2086 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002088 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 return rc;
2090 }
Harald Welte78283ef2009-07-23 21:36:44 +02002091 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00002092
Harald Welte65e74cc2008-12-29 01:55:35 +00002093 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00002094
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00002096
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 /* bearer capability */
2098 if (setup->fields & MNCC_F_BEARER_CAP)
2099 encode_bearer_cap(msg, 0, &setup->bearer_cap);
2100 /* facility */
2101 if (setup->fields & MNCC_F_FACILITY)
2102 encode_facility(msg, 0, &setup->facility);
2103 /* progress */
2104 if (setup->fields & MNCC_F_PROGRESS)
2105 encode_progress(msg, 0, &setup->progress);
2106 /* calling party BCD number */
2107 if (setup->fields & MNCC_F_CALLING)
2108 encode_calling(msg, &setup->calling);
2109 /* called party BCD number */
2110 if (setup->fields & MNCC_F_CALLED)
2111 encode_called(msg, &setup->called);
2112 /* user-user */
2113 if (setup->fields & MNCC_F_USERUSER)
2114 encode_useruser(msg, 0, &setup->useruser);
2115 /* redirecting party BCD number */
2116 if (setup->fields & MNCC_F_REDIRECTING)
2117 encode_redirecting(msg, &setup->redirecting);
2118 /* signal */
2119 if (setup->fields & MNCC_F_SIGNAL)
2120 encode_signal(msg, setup->signal);
2121
2122 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002123
Harald Welte39e2ead2009-07-23 21:13:03 +02002124 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002125}
2126
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2128{
2129 struct gsm48_hdr *gh = msgb_l3(msg);
2130 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2131 struct tlv_parsed tp;
2132 struct gsm_mncc call_conf;
2133
2134 gsm48_stop_cc_timer(trans);
2135 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2136
2137 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2138 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002139 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140#if 0
2141 /* repeat */
2142 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2143 call_conf.repeat = 1;
2144 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2145 call_conf.repeat = 2;
2146#endif
2147 /* bearer capability */
2148 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2149 call_conf.fields |= MNCC_F_BEARER_CAP;
2150 decode_bearer_cap(&call_conf.bearer_cap,
2151 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2152 }
2153 /* cause */
2154 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2155 call_conf.fields |= MNCC_F_CAUSE;
2156 decode_cause(&call_conf.cause,
2157 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2158 }
2159 /* cc cap */
2160 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2161 call_conf.fields |= MNCC_F_CCCAP;
2162 decode_cccap(&call_conf.cccap,
2163 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2164 }
2165
2166 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2167
Harald Welte596fed42009-07-23 19:06:52 +02002168 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2169 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170}
2171
2172static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2173{
2174 struct gsm_mncc *proceeding = arg;
2175 struct msgb *msg = gsm48_msgb_alloc();
2176 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2177
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2179
2180 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2181
2182 /* bearer capability */
2183 if (proceeding->fields & MNCC_F_BEARER_CAP)
2184 encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
2185 /* facility */
2186 if (proceeding->fields & MNCC_F_FACILITY)
2187 encode_facility(msg, 0, &proceeding->facility);
2188 /* progress */
2189 if (proceeding->fields & MNCC_F_PROGRESS)
2190 encode_progress(msg, 0, &proceeding->progress);
2191
Harald Welte39e2ead2009-07-23 21:13:03 +02002192 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193}
2194
2195static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2196{
2197 struct gsm48_hdr *gh = msgb_l3(msg);
2198 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2199 struct tlv_parsed tp;
2200 struct gsm_mncc alerting;
2201
2202 gsm48_stop_cc_timer(trans);
2203 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2204
2205 memset(&alerting, 0, sizeof(struct gsm_mncc));
2206 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002207 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 /* facility */
2209 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2210 alerting.fields |= MNCC_F_FACILITY;
2211 decode_facility(&alerting.facility,
2212 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2213 }
2214
2215 /* progress */
2216 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2217 alerting.fields |= MNCC_F_PROGRESS;
2218 decode_progress(&alerting.progress,
2219 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2220 }
2221 /* ss-version */
2222 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2223 alerting.fields |= MNCC_F_SSVERSION;
2224 decode_ssversion(&alerting.ssversion,
2225 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2226 }
2227
2228 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2229
Harald Welte596fed42009-07-23 19:06:52 +02002230 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2231 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232}
2233
2234static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2235{
2236 struct gsm_mncc *alerting = arg;
2237 struct msgb *msg = gsm48_msgb_alloc();
2238 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2239
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 gh->msg_type = GSM48_MT_CC_ALERTING;
2241
2242 /* facility */
2243 if (alerting->fields & MNCC_F_FACILITY)
2244 encode_facility(msg, 0, &alerting->facility);
2245 /* progress */
2246 if (alerting->fields & MNCC_F_PROGRESS)
2247 encode_progress(msg, 0, &alerting->progress);
2248 /* user-user */
2249 if (alerting->fields & MNCC_F_USERUSER)
2250 encode_useruser(msg, 0, &alerting->useruser);
2251
2252 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2253
Harald Welte39e2ead2009-07-23 21:13:03 +02002254 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255}
2256
2257static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2258{
2259 struct gsm_mncc *progress = arg;
2260 struct msgb *msg = gsm48_msgb_alloc();
2261 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2262
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 gh->msg_type = GSM48_MT_CC_PROGRESS;
2264
2265 /* progress */
2266 encode_progress(msg, 1, &progress->progress);
2267 /* user-user */
2268 if (progress->fields & MNCC_F_USERUSER)
2269 encode_useruser(msg, 0, &progress->useruser);
2270
Harald Welte39e2ead2009-07-23 21:13:03 +02002271 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272}
2273
2274static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2275{
2276 struct gsm_mncc *connect = arg;
2277 struct msgb *msg = gsm48_msgb_alloc();
2278 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2279
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 gh->msg_type = GSM48_MT_CC_CONNECT;
2281
2282 gsm48_stop_cc_timer(trans);
2283 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2284
2285 /* facility */
2286 if (connect->fields & MNCC_F_FACILITY)
2287 encode_facility(msg, 0, &connect->facility);
2288 /* progress */
2289 if (connect->fields & MNCC_F_PROGRESS)
2290 encode_progress(msg, 0, &connect->progress);
2291 /* connected number */
2292 if (connect->fields & MNCC_F_CONNECTED)
2293 encode_connected(msg, &connect->connected);
2294 /* user-user */
2295 if (connect->fields & MNCC_F_USERUSER)
2296 encode_useruser(msg, 0, &connect->useruser);
2297
2298 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2299
Harald Welte39e2ead2009-07-23 21:13:03 +02002300 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301}
2302
2303static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2304{
2305 struct gsm48_hdr *gh = msgb_l3(msg);
2306 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2307 struct tlv_parsed tp;
2308 struct gsm_mncc connect;
2309
2310 gsm48_stop_cc_timer(trans);
2311
2312 memset(&connect, 0, sizeof(struct gsm_mncc));
2313 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002314 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 /* use subscriber as connected party number */
2316 if (trans->subscr) {
2317 connect.fields |= MNCC_F_CONNECTED;
2318 strncpy(connect.connected.number, trans->subscr->extension,
2319 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002320 strncpy(connect.imsi, trans->subscr->imsi,
2321 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 }
2323 /* facility */
2324 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2325 connect.fields |= MNCC_F_FACILITY;
2326 decode_facility(&connect.facility,
2327 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2328 }
2329 /* user-user */
2330 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2331 connect.fields |= MNCC_F_USERUSER;
2332 decode_useruser(&connect.useruser,
2333 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2334 }
2335 /* ss-version */
2336 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2337 connect.fields |= MNCC_F_SSVERSION;
2338 decode_ssversion(&connect.ssversion,
2339 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2340 }
2341
2342 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2343
Harald Welte596fed42009-07-23 19:06:52 +02002344 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345}
2346
2347
2348static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2349{
2350 struct gsm_mncc connect_ack;
2351
2352 gsm48_stop_cc_timer(trans);
2353
2354 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2355
2356 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2357 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002358 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 &connect_ack);
2360}
2361
2362static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2363{
2364 struct msgb *msg = gsm48_msgb_alloc();
2365 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2366
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2368
2369 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2370
Harald Welte39e2ead2009-07-23 21:13:03 +02002371 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372}
2373
2374static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2375{
2376 struct gsm48_hdr *gh = msgb_l3(msg);
2377 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2378 struct tlv_parsed tp;
2379 struct gsm_mncc disc;
2380
2381 gsm48_stop_cc_timer(trans);
2382
2383 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2384
2385 memset(&disc, 0, sizeof(struct gsm_mncc));
2386 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002387 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 /* cause */
2389 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2390 disc.fields |= MNCC_F_CAUSE;
2391 decode_cause(&disc.cause,
2392 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2393 }
2394 /* facility */
2395 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2396 disc.fields |= MNCC_F_FACILITY;
2397 decode_facility(&disc.facility,
2398 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2399 }
2400 /* user-user */
2401 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2402 disc.fields |= MNCC_F_USERUSER;
2403 decode_useruser(&disc.useruser,
2404 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2405 }
2406 /* ss-version */
2407 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2408 disc.fields |= MNCC_F_SSVERSION;
2409 decode_ssversion(&disc.ssversion,
2410 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2411 }
2412
Harald Welte596fed42009-07-23 19:06:52 +02002413 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414
2415}
2416
Harald Weltec66b71c2009-06-11 14:23:20 +08002417static struct gsm_mncc_cause default_cause = {
2418 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2419 .coding = 0,
2420 .rec = 0,
2421 .rec_val = 0,
2422 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2423 .diag_len = 0,
2424 .diag = { 0 },
2425};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426
2427static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *disc = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2434
2435 gsm48_stop_cc_timer(trans);
2436 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2437
2438 /* cause */
2439 if (disc->fields & MNCC_F_CAUSE)
2440 encode_cause(msg, 1, &disc->cause);
2441 else
2442 encode_cause(msg, 1, &default_cause);
2443
2444 /* facility */
2445 if (disc->fields & MNCC_F_FACILITY)
2446 encode_facility(msg, 0, &disc->facility);
2447 /* progress */
2448 if (disc->fields & MNCC_F_PROGRESS)
2449 encode_progress(msg, 0, &disc->progress);
2450 /* user-user */
2451 if (disc->fields & MNCC_F_USERUSER)
2452 encode_useruser(msg, 0, &disc->useruser);
2453
2454 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002455 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456
2457 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2458
Harald Welte39e2ead2009-07-23 21:13:03 +02002459 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460}
2461
2462static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2463{
2464 struct gsm48_hdr *gh = msgb_l3(msg);
2465 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2466 struct tlv_parsed tp;
2467 struct gsm_mncc rel;
2468 int rc;
2469
2470 gsm48_stop_cc_timer(trans);
2471
2472 memset(&rel, 0, sizeof(struct gsm_mncc));
2473 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002474 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 /* cause */
2476 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2477 rel.fields |= MNCC_F_CAUSE;
2478 decode_cause(&rel.cause,
2479 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2480 }
2481 /* facility */
2482 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2483 rel.fields |= MNCC_F_FACILITY;
2484 decode_facility(&rel.facility,
2485 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2486 }
2487 /* user-user */
2488 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2489 rel.fields |= MNCC_F_USERUSER;
2490 decode_useruser(&rel.useruser,
2491 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2492 }
2493 /* ss-version */
2494 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2495 rel.fields |= MNCC_F_SSVERSION;
2496 decode_ssversion(&rel.ssversion,
2497 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2498 }
2499
Harald Weltedcaf5652009-07-23 18:56:43 +02002500 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002502 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002504 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002505 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002506 GSM48_MT_CC_RELEASE_COMPL);
2507 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 }
2509
2510 new_cc_state(trans, GSM_CSTATE_NULL);
2511
2512 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002513 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514
2515 return rc;
2516}
2517
2518static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2519{
2520 struct gsm_mncc *rel = arg;
2521 struct msgb *msg = gsm48_msgb_alloc();
2522 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2523
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524 gh->msg_type = GSM48_MT_CC_RELEASE;
2525
2526 trans->callref = 0;
2527
2528 gsm48_stop_cc_timer(trans);
2529 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2530
2531 /* cause */
2532 if (rel->fields & MNCC_F_CAUSE)
2533 encode_cause(msg, 0, &rel->cause);
2534 /* facility */
2535 if (rel->fields & MNCC_F_FACILITY)
2536 encode_facility(msg, 0, &rel->facility);
2537 /* user-user */
2538 if (rel->fields & MNCC_F_USERUSER)
2539 encode_useruser(msg, 0, &rel->useruser);
2540
Harald Weltedcaf5652009-07-23 18:56:43 +02002541 trans->cc.T308_second = 0;
2542 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543
Harald Weltedcaf5652009-07-23 18:56:43 +02002544 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2546
Harald Welte39e2ead2009-07-23 21:13:03 +02002547 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548}
2549
2550static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2551{
2552 struct gsm48_hdr *gh = msgb_l3(msg);
2553 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2554 struct tlv_parsed tp;
2555 struct gsm_mncc rel;
2556 int rc = 0;
2557
2558 gsm48_stop_cc_timer(trans);
2559
2560 memset(&rel, 0, sizeof(struct gsm_mncc));
2561 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002562 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 /* cause */
2564 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2565 rel.fields |= MNCC_F_CAUSE;
2566 decode_cause(&rel.cause,
2567 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2568 }
2569 /* facility */
2570 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2571 rel.fields |= MNCC_F_FACILITY;
2572 decode_facility(&rel.facility,
2573 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2574 }
2575 /* user-user */
2576 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2577 rel.fields |= MNCC_F_USERUSER;
2578 decode_useruser(&rel.useruser,
2579 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2580 }
2581 /* ss-version */
2582 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2583 rel.fields |= MNCC_F_SSVERSION;
2584 decode_ssversion(&rel.ssversion,
2585 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2586 }
2587
2588 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002589 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002591 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002592 MNCC_REJ_IND, &rel);
2593 break;
2594 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002595 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002597 /* FIXME: in case of multiple calls, we can't simply
2598 * hang up here ! */
2599 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600 break;
2601 default:
Harald Welte596fed42009-07-23 19:06:52 +02002602 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 MNCC_REL_IND, &rel);
2604 }
2605 }
2606
2607 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002608 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609
2610 return rc;
2611}
2612
2613static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2614{
2615 struct gsm_mncc *rel = arg;
2616 struct msgb *msg = gsm48_msgb_alloc();
2617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2618
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2620
2621 trans->callref = 0;
2622
2623 gsm48_stop_cc_timer(trans);
2624
2625 /* cause */
2626 if (rel->fields & MNCC_F_CAUSE)
2627 encode_cause(msg, 0, &rel->cause);
2628 /* facility */
2629 if (rel->fields & MNCC_F_FACILITY)
2630 encode_facility(msg, 0, &rel->facility);
2631 /* user-user */
2632 if (rel->fields & MNCC_F_USERUSER)
2633 encode_useruser(msg, 0, &rel->useruser);
2634
Harald Weltedcaf5652009-07-23 18:56:43 +02002635 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002636
Harald Welte39e2ead2009-07-23 21:13:03 +02002637 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638}
2639
2640static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2641{
2642 struct gsm48_hdr *gh = msgb_l3(msg);
2643 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2644 struct tlv_parsed tp;
2645 struct gsm_mncc fac;
2646
2647 memset(&fac, 0, sizeof(struct gsm_mncc));
2648 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002649 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650 /* facility */
2651 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2652 fac.fields |= MNCC_F_FACILITY;
2653 decode_facility(&fac.facility,
2654 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2655 }
2656 /* ss-version */
2657 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2658 fac.fields |= MNCC_F_SSVERSION;
2659 decode_ssversion(&fac.ssversion,
2660 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2661 }
2662
Harald Welte596fed42009-07-23 19:06:52 +02002663 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664}
2665
2666static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2667{
2668 struct gsm_mncc *fac = arg;
2669 struct msgb *msg = gsm48_msgb_alloc();
2670 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2671
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672 gh->msg_type = GSM48_MT_CC_FACILITY;
2673
2674 /* facility */
2675 encode_facility(msg, 1, &fac->facility);
2676
Harald Welte39e2ead2009-07-23 21:13:03 +02002677 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678}
2679
2680static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2681{
2682 struct gsm_mncc hold;
2683
2684 memset(&hold, 0, sizeof(struct gsm_mncc));
2685 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002686 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687}
2688
2689static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2690{
2691 struct msgb *msg = gsm48_msgb_alloc();
2692 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2693
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2695
Harald Welte39e2ead2009-07-23 21:13:03 +02002696 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697}
2698
2699static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2700{
2701 struct gsm_mncc *hold_rej = arg;
2702 struct msgb *msg = gsm48_msgb_alloc();
2703 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2704
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2706
2707 /* cause */
2708 if (hold_rej->fields & MNCC_F_CAUSE)
2709 encode_cause(msg, 1, &hold_rej->cause);
2710 else
2711 encode_cause(msg, 1, &default_cause);
2712
Harald Welte39e2ead2009-07-23 21:13:03 +02002713 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714}
2715
2716static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2717{
2718 struct gsm_mncc retrieve;
2719
2720 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2721 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002722 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2723 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724}
2725
2726static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2727{
2728 struct msgb *msg = gsm48_msgb_alloc();
2729 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2730
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2732
Harald Welte39e2ead2009-07-23 21:13:03 +02002733 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734}
2735
2736static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2737{
2738 struct gsm_mncc *retrieve_rej = arg;
2739 struct msgb *msg = gsm48_msgb_alloc();
2740 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2741
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2743
2744 /* cause */
2745 if (retrieve_rej->fields & MNCC_F_CAUSE)
2746 encode_cause(msg, 1, &retrieve_rej->cause);
2747 else
2748 encode_cause(msg, 1, &default_cause);
2749
Harald Welte39e2ead2009-07-23 21:13:03 +02002750 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751}
2752
2753static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2754{
2755 struct gsm48_hdr *gh = msgb_l3(msg);
2756 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2757 struct tlv_parsed tp;
2758 struct gsm_mncc dtmf;
2759
2760 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2761 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002762 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 /* keypad facility */
2764 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2765 dtmf.fields |= MNCC_F_KEYPAD;
2766 decode_keypad(&dtmf.keypad,
2767 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2768 }
2769
Harald Welte596fed42009-07-23 19:06:52 +02002770 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002771}
2772
2773static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2774{
2775 struct gsm_mncc *dtmf = arg;
2776 struct msgb *msg = gsm48_msgb_alloc();
2777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2778
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2780
2781 /* keypad */
2782 if (dtmf->fields & MNCC_F_KEYPAD)
2783 encode_keypad(msg, dtmf->keypad);
2784
Harald Welte39e2ead2009-07-23 21:13:03 +02002785 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786}
2787
2788static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2789{
2790 struct gsm_mncc *dtmf = arg;
2791 struct msgb *msg = gsm48_msgb_alloc();
2792 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2793
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2795
2796 /* cause */
2797 if (dtmf->fields & MNCC_F_CAUSE)
2798 encode_cause(msg, 1, &dtmf->cause);
2799 else
2800 encode_cause(msg, 1, &default_cause);
2801
Harald Welte39e2ead2009-07-23 21:13:03 +02002802 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803}
2804
2805static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2806{
2807 struct msgb *msg = gsm48_msgb_alloc();
2808 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2809
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2811
Harald Welte39e2ead2009-07-23 21:13:03 +02002812 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813}
2814
2815static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2816{
2817 struct gsm_mncc dtmf;
2818
2819 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2820 dtmf.callref = trans->callref;
2821
Harald Welte596fed42009-07-23 19:06:52 +02002822 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823}
2824
2825static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2826{
2827 struct gsm48_hdr *gh = msgb_l3(msg);
2828 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2829 struct tlv_parsed tp;
2830 struct gsm_mncc modify;
2831
2832 memset(&modify, 0, sizeof(struct gsm_mncc));
2833 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002834 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 /* bearer capability */
2836 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2837 modify.fields |= MNCC_F_BEARER_CAP;
2838 decode_bearer_cap(&modify.bearer_cap,
2839 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2840 }
2841
2842 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2843
Harald Welte596fed42009-07-23 19:06:52 +02002844 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845}
2846
2847static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2848{
2849 struct gsm_mncc *modify = arg;
2850 struct msgb *msg = gsm48_msgb_alloc();
2851 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2852
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 gh->msg_type = GSM48_MT_CC_MODIFY;
2854
2855 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2856
2857 /* bearer capability */
2858 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2859
2860 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2861
Harald Welte39e2ead2009-07-23 21:13:03 +02002862 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863}
2864
2865static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2866{
2867 struct gsm48_hdr *gh = msgb_l3(msg);
2868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2869 struct tlv_parsed tp;
2870 struct gsm_mncc modify;
2871
2872 gsm48_stop_cc_timer(trans);
2873
2874 memset(&modify, 0, sizeof(struct gsm_mncc));
2875 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002876 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002877 /* bearer capability */
2878 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2879 modify.fields |= MNCC_F_BEARER_CAP;
2880 decode_bearer_cap(&modify.bearer_cap,
2881 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2882 }
2883
2884 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2885
Harald Welte596fed42009-07-23 19:06:52 +02002886 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887}
2888
2889static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2890{
2891 struct gsm_mncc *modify = arg;
2892 struct msgb *msg = gsm48_msgb_alloc();
2893 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2894
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2896
2897 /* bearer capability */
2898 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2899
2900 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2901
Harald Welte39e2ead2009-07-23 21:13:03 +02002902 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002903}
2904
2905static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2906{
2907 struct gsm48_hdr *gh = msgb_l3(msg);
2908 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2909 struct tlv_parsed tp;
2910 struct gsm_mncc modify;
2911
2912 gsm48_stop_cc_timer(trans);
2913
2914 memset(&modify, 0, sizeof(struct gsm_mncc));
2915 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002916 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002917 /* bearer capability */
2918 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2919 modify.fields |= GSM48_IE_BEARER_CAP;
2920 decode_bearer_cap(&modify.bearer_cap,
2921 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2922 }
2923 /* cause */
2924 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2925 modify.fields |= MNCC_F_CAUSE;
2926 decode_cause(&modify.cause,
2927 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2928 }
2929
2930 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2931
Harald Welte596fed42009-07-23 19:06:52 +02002932 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933}
2934
2935static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2936{
2937 struct gsm_mncc *modify = arg;
2938 struct msgb *msg = gsm48_msgb_alloc();
2939 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2940
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2942
2943 /* bearer capability */
2944 encode_bearer_cap(msg, 1, &modify->bearer_cap);
2945 /* cause */
2946 encode_cause(msg, 1, &modify->cause);
2947
2948 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2949
Harald Welte39e2ead2009-07-23 21:13:03 +02002950 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951}
2952
2953static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2954{
2955 struct gsm_mncc *notify = arg;
2956 struct msgb *msg = gsm48_msgb_alloc();
2957 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2958
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959 gh->msg_type = GSM48_MT_CC_NOTIFY;
2960
2961 /* notify */
2962 encode_notify(msg, notify->notify);
2963
Harald Welte39e2ead2009-07-23 21:13:03 +02002964 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965}
2966
2967static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2968{
2969 struct gsm48_hdr *gh = msgb_l3(msg);
2970 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2971// struct tlv_parsed tp;
2972 struct gsm_mncc notify;
2973
2974 memset(&notify, 0, sizeof(struct gsm_mncc));
2975 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002976// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977 if (payload_len >= 1)
2978 decode_notify(&notify.notify, gh->data);
2979
Harald Welte596fed42009-07-23 19:06:52 +02002980 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002981}
2982
2983static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2984{
2985 struct gsm_mncc *user = arg;
2986 struct msgb *msg = gsm48_msgb_alloc();
2987 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2988
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 gh->msg_type = GSM48_MT_CC_USER_INFO;
2990
2991 /* user-user */
2992 if (user->fields & MNCC_F_USERUSER)
2993 encode_useruser(msg, 1, &user->useruser);
2994 /* more data */
2995 if (user->more)
2996 encode_more(msg);
2997
Harald Welte39e2ead2009-07-23 21:13:03 +02002998 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002999}
3000
3001static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
3002{
3003 struct gsm48_hdr *gh = msgb_l3(msg);
3004 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
3005 struct tlv_parsed tp;
3006 struct gsm_mncc user;
3007
3008 memset(&user, 0, sizeof(struct gsm_mncc));
3009 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01003010 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 /* user-user */
3012 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
3013 user.fields |= MNCC_F_USERUSER;
3014 decode_useruser(&user.useruser,
3015 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
3016 }
3017 /* more data */
3018 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
3019 user.more = 1;
3020
Harald Welte596fed42009-07-23 19:06:52 +02003021 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022}
3023
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003024static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025{
3026 struct gsm_mncc *mode = arg;
3027
Holger Hans Peter Freytherea528022009-11-18 22:57:02 +01003028 return gsm48_lchan_modify(trans->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029}
3030
3031static struct downstate {
3032 u_int32_t states;
3033 int type;
3034 int (*rout) (struct gsm_trans *trans, void *arg);
3035} downstatelist[] = {
3036 /* mobile originating call establishment */
3037 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3038 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3039 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3040 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3041 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) | SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.2 | 5.2.1.6 | 5.2.1.6 */
3042 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3043 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3044 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3045 /* mobile terminating call establishment */
3046 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3047 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3048 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3049 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3050 /* signalling during call */
3051 {SBIT(GSM_CSTATE_ACTIVE),
3052 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3053 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3054 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3055 {ALL_STATES,
3056 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3057 {ALL_STATES,
3058 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3059 {ALL_STATES,
3060 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3061 {SBIT(GSM_CSTATE_ACTIVE),
3062 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3063 {SBIT(GSM_CSTATE_ACTIVE),
3064 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3065 {SBIT(GSM_CSTATE_ACTIVE),
3066 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3067 {SBIT(GSM_CSTATE_ACTIVE),
3068 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3069 {SBIT(GSM_CSTATE_ACTIVE),
3070 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3071 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3072 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3073 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3074 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3075 {SBIT(GSM_CSTATE_ACTIVE),
3076 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3077 /* clearing */
3078 {SBIT(GSM_CSTATE_INITIATED),
3079 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3080 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3081 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3082 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3083 MNCC_REL_REQ, gsm48_cc_tx_release},
3084 /* special */
3085 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003086 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087};
3088
3089#define DOWNSLLEN \
3090 (sizeof(downstatelist) / sizeof(struct downstate))
3091
3092
3093int mncc_send(struct gsm_network *net, int msg_type, void *arg)
3094{
Harald Welte1a6f7982009-08-09 18:52:33 +02003095 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02003097 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003098 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 struct gsm_mncc *data = arg, rel;
3100
3101 /* handle special messages */
3102 switch(msg_type) {
3103 case MNCC_BRIDGE:
3104 return tch_bridge(net, arg);
3105 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003106 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003107 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003108 return tch_recv_mncc(net, data->callref, 1);
3109 case GSM_TCHF_FRAME:
3110 /* Find callref */
3111 trans = trans_find_by_callref(net, data->callref);
3112 if (!trans)
3113 return -EIO;
3114 if (!trans->lchan)
3115 return 0;
3116 if (trans->lchan->type != GSM_LCHAN_TCH_F)
3117 return 0;
3118 bts = trans->lchan->ts->trx->bts;
3119 switch (bts->type) {
3120 case GSM_BTS_TYPE_NANOBTS:
3121 if (!trans->lchan->abis_ip.rtp_socket)
3122 return 0;
3123 return rtp_send_frame(trans->lchan->abis_ip.rtp_socket, arg);
3124 case GSM_BTS_TYPE_BS11:
3125 return trau_send_frame(trans->lchan, arg);
3126 default:
3127 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
3128 }
3129 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 }
3131
3132 memset(&rel, 0, sizeof(struct gsm_mncc));
3133 rel.callref = data->callref;
3134
3135 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003136 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003137
3138 /* Callref unknown */
3139 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003140 struct gsm_subscriber *subscr;
3141
Harald Welte4a3464c2009-07-04 10:11:24 +02003142 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3144 "Received '%s' from MNCC with "
3145 "unknown callref %d\n", data->called.number,
3146 get_mncc_name(msg_type), data->callref);
3147 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003148 return mncc_release_ind(net, NULL, data->callref,
3149 GSM48_CAUSE_LOC_PRN_S_LU,
3150 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003152 if (!data->called.number[0] && !data->imsi[0]) {
3153 DEBUGP(DCC, "(bts - trx - ts - ti) "
3154 "Received '%s' from MNCC with "
3155 "no number or IMSI\n", get_mncc_name(msg_type));
3156 /* Invalid number */
3157 return mncc_release_ind(net, NULL, data->callref,
3158 GSM48_CAUSE_LOC_PRN_S_LU,
3159 GSM48_CC_CAUSE_INV_NR_FORMAT);
3160 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003161 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003162 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003163 subscr = subscr_get_by_extension(net,
3164 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003165 else
Harald Welte9176bd42009-07-23 18:46:00 +02003166 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167 /* If subscriber is not found */
3168 if (!subscr) {
3169 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3170 "Received '%s' from MNCC with "
3171 "unknown subscriber %s\n", data->called.number,
3172 get_mncc_name(msg_type), data->called.number);
3173 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003174 return mncc_release_ind(net, NULL, data->callref,
3175 GSM48_CAUSE_LOC_PRN_S_LU,
3176 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177 }
3178 /* If subscriber is not "attached" */
3179 if (!subscr->lac) {
3180 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3181 "Received '%s' from MNCC with "
3182 "detached subscriber %s\n", data->called.number,
3183 get_mncc_name(msg_type), data->called.number);
3184 subscr_put(subscr);
3185 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003186 return mncc_release_ind(net, NULL, data->callref,
3187 GSM48_CAUSE_LOC_PRN_S_LU,
3188 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003189 }
3190 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003191 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3192 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193 DEBUGP(DCC, "No memory for trans.\n");
3194 subscr_put(subscr);
3195 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003196 mncc_release_ind(net, NULL, data->callref,
3197 GSM48_CAUSE_LOC_PRN_S_LU,
3198 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003199 return -ENOMEM;
3200 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003201 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02003202 lchan = lchan_for_subscr(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003203 /* If subscriber has no lchan */
3204 if (!lchan) {
3205 /* find transaction with this subscriber already paging */
3206 llist_for_each_entry(transt, &net->trans_list, entry) {
3207 /* Transaction of our lchan? */
3208 if (transt == trans ||
3209 transt->subscr != subscr)
3210 continue;
3211 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
3212 "Received '%s' from MNCC with "
3213 "unallocated channel, paging already "
3214 "started.\n", bts->nr,
3215 data->called.number,
3216 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003217 subscr_put(subscr);
3218 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003219 return 0;
3220 }
3221 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003222 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02003223 /* Trigger paging */
3224 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
3225 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003226 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227 return 0;
3228 }
3229 /* Assign lchan */
3230 trans->lchan = lchan;
3231 use_lchan(lchan);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003232 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003233 }
3234 lchan = trans->lchan;
3235
3236 /* if paging did not respond yet */
3237 if (!lchan) {
3238 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3239 "Received '%s' from MNCC in paging state\n",
3240 (trans->subscr)?(trans->subscr->extension):"-",
3241 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003242 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3243 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003244 if (msg_type == MNCC_REL_REQ)
3245 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3246 else
3247 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3248 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003249 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003250 return rc;
3251 }
3252
3253 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3254 "Received '%s' from MNCC in state %d (%s)\n",
3255 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3256 trans->transaction_id,
3257 (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003258 get_mncc_name(msg_type), trans->cc.state,
3259 cc_state_names[trans->cc.state]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003260
3261 /* Find function for current state and message */
3262 for (i = 0; i < DOWNSLLEN; i++)
3263 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003264 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003265 break;
3266 if (i == DOWNSLLEN) {
3267 DEBUGP(DCC, "Message unhandled at this state.\n");
3268 return 0;
3269 }
3270
3271 rc = downstatelist[i].rout(trans, arg);
3272
3273 return rc;
3274}
3275
3276
3277static struct datastate {
3278 u_int32_t states;
3279 int type;
3280 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3281} datastatelist[] = {
3282 /* mobile originating call establishment */
3283 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3284 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3285 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3286 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3287 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3288 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3289 /* mobile terminating call establishment */
3290 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3291 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3292 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3293 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
3294 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
3295 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3296 /* signalling during call */
3297 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3298 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3299 {SBIT(GSM_CSTATE_ACTIVE),
3300 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3301 {ALL_STATES,
3302 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3303 {ALL_STATES,
3304 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3305 {ALL_STATES,
3306 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3307 {SBIT(GSM_CSTATE_ACTIVE),
3308 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3309 {SBIT(GSM_CSTATE_ACTIVE),
3310 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3311 {SBIT(GSM_CSTATE_ACTIVE),
3312 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3313 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3314 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3315 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3316 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3317 {SBIT(GSM_CSTATE_ACTIVE),
3318 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3319 /* clearing */
3320 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3321 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3322 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3323 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3324 {ALL_STATES, /* 5.4.3.4 */
3325 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3326};
3327
3328#define DATASLLEN \
3329 (sizeof(datastatelist) / sizeof(struct datastate))
3330
Harald Welte4bc90a12008-12-27 16:32:52 +00003331static int gsm0408_rcv_cc(struct msgb *msg)
3332{
3333 struct gsm48_hdr *gh = msgb_l3(msg);
3334 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003335 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003336 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003337 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003338 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003339
Harald Welte4bfdfe72009-06-10 23:11:52 +08003340 if (msg_type & 0x80) {
3341 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3342 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003343 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003344
3345 /* Find transaction */
Harald Welteb8b40732009-07-23 21:58:40 +02003346 trans = trans_find_by_id(lchan->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003347
Harald Welte6f5aee02009-07-23 21:21:14 +02003348 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003349 "Received '%s' from MS in state %d (%s)\n",
3350 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
3351 transaction_id, (lchan->subscr)?(lchan->subscr->extension):"-",
Harald Welteafedeab2010-03-04 10:55:40 +01003352 gsm48_cc_msg_names[msg_type], trans?(trans->cc.state):0,
Harald Weltedcaf5652009-07-23 18:56:43 +02003353 cc_state_names[trans?(trans->cc.state):0]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003354
3355 /* Create transaction */
3356 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003357 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003358 "creating new trans.\n", transaction_id);
3359 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003360 trans = trans_alloc(lchan->subscr, GSM48_PDISC_CC,
3361 transaction_id, new_callref++);
3362 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003363 DEBUGP(DCC, "No memory for trans.\n");
3364 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003365 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003366 GSM48_MT_CC_RELEASE_COMPL);
3367 return -ENOMEM;
3368 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369 /* Assign transaction */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003370 trans->lchan = lchan;
3371 use_lchan(lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003372 }
3373
3374 /* find function for current state and message */
3375 for (i = 0; i < DATASLLEN; i++)
3376 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003377 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003378 break;
3379 if (i == DATASLLEN) {
3380 DEBUGP(DCC, "Message unhandled at this state.\n");
3381 return 0;
3382 }
3383
3384 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003385
3386 return rc;
3387}
3388
Harald Welte52b1f982008-12-23 20:25:15 +00003389/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)daef6062009-08-14 11:41:12 +02003390int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003391{
3392 struct gsm48_hdr *gh = msgb_l3(msg);
3393 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003394 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003395
3396 if (silent_call_reroute(msg))
3397 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003398
3399 switch (pdisc) {
3400 case GSM48_PDISC_CC:
3401 rc = gsm0408_rcv_cc(msg);
3402 break;
3403 case GSM48_PDISC_MM:
3404 rc = gsm0408_rcv_mm(msg);
3405 break;
3406 case GSM48_PDISC_RR:
3407 rc = gsm0408_rcv_rr(msg);
3408 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003409 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003410 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003411 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003412 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003413 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003414 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3415 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003416 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003417 case GSM48_PDISC_NC_SS:
3418 rc = handle_rcv_ussd(msg);
3419 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003420 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003421 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3422 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003423 break;
3424 }
3425
3426 return rc;
3427}
Harald Welte8470bf22008-12-25 23:28:35 +00003428
Harald Welte4bfdfe72009-06-10 23:11:52 +08003429/* dequeue messages to layer 4 */
3430int bsc_upqueue(struct gsm_network *net)
3431{
3432 struct gsm_mncc *mncc;
3433 struct msgb *msg;
3434 int work = 0;
3435
3436 if (net)
3437 while ((msg = msgb_dequeue(&net->upqueue))) {
3438 mncc = (struct gsm_mncc *)msg->data;
3439 if (net->mncc_recv)
3440 net->mncc_recv(net, mncc->msg_type, mncc);
3441 work = 1; /* work done */
Harald Welte316c8252009-06-26 19:40:48 +02003442 talloc_free(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003443 }
3444
3445 return work;
3446}
Harald Weltedcaf5652009-07-23 18:56:43 +02003447
Harald Welte805f6442009-07-28 18:25:29 +02003448/*
3449 * This will be ran by the linker when loading the DSO. We use it to
3450 * do system initialization, e.g. registration of signal handlers.
3451 */
3452static __attribute__((constructor)) void on_dso_load_0408(void)
3453{
Harald Welte805f6442009-07-28 18:25:29 +02003454 register_signal_handler(SS_LCHAN, gsm0408_handle_lchan_signal, NULL);
3455 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3456}