blob: 93e136c1689dfeb28d4cc7800e35e6710cc18ac2 [file] [log] [blame]
Harald Welte27989d42018-06-21 20:39:20 +02001/* GSM Mobile Radio Interface Layer 3 Call Control */
2
3/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdbool.h>
27#include <errno.h>
28#include <time.h>
29#include <netinet/in.h>
30#include <regex.h>
31#include <sys/types.h>
32
33#include "bscconfig.h"
34
35#include <osmocom/msc/db.h>
36#include <osmocom/msc/debug.h>
37#include <osmocom/msc/gsm_data.h>
38#include <osmocom/msc/gsm_subscriber.h>
39#include <osmocom/msc/gsm_04_11.h>
40#include <osmocom/msc/gsm_04_08.h>
41#include <osmocom/msc/gsm_04_80.h>
42#include <osmocom/msc/gsm_04_14.h>
43#include <osmocom/msc/gsm_09_11.h>
44#include <osmocom/msc/signal.h>
45#include <osmocom/msc/transaction.h>
46#include <osmocom/msc/silent_call.h>
Harald Welte27989d42018-06-21 20:39:20 +020047#include <osmocom/msc/mncc_int.h>
48#include <osmocom/abis/e1_input.h>
49#include <osmocom/core/bitvec.h>
50#include <osmocom/msc/vlr.h>
51#include <osmocom/msc/msc_ifaces.h>
52
53#include <osmocom/gsm/gsm48.h>
54#include <osmocom/gsm/gsm0480.h>
55#include <osmocom/gsm/gsm_utils.h>
56#include <osmocom/gsm/protocol/gsm_04_08.h>
57#include <osmocom/core/msgb.h>
58#include <osmocom/core/talloc.h>
59#include <osmocom/core/utils.h>
60#include <osmocom/core/byteswap.h>
61#include <osmocom/gsm/tlv.h>
62#include <osmocom/crypt/auth.h>
63#ifdef BUILD_IU
64#include <osmocom/ranap/iu_client.h>
65#endif
66
67#include <osmocom/msc/msc_ifaces.h>
68#include <osmocom/msc/a_iface.h>
69#include <osmocom/msc/msc_mgcp.h>
70
71#include <assert.h>
72
73static uint32_t new_callref = 0x80000001;
74
Philipp Maier9ca7b312018-10-10 17:00:49 +020075static void gsm48_cc_guard_timeout(void *arg)
76{
77 struct gsm_trans *trans = arg;
78 DEBUGP(DCC, "(sub %s) guard timeout expired\n",
79 vlr_subscr_msisdn_or_name(trans->vsub));
80 trans_free(trans);
81 return;
82}
83
84static void gsm48_stop_guard_timer(struct gsm_trans *trans)
85{
86 if (osmo_timer_pending(&trans->cc.timer_guard)) {
87 DEBUGP(DCC, "(sub %s) stopping pending guard timer\n",
88 vlr_subscr_msisdn_or_name(trans->vsub));
89 osmo_timer_del(&trans->cc.timer_guard);
90 }
91}
92
93static void gsm48_start_guard_timer(struct gsm_trans *trans)
94{
95 /* NOTE: The purpose of this timer is to prevent the cc state machine
96 * from hanging in cases where mncc, gsm48 or both become unresponsive
97 * for some reason. The timer is started initially with the setup from
98 * the gsm48 side and then re-started with every incoming mncc message.
99 * Once the mncc state reaches its active state the timer is stopped.
100 * So if the cc state machine does not show any activity for an
101 * extended amount of time during call setup or teardown the guard
102 * timer will time out and hard-clear the connection. */
103 if (osmo_timer_pending(&trans->cc.timer_guard))
104 gsm48_stop_guard_timer(trans);
105 DEBUGP(DCC, "(sub %s) starting guard timer with %d seconds\n",
106 vlr_subscr_msisdn_or_name(trans->vsub),
107 trans->net->mncc_guard_timeout);
108 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
109 osmo_timer_schedule(&trans->cc.timer_guard,
110 trans->net->mncc_guard_timeout, 0);
111}
Harald Welte27989d42018-06-21 20:39:20 +0200112
113/* Call Control */
114
115void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
116{
117 net->mncc_recv(net, msg);
118}
119
120int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
121{
122 struct gsm48_hdr *gh;
123 struct msgb *ss_notify;
124
125 ss_notify = gsm0480_create_notifySS(message);
126 if (!ss_notify)
127 return -1;
128
129 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
130 uint8_t *data = msgb_push(ss_notify, 1);
131 data[0] = ss_notify->len - 1;
132 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
133 gh->msg_type = GSM48_MT_CC_FACILITY;
134 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
135}
136
137/* FIXME: this count_statistics is a state machine behaviour. we should convert
138 * the complete call control into a state machine. Afterwards we can move this
139 * code into state transitions.
140 */
141static void count_statistics(struct gsm_trans *trans, int new_state)
142{
143 int old_state = trans->cc.state;
144 struct rate_ctr_group *msc = trans->net->msc_ctrs;
145
146 if (old_state == new_state)
147 return;
148
149 /* state incoming */
150 switch (new_state) {
151 case GSM_CSTATE_ACTIVE:
152 osmo_counter_inc(trans->net->active_calls);
153 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
154 break;
155 }
156
157 /* state outgoing */
158 switch (old_state) {
159 case GSM_CSTATE_ACTIVE:
160 osmo_counter_dec(trans->net->active_calls);
161 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
162 new_state == GSM_CSTATE_DISCONNECT_IND)
163 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
164 else
165 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
166 break;
167 }
168}
169
170/* The entire call control code is written in accordance with Figure 7.10c
171 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
172 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
173 * it for voice */
174
175static void new_cc_state(struct gsm_trans *trans, int state)
176{
177 if (state > 31 || state < 0)
178 return;
179
180 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
181 trans->transaction_id,
182 vlr_subscr_name(trans->vsub),
183 gsm48_cc_state_name(trans->cc.state),
184 gsm48_cc_state_name(state));
185
186 count_statistics(trans, state);
187 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200188
189 /* Stop the guard timer when a call reaches the active state */
190 if (state == GSM_CSTATE_ACTIVE)
191 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200192}
193
194static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
195{
196 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
197 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
198 uint8_t *cause, *call_state;
199
200 gh->msg_type = GSM48_MT_CC_STATUS;
201
202 cause = msgb_put(msg, 3);
203 cause[0] = 2;
204 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
205 cause[2] = 0x80 | 30; /* response to status inquiry */
206
207 call_state = msgb_put(msg, 1);
208 call_state[0] = 0xc0 | 0x00;
209
210 return gsm48_conn_sendmsg(msg, trans->conn, trans);
211}
212
213static void gsm48_stop_cc_timer(struct gsm_trans *trans)
214{
215 if (osmo_timer_pending(&trans->cc.timer)) {
216 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
217 osmo_timer_del(&trans->cc.timer);
218 trans->cc.Tcurrent = 0;
219 }
220}
221
222static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
223 int msg_type, struct gsm_mncc *mncc)
224{
225 struct msgb *msg;
226 unsigned char *data;
227
228 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
229
230#if BEFORE_MSCSPLIT
231 /* Re-enable this log output once we can obtain this information via
232 * A-interface, see OS#2391. */
233 if (trans)
234 if (trans->conn && trans->conn->lchan)
235 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
236 "Sending '%s' to MNCC.\n",
237 trans->conn->lchan->ts->trx->bts->nr,
238 trans->conn->lchan->ts->trx->nr,
239 trans->conn->lchan->ts->nr, trans->transaction_id,
240 vlr_subscr_msisdn_or_name(trans->vsub),
241 get_mncc_name(msg_type));
242 else
243 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
244 "Sending '%s' to MNCC.\n",
245 vlr_subscr_msisdn_or_name(trans->vsub),
246 get_mncc_name(msg_type));
247 else
248 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
249 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
250#else
251 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
252#endif
253
254 mncc->msg_type = msg_type;
255
256 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
257 if (!msg)
258 return -ENOMEM;
259
260 data = msgb_put(msg, sizeof(struct gsm_mncc));
261 memcpy(data, mncc, sizeof(struct gsm_mncc));
262
263 cc_tx_to_mncc(net, msg);
264
265 return 0;
266}
267
268int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
269 uint32_t callref, int location, int value)
270{
271 struct gsm_mncc rel;
272
273 memset(&rel, 0, sizeof(rel));
274 rel.callref = callref;
275 mncc_set_cause(&rel, location, value);
276 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
277 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
278 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
279}
280
281/* Call Control Specific transaction release.
282 * gets called by trans_free, DO NOT CALL YOURSELF! */
283void _gsm48_cc_trans_free(struct gsm_trans *trans)
284{
285 gsm48_stop_cc_timer(trans);
286
287 /* Initiate the teardown of the related connections on the MGW */
288 msc_mgcp_call_release(trans);
289
290 /* send release to L4, if callref still exists */
291 if (trans->callref) {
292 /* Ressource unavailable */
293 mncc_release_ind(trans->net, trans, trans->callref,
294 GSM48_CAUSE_LOC_PRN_S_LU,
295 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
296 /* This is a final freeing of the transaction. The MNCC release may have triggered the
297 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
298 gsm48_stop_cc_timer(trans);
299 }
300 if (trans->cc.state != GSM_CSTATE_NULL)
301 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200302
303 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200304}
305
306static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
307
308/* call-back from paging the B-end of the connection */
309static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
310 struct msgb *msg, void *_conn, void *_transt)
311{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100312 struct ran_conn *conn = _conn;
Harald Welte27989d42018-06-21 20:39:20 +0200313 struct gsm_trans *transt = _transt;
314 enum gsm_paging_event paging_event = event;
315
316 OSMO_ASSERT(!transt->conn);
317
318 switch (paging_event) {
319 case GSM_PAGING_SUCCEEDED:
320 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
321 vlr_subscr_msisdn_or_name(transt->vsub));
322 OSMO_ASSERT(conn);
323 /* Assign conn */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100324 transt->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200325 transt->paging_request = NULL;
326 /* send SETUP request to called party */
327 gsm48_cc_tx_setup(transt, &transt->cc.msg);
328 break;
329 case GSM_PAGING_EXPIRED:
330 case GSM_PAGING_BUSY:
331 DEBUGP(DCC, "Paging subscr %s %s!\n",
332 vlr_subscr_msisdn_or_name(transt->vsub),
333 paging_event == GSM_PAGING_EXPIRED ? "expired" : "busy");
334 /* Temporarily out of order */
335 mncc_release_ind(transt->net, transt,
336 transt->callref,
337 GSM48_CAUSE_LOC_PRN_S_LU,
338 GSM48_CC_CAUSE_DEST_OOO);
339 transt->callref = 0;
340 transt->paging_request = NULL;
341 trans_free(transt);
342 break;
343 }
344
345 return 0;
346}
347
348/* bridge channels of two transactions */
349static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
350{
351 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
352 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
353 int rc;
354
355 if (!trans1 || !trans2)
356 return -EIO;
357
358 if (!trans1->conn || !trans2->conn)
359 return -EIO;
360
361 /* Which subscriber do we want to track trans1 or trans2? */
362 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
363
Philipp Maier8ad3dac2018-08-07 13:00:14 +0200364 /* This call briding mechanism is only used with the internal MNCC.
365 * functionality (with external MNCC briding would be done by the PBX)
366 * This means we may just copy the codec info we have for the RAN side
367 * of the first leg to the CN side of both legs. This also means that
368 * if both legs use different codecs the MGW must perform transcoding
369 * on the second leg. */
370 trans1->conn->rtp.codec_cn = trans1->conn->rtp.codec_ran;
371 trans2->conn->rtp.codec_cn = trans1->conn->rtp.codec_ran;
372
Harald Welte27989d42018-06-21 20:39:20 +0200373 /* Bridge RTP streams */
374 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
375 trans2->conn->rtp.local_addr_cn);
376 if (rc)
377 return -EINVAL;
378
379 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
380 trans1->conn->rtp.local_addr_cn);
381 if (rc)
382 return -EINVAL;
383
384 return 0;
385}
386
387static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
388{
389 DEBUGP(DCC, "-> STATUS ENQ\n");
390 return gsm48_cc_tx_status(trans, msg);
391}
392
393static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
394static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
395
396static void gsm48_cc_timeout(void *arg)
397{
398 struct gsm_trans *trans = arg;
399 int disconnect = 0, release = 0;
400 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
401 int mo_location = GSM48_CAUSE_LOC_USER;
402 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
403 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
404 struct gsm_mncc mo_rel, l4_rel;
405
406 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
407 mo_rel.callref = trans->callref;
408 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
409 l4_rel.callref = trans->callref;
410
411 switch(trans->cc.Tcurrent) {
412 case 0x303:
413 release = 1;
414 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
415 break;
416 case 0x310:
417 disconnect = 1;
418 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
419 break;
420 case 0x313:
421 disconnect = 1;
422 /* unknown, did not find it in the specs */
423 break;
424 case 0x301:
425 disconnect = 1;
426 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
427 break;
428 case 0x308:
429 if (!trans->cc.T308_second) {
430 /* restart T308 a second time */
431 gsm48_cc_tx_release(trans, &trans->cc.msg);
432 trans->cc.T308_second = 1;
433 break; /* stay in release state */
434 }
435 trans_free(trans);
436 return;
437 case 0x306:
438 release = 1;
439 mo_cause = trans->cc.msg.cause.value;
440 mo_location = trans->cc.msg.cause.location;
441 break;
442 case 0x323:
443 disconnect = 1;
444 break;
445 default:
446 release = 1;
447 }
448
449 if (release && trans->callref) {
450 /* process release towards layer 4 */
451 mncc_release_ind(trans->net, trans, trans->callref,
452 l4_location, l4_cause);
453 trans->callref = 0;
454 }
455
456 if (disconnect && trans->callref) {
457 /* process disconnect towards layer 4 */
458 mncc_set_cause(&l4_rel, l4_location, l4_cause);
459 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
460 }
461
462 /* process disconnect towards mobile station */
463 if (disconnect || release) {
464 mncc_set_cause(&mo_rel, mo_location, mo_cause);
465 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
466 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
467 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
468 mo_rel.cause.diag_len = 3;
469
470 if (disconnect)
471 gsm48_cc_tx_disconnect(trans, &mo_rel);
472 if (release)
473 gsm48_cc_tx_release(trans, &mo_rel);
474 }
475
476}
477
478/* disconnect both calls from the bridge */
479static inline void disconnect_bridge(struct gsm_network *net,
480 struct gsm_mncc_bridge *bridge, int err)
481{
482 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
483 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
484 struct gsm_mncc mx_rel;
485 if (!trans0 || !trans1)
486 return;
487
488 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
489 trans0->callref, trans1->callref, strerror(err));
490
491 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
492 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
493 GSM48_CC_CAUSE_CHAN_UNACCEPT);
494
495 mx_rel.callref = trans0->callref;
496 gsm48_cc_tx_disconnect(trans0, &mx_rel);
497
498 mx_rel.callref = trans1->callref;
499 gsm48_cc_tx_disconnect(trans1, &mx_rel);
500}
501
502static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
503 int sec, int micro)
504{
505 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
506 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
507 osmo_timer_schedule(&trans->cc.timer, sec, micro);
508 trans->cc.Tcurrent = current;
509}
510
511static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
512{
513 struct gsm48_hdr *gh = msgb_l3(msg);
514 uint8_t msg_type = gsm48_hdr_msg_type(gh);
515 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
516 struct tlv_parsed tp;
517 struct gsm_mncc setup;
518
Philipp Maier9ca7b312018-10-10 17:00:49 +0200519 gsm48_start_guard_timer(trans);
520
Harald Welte27989d42018-06-21 20:39:20 +0200521 memset(&setup, 0, sizeof(struct gsm_mncc));
522 setup.callref = trans->callref;
523
524 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
525 /* emergency setup is identified by msg_type */
526 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
527 setup.fields |= MNCC_F_EMERGENCY;
528 setup.emergency = 1;
529 /* use destination number as configured by user (if any) */
530 if (trans->net->emergency.route_to_msisdn) {
531 setup.fields |= MNCC_F_CALLED;
532 setup.called.type = 0; /* unknown */
533 setup.called.plan = 0; /* unknown */
534 OSMO_STRLCPY_ARRAY(setup.called.number,
535 trans->net->emergency.route_to_msisdn);
536 }
537 }
538
539 /* use subscriber as calling party number */
540 setup.fields |= MNCC_F_CALLING;
541 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
542 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
543
544 /* bearer capability */
545 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
546 setup.fields |= MNCC_F_BEARER_CAP;
547 gsm48_decode_bearer_cap(&setup.bearer_cap,
548 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
549
550 /* Create a copy of the bearer capability
551 * in the transaction struct, so we can use
552 * this information later */
553 memcpy(&trans->bearer_cap,&setup.bearer_cap,
554 sizeof(trans->bearer_cap));
555 }
556 /* facility */
557 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
558 setup.fields |= MNCC_F_FACILITY;
559 gsm48_decode_facility(&setup.facility,
560 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
561 }
562 /* called party bcd number */
563 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
564 setup.fields |= MNCC_F_CALLED;
565 gsm48_decode_called(&setup.called,
566 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
567 }
568 /* user-user */
569 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
570 setup.fields |= MNCC_F_USERUSER;
571 gsm48_decode_useruser(&setup.useruser,
572 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
573 }
574 /* ss-version */
575 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
576 setup.fields |= MNCC_F_SSVERSION;
577 gsm48_decode_ssversion(&setup.ssversion,
578 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
579 }
580 /* CLIR suppression */
581 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
582 setup.clir.sup = 1;
583 /* CLIR invocation */
584 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
585 setup.clir.inv = 1;
586 /* cc cap */
587 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
588 setup.fields |= MNCC_F_CCCAP;
589 gsm48_decode_cccap(&setup.cccap,
590 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
591 }
592
593 new_cc_state(trans, GSM_CSTATE_INITIATED);
594
595 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
596 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
597 setup.called.number);
598
599 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
600
601 /* indicate setup to MNCC */
602 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
603
604 /* MNCC code will modify the channel asynchronously, we should
605 * ipaccess-bind only after the modification has been made to the
606 * lchan->tch_mode */
607 return 0;
608}
609
610static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
611{
612 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
613 struct gsm48_hdr *gh;
614 struct gsm_mncc *setup = arg;
615 int rc, trans_id;
616
617 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
618
619 /* transaction id must not be assigned */
620 if (trans->transaction_id != 0xff) { /* unasssigned */
621 DEBUGP(DCC, "TX Setup with assigned transaction. "
622 "This is not allowed!\n");
623 /* Temporarily out of order */
624 rc = mncc_release_ind(trans->net, trans, trans->callref,
625 GSM48_CAUSE_LOC_PRN_S_LU,
626 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
627 trans->callref = 0;
628 trans_free(trans);
629 return rc;
630 }
631
632 /* Get free transaction_id */
633 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
634 GSM48_PDISC_CC, 0);
635 if (trans_id < 0) {
636 /* no free transaction ID */
637 rc = mncc_release_ind(trans->net, trans, trans->callref,
638 GSM48_CAUSE_LOC_PRN_S_LU,
639 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
640 trans->callref = 0;
641 trans_free(trans);
642 return rc;
643 }
644 trans->transaction_id = trans_id;
645
646 gh->msg_type = GSM48_MT_CC_SETUP;
647
648 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
649
650 /* bearer capability */
651 if (setup->fields & MNCC_F_BEARER_CAP) {
652 /* Create a copy of the bearer capability in the transaction struct, so we
653 * can use this information later */
654 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
655 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
656 }
657 /* facility */
658 if (setup->fields & MNCC_F_FACILITY)
659 gsm48_encode_facility(msg, 0, &setup->facility);
660 /* progress */
661 if (setup->fields & MNCC_F_PROGRESS)
662 gsm48_encode_progress(msg, 0, &setup->progress);
663 /* calling party BCD number */
664 if (setup->fields & MNCC_F_CALLING)
665 gsm48_encode_calling(msg, &setup->calling);
666 /* called party BCD number */
667 if (setup->fields & MNCC_F_CALLED)
668 gsm48_encode_called(msg, &setup->called);
669 /* user-user */
670 if (setup->fields & MNCC_F_USERUSER)
671 gsm48_encode_useruser(msg, 0, &setup->useruser);
672 /* redirecting party BCD number */
673 if (setup->fields & MNCC_F_REDIRECTING)
674 gsm48_encode_redirecting(msg, &setup->redirecting);
675 /* signal */
676 if (setup->fields & MNCC_F_SIGNAL)
677 gsm48_encode_signal(msg, setup->signal);
678
679 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
680
681 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
682
683 return gsm48_conn_sendmsg(msg, trans->conn, trans);
684}
685
686static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
687{
688 struct gsm48_hdr *gh = msgb_l3(msg);
689 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
690 struct tlv_parsed tp;
691 struct gsm_mncc call_conf;
692 int rc;
693
694 gsm48_stop_cc_timer(trans);
695 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
696
697 memset(&call_conf, 0, sizeof(struct gsm_mncc));
698 call_conf.callref = trans->callref;
699
700 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
701#if 0
702 /* repeat */
703 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
704 call_conf.repeat = 1;
705 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
706 call_conf.repeat = 2;
707#endif
708 /* bearer capability */
709 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
710 call_conf.fields |= MNCC_F_BEARER_CAP;
711 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
712 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
713
714 /* Create a copy of the bearer capability
715 * in the transaction struct, so we can use
716 * this information later */
717 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
718 sizeof(trans->bearer_cap));
719 }
720 /* cause */
721 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
722 call_conf.fields |= MNCC_F_CAUSE;
723 gsm48_decode_cause(&call_conf.cause,
724 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
725 }
726 /* cc cap */
727 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
728 call_conf.fields |= MNCC_F_CCCAP;
729 gsm48_decode_cccap(&call_conf.cccap,
730 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
731 }
732
733 /* IMSI of called subscriber */
734 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
735
736 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
737
738 /* Assign call (if not done yet) */
Neels Hofmeyrb16259f2018-12-20 02:57:56 +0100739 rc = msc_mgcp_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200740
741 /* don't continue, if there were problems with
742 * the call assignment. */
743 if (rc)
744 return rc;
745
746 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
747 &call_conf);
748}
749
750static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
751{
752 struct gsm_mncc *proceeding = arg;
753 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
754 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
755 int rc;
756
757 gh->msg_type = GSM48_MT_CC_CALL_PROC;
758
759 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
760
761 /* bearer capability */
762 if (proceeding->fields & MNCC_F_BEARER_CAP) {
763 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
764 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
765 }
766 /* facility */
767 if (proceeding->fields & MNCC_F_FACILITY)
768 gsm48_encode_facility(msg, 0, &proceeding->facility);
769 /* progress */
770 if (proceeding->fields & MNCC_F_PROGRESS)
771 gsm48_encode_progress(msg, 0, &proceeding->progress);
772
773 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
774 if (rc)
775 return rc;
776
777 /* Assign call (if not done yet) */
Neels Hofmeyrb16259f2018-12-20 02:57:56 +0100778 return msc_mgcp_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200779}
780
781static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
782{
783 struct gsm48_hdr *gh = msgb_l3(msg);
784 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
785 struct tlv_parsed tp;
786 struct gsm_mncc alerting;
787
788 gsm48_stop_cc_timer(trans);
789 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
790
791 memset(&alerting, 0, sizeof(struct gsm_mncc));
792 alerting.callref = trans->callref;
793 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
794 /* facility */
795 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
796 alerting.fields |= MNCC_F_FACILITY;
797 gsm48_decode_facility(&alerting.facility,
798 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
799 }
800
801 /* progress */
802 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
803 alerting.fields |= MNCC_F_PROGRESS;
804 gsm48_decode_progress(&alerting.progress,
805 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
806 }
807 /* ss-version */
808 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
809 alerting.fields |= MNCC_F_SSVERSION;
810 gsm48_decode_ssversion(&alerting.ssversion,
811 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
812 }
813
814 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
815
816 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
817 &alerting);
818}
819
820static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
821{
822 struct gsm_mncc *alerting = arg;
823 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
824 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
825
826 gh->msg_type = GSM48_MT_CC_ALERTING;
827
828 /* facility */
829 if (alerting->fields & MNCC_F_FACILITY)
830 gsm48_encode_facility(msg, 0, &alerting->facility);
831 /* progress */
832 if (alerting->fields & MNCC_F_PROGRESS)
833 gsm48_encode_progress(msg, 0, &alerting->progress);
834 /* user-user */
835 if (alerting->fields & MNCC_F_USERUSER)
836 gsm48_encode_useruser(msg, 0, &alerting->useruser);
837
838 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
839
840 return gsm48_conn_sendmsg(msg, trans->conn, trans);
841}
842
843static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
844{
845 struct gsm_mncc *progress = arg;
846 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
847 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
848
849 gh->msg_type = GSM48_MT_CC_PROGRESS;
850
851 /* progress */
852 gsm48_encode_progress(msg, 1, &progress->progress);
853 /* user-user */
854 if (progress->fields & MNCC_F_USERUSER)
855 gsm48_encode_useruser(msg, 0, &progress->useruser);
856
857 return gsm48_conn_sendmsg(msg, trans->conn, trans);
858}
859
860static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
861{
862 struct gsm_mncc *connect = arg;
863 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
864 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
865
866 gh->msg_type = GSM48_MT_CC_CONNECT;
867
868 gsm48_stop_cc_timer(trans);
869 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
870
871 /* facility */
872 if (connect->fields & MNCC_F_FACILITY)
873 gsm48_encode_facility(msg, 0, &connect->facility);
874 /* progress */
875 if (connect->fields & MNCC_F_PROGRESS)
876 gsm48_encode_progress(msg, 0, &connect->progress);
877 /* connected number */
878 if (connect->fields & MNCC_F_CONNECTED)
879 gsm48_encode_connected(msg, &connect->connected);
880 /* user-user */
881 if (connect->fields & MNCC_F_USERUSER)
882 gsm48_encode_useruser(msg, 0, &connect->useruser);
883
884 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
885
886 return gsm48_conn_sendmsg(msg, trans->conn, trans);
887}
888
889static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
890{
891 struct gsm48_hdr *gh = msgb_l3(msg);
892 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
893 struct tlv_parsed tp;
894 struct gsm_mncc connect;
895
896 gsm48_stop_cc_timer(trans);
897
898 memset(&connect, 0, sizeof(struct gsm_mncc));
899 connect.callref = trans->callref;
900 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
901 /* use subscriber as connected party number */
902 connect.fields |= MNCC_F_CONNECTED;
903 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
904 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
905
906 /* facility */
907 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
908 connect.fields |= MNCC_F_FACILITY;
909 gsm48_decode_facility(&connect.facility,
910 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
911 }
912 /* user-user */
913 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
914 connect.fields |= MNCC_F_USERUSER;
915 gsm48_decode_useruser(&connect.useruser,
916 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
917 }
918 /* ss-version */
919 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
920 connect.fields |= MNCC_F_SSVERSION;
921 gsm48_decode_ssversion(&connect.ssversion,
922 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
923 }
924
925 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
926 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
927
928 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
929}
930
931
932static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
933{
934 struct gsm_mncc connect_ack;
935
936 gsm48_stop_cc_timer(trans);
937
938 new_cc_state(trans, GSM_CSTATE_ACTIVE);
939 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
940
941 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
942 connect_ack.callref = trans->callref;
943
944 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
945 &connect_ack);
946}
947
948static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
949{
950 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
951 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
952
953 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
954
955 new_cc_state(trans, GSM_CSTATE_ACTIVE);
956
957 return gsm48_conn_sendmsg(msg, trans->conn, trans);
958}
959
960static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
961{
962 struct gsm48_hdr *gh = msgb_l3(msg);
963 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
964 struct tlv_parsed tp;
965 struct gsm_mncc disc;
966
967 gsm48_stop_cc_timer(trans);
968
969 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
970
971 memset(&disc, 0, sizeof(struct gsm_mncc));
972 disc.callref = trans->callref;
973 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
974 /* cause */
975 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
976 disc.fields |= MNCC_F_CAUSE;
977 gsm48_decode_cause(&disc.cause,
978 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
979 }
980 /* facility */
981 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
982 disc.fields |= MNCC_F_FACILITY;
983 gsm48_decode_facility(&disc.facility,
984 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
985 }
986 /* user-user */
987 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
988 disc.fields |= MNCC_F_USERUSER;
989 gsm48_decode_useruser(&disc.useruser,
990 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
991 }
992 /* ss-version */
993 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
994 disc.fields |= MNCC_F_SSVERSION;
995 gsm48_decode_ssversion(&disc.ssversion,
996 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
997 }
998
999 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
1000
1001}
1002
1003static struct gsm_mncc_cause default_cause = {
1004 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1005 .coding = 0,
1006 .rec = 0,
1007 .rec_val = 0,
1008 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1009 .diag_len = 0,
1010 .diag = { 0 },
1011};
1012
1013static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1014{
1015 struct gsm_mncc *disc = arg;
1016 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1017 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1018
1019 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1020
1021 gsm48_stop_cc_timer(trans);
1022 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1023
1024 /* cause */
1025 if (disc->fields & MNCC_F_CAUSE)
1026 gsm48_encode_cause(msg, 1, &disc->cause);
1027 else
1028 gsm48_encode_cause(msg, 1, &default_cause);
1029
1030 /* facility */
1031 if (disc->fields & MNCC_F_FACILITY)
1032 gsm48_encode_facility(msg, 0, &disc->facility);
1033 /* progress */
1034 if (disc->fields & MNCC_F_PROGRESS)
1035 gsm48_encode_progress(msg, 0, &disc->progress);
1036 /* user-user */
1037 if (disc->fields & MNCC_F_USERUSER)
1038 gsm48_encode_useruser(msg, 0, &disc->useruser);
1039
1040 /* store disconnect cause for T306 expiry */
1041 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1042
1043 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1044
1045 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1046}
1047
1048static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1049{
1050 struct gsm48_hdr *gh = msgb_l3(msg);
1051 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1052 struct tlv_parsed tp;
1053 struct gsm_mncc rel;
1054 int rc;
1055
1056 gsm48_stop_cc_timer(trans);
1057
1058 memset(&rel, 0, sizeof(struct gsm_mncc));
1059 rel.callref = trans->callref;
1060 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1061 /* cause */
1062 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1063 rel.fields |= MNCC_F_CAUSE;
1064 gsm48_decode_cause(&rel.cause,
1065 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1066 }
1067 /* facility */
1068 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1069 rel.fields |= MNCC_F_FACILITY;
1070 gsm48_decode_facility(&rel.facility,
1071 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1072 }
1073 /* user-user */
1074 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1075 rel.fields |= MNCC_F_USERUSER;
1076 gsm48_decode_useruser(&rel.useruser,
1077 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1078 }
1079 /* ss-version */
1080 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1081 rel.fields |= MNCC_F_SSVERSION;
1082 gsm48_decode_ssversion(&rel.ssversion,
1083 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1084 }
1085
1086 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1087 /* release collision 5.4.5 */
1088 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1089 } else {
1090 rc = gsm48_tx_simple(trans->conn,
1091 GSM48_PDISC_CC | (trans->transaction_id << 4),
1092 GSM48_MT_CC_RELEASE_COMPL);
1093 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1094 }
1095
1096 new_cc_state(trans, GSM_CSTATE_NULL);
1097
1098 trans->callref = 0;
1099 trans_free(trans);
1100
1101 return rc;
1102}
1103
1104static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1105{
1106 struct gsm_mncc *rel = arg;
1107 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1108 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1109
1110 gh->msg_type = GSM48_MT_CC_RELEASE;
1111
1112 gsm48_stop_cc_timer(trans);
1113 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1114
1115 /* cause */
1116 if (rel->fields & MNCC_F_CAUSE)
1117 gsm48_encode_cause(msg, 0, &rel->cause);
1118 /* facility */
1119 if (rel->fields & MNCC_F_FACILITY)
1120 gsm48_encode_facility(msg, 0, &rel->facility);
1121 /* user-user */
1122 if (rel->fields & MNCC_F_USERUSER)
1123 gsm48_encode_useruser(msg, 0, &rel->useruser);
1124
1125 trans->cc.T308_second = 0;
1126 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1127
1128 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1129 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1130
1131 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1132}
1133
1134static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1135{
1136 struct gsm48_hdr *gh = msgb_l3(msg);
1137 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1138 struct tlv_parsed tp;
1139 struct gsm_mncc rel;
1140 int rc = 0;
1141
1142 gsm48_stop_cc_timer(trans);
1143
1144 memset(&rel, 0, sizeof(struct gsm_mncc));
1145 rel.callref = trans->callref;
1146 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1147 /* cause */
1148 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1149 rel.fields |= MNCC_F_CAUSE;
1150 gsm48_decode_cause(&rel.cause,
1151 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1152 }
1153 /* facility */
1154 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1155 rel.fields |= MNCC_F_FACILITY;
1156 gsm48_decode_facility(&rel.facility,
1157 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1158 }
1159 /* user-user */
1160 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1161 rel.fields |= MNCC_F_USERUSER;
1162 gsm48_decode_useruser(&rel.useruser,
1163 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1164 }
1165 /* ss-version */
1166 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1167 rel.fields |= MNCC_F_SSVERSION;
1168 gsm48_decode_ssversion(&rel.ssversion,
1169 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1170 }
1171
1172 if (trans->callref) {
1173 switch (trans->cc.state) {
1174 case GSM_CSTATE_CALL_PRESENT:
1175 rc = mncc_recvmsg(trans->net, trans,
1176 MNCC_REJ_IND, &rel);
1177 break;
1178 case GSM_CSTATE_RELEASE_REQ:
1179 rc = mncc_recvmsg(trans->net, trans,
1180 MNCC_REL_CNF, &rel);
1181 break;
1182 default:
1183 rc = mncc_recvmsg(trans->net, trans,
1184 MNCC_REL_IND, &rel);
1185 }
1186 }
1187
1188 trans->callref = 0;
1189 trans_free(trans);
1190
1191 return rc;
1192}
1193
1194static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1195{
1196 struct gsm_mncc *rel = arg;
1197 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1198 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1199 int ret;
1200
1201 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1202
1203 trans->callref = 0;
1204
1205 gsm48_stop_cc_timer(trans);
1206
1207 /* cause */
1208 if (rel->fields & MNCC_F_CAUSE)
1209 gsm48_encode_cause(msg, 0, &rel->cause);
1210 /* facility */
1211 if (rel->fields & MNCC_F_FACILITY)
1212 gsm48_encode_facility(msg, 0, &rel->facility);
1213 /* user-user */
1214 if (rel->fields & MNCC_F_USERUSER)
1215 gsm48_encode_useruser(msg, 0, &rel->useruser);
1216
1217 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
1218
1219 trans_free(trans);
1220
1221 return ret;
1222}
1223
1224static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1225{
1226 struct gsm48_hdr *gh = msgb_l3(msg);
1227 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1228 struct tlv_parsed tp;
1229 struct gsm_mncc fac;
1230
1231 memset(&fac, 0, sizeof(struct gsm_mncc));
1232 fac.callref = trans->callref;
1233 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1234 /* facility */
1235 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1236 fac.fields |= MNCC_F_FACILITY;
1237 gsm48_decode_facility(&fac.facility,
1238 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1239 }
1240 /* ss-version */
1241 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1242 fac.fields |= MNCC_F_SSVERSION;
1243 gsm48_decode_ssversion(&fac.ssversion,
1244 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1245 }
1246
1247 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1248}
1249
1250static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1251{
1252 struct gsm_mncc *fac = arg;
1253 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1254 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1255
1256 gh->msg_type = GSM48_MT_CC_FACILITY;
1257
1258 /* facility */
1259 gsm48_encode_facility(msg, 1, &fac->facility);
1260
1261 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1262}
1263
1264static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1265{
1266 struct gsm_mncc hold;
1267
1268 memset(&hold, 0, sizeof(struct gsm_mncc));
1269 hold.callref = trans->callref;
1270 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1271}
1272
1273static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1274{
1275 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1276 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1277
1278 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1279
1280 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1281}
1282
1283static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1284{
1285 struct gsm_mncc *hold_rej = arg;
1286 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1287 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1288
1289 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1290
1291 /* cause */
1292 if (hold_rej->fields & MNCC_F_CAUSE)
1293 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1294 else
1295 gsm48_encode_cause(msg, 1, &default_cause);
1296
1297 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1298}
1299
1300static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1301{
1302 struct gsm_mncc retrieve;
1303
1304 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1305 retrieve.callref = trans->callref;
1306 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1307 &retrieve);
1308}
1309
1310static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1311{
1312 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1313 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1314
1315 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1316
1317 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1318}
1319
1320static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1321{
1322 struct gsm_mncc *retrieve_rej = arg;
1323 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1324 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1325
1326 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1327
1328 /* cause */
1329 if (retrieve_rej->fields & MNCC_F_CAUSE)
1330 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1331 else
1332 gsm48_encode_cause(msg, 1, &default_cause);
1333
1334 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1335}
1336
1337static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1338{
1339 struct gsm48_hdr *gh = msgb_l3(msg);
1340 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1341 struct tlv_parsed tp;
1342 struct gsm_mncc dtmf;
1343
1344 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1345 dtmf.callref = trans->callref;
1346 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1347 /* keypad facility */
1348 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1349 dtmf.fields |= MNCC_F_KEYPAD;
1350 gsm48_decode_keypad(&dtmf.keypad,
1351 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1352 }
1353
1354 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1355}
1356
1357static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1358{
1359 struct gsm_mncc *dtmf = arg;
1360 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1361 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1362
1363 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1364
1365 /* keypad */
1366 if (dtmf->fields & MNCC_F_KEYPAD)
1367 gsm48_encode_keypad(msg, dtmf->keypad);
1368
1369 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1370}
1371
1372static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1373{
1374 struct gsm_mncc *dtmf = arg;
1375 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1376 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1377
1378 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1379
1380 /* cause */
1381 if (dtmf->fields & MNCC_F_CAUSE)
1382 gsm48_encode_cause(msg, 1, &dtmf->cause);
1383 else
1384 gsm48_encode_cause(msg, 1, &default_cause);
1385
1386 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1387}
1388
1389static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1390{
1391 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1392 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1393
1394 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1395
1396 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1397}
1398
1399static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1400{
1401 struct gsm_mncc dtmf;
1402
1403 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1404 dtmf.callref = trans->callref;
1405
1406 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1407}
1408
1409static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1410{
1411 struct gsm48_hdr *gh = msgb_l3(msg);
1412 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1413 struct tlv_parsed tp;
1414 struct gsm_mncc modify;
1415
1416 memset(&modify, 0, sizeof(struct gsm_mncc));
1417 modify.callref = trans->callref;
1418 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1419 /* bearer capability */
1420 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1421 modify.fields |= MNCC_F_BEARER_CAP;
1422 gsm48_decode_bearer_cap(&modify.bearer_cap,
1423 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1424
1425 /* Create a copy of the bearer capability
1426 * in the transaction struct, so we can use
1427 * this information later */
1428 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1429 sizeof(trans->bearer_cap));
1430 }
1431
1432 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1433
1434 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1435}
1436
1437static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1438{
1439 struct gsm_mncc *modify = arg;
1440 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1442
1443 gh->msg_type = GSM48_MT_CC_MODIFY;
1444
1445 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1446
1447 /* bearer capability */
1448 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1449 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1450
1451 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1452
1453 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1454}
1455
1456static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1457{
1458 struct gsm48_hdr *gh = msgb_l3(msg);
1459 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1460 struct tlv_parsed tp;
1461 struct gsm_mncc modify;
1462
1463 gsm48_stop_cc_timer(trans);
1464
1465 memset(&modify, 0, sizeof(struct gsm_mncc));
1466 modify.callref = trans->callref;
1467 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1468 /* bearer capability */
1469 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1470 modify.fields |= MNCC_F_BEARER_CAP;
1471 gsm48_decode_bearer_cap(&modify.bearer_cap,
1472 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1473
1474 /* Create a copy of the bearer capability
1475 * in the transaction struct, so we can use
1476 * this information later */
1477 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1478 sizeof(trans->bearer_cap));
1479 }
1480
1481 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1482
1483 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1484}
1485
1486static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1487{
1488 struct gsm_mncc *modify = arg;
1489 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1490 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1491
1492 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1493
1494 /* bearer capability */
1495 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1496 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1497
1498 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1499
1500 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1501}
1502
1503static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1504{
1505 struct gsm48_hdr *gh = msgb_l3(msg);
1506 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1507 struct tlv_parsed tp;
1508 struct gsm_mncc modify;
1509
1510 gsm48_stop_cc_timer(trans);
1511
1512 memset(&modify, 0, sizeof(struct gsm_mncc));
1513 modify.callref = trans->callref;
1514 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1515 /* bearer capability */
1516 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1517 modify.fields |= GSM48_IE_BEARER_CAP;
1518 gsm48_decode_bearer_cap(&modify.bearer_cap,
1519 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1520
1521 /* Create a copy of the bearer capability
1522 * in the transaction struct, so we can use
1523 * this information later */
1524 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1525 sizeof(trans->bearer_cap));
1526 }
1527 /* cause */
1528 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1529 modify.fields |= MNCC_F_CAUSE;
1530 gsm48_decode_cause(&modify.cause,
1531 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1532 }
1533
1534 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1535
1536 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1537}
1538
1539static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1540{
1541 struct gsm_mncc *modify = arg;
1542 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1543 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1544
1545 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1546
1547 /* bearer capability */
1548 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1549 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1550 /* cause */
1551 gsm48_encode_cause(msg, 1, &modify->cause);
1552
1553 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1554
1555 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1556}
1557
1558static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1559{
1560 struct gsm_mncc *notify = arg;
1561 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1562 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1563
1564 gh->msg_type = GSM48_MT_CC_NOTIFY;
1565
1566 /* notify */
1567 gsm48_encode_notify(msg, notify->notify);
1568
1569 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1570}
1571
1572static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1573{
1574 struct gsm48_hdr *gh = msgb_l3(msg);
1575 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1576// struct tlv_parsed tp;
1577 struct gsm_mncc notify;
1578
1579 memset(&notify, 0, sizeof(struct gsm_mncc));
1580 notify.callref = trans->callref;
1581// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1582 if (payload_len >= 1)
1583 gsm48_decode_notify(&notify.notify, gh->data);
1584
1585 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1586}
1587
1588static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1589{
1590 struct gsm_mncc *user = arg;
1591 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1592 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1593
1594 gh->msg_type = GSM48_MT_CC_USER_INFO;
1595
1596 /* user-user */
1597 if (user->fields & MNCC_F_USERUSER)
1598 gsm48_encode_useruser(msg, 1, &user->useruser);
1599 /* more data */
1600 if (user->more)
1601 gsm48_encode_more(msg);
1602
1603 return gsm48_conn_sendmsg(msg, trans->conn, trans);
1604}
1605
1606static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1607{
1608 struct gsm48_hdr *gh = msgb_l3(msg);
1609 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1610 struct tlv_parsed tp;
1611 struct gsm_mncc user;
1612
1613 memset(&user, 0, sizeof(struct gsm_mncc));
1614 user.callref = trans->callref;
1615 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1616 /* user-user */
1617 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1618 user.fields |= MNCC_F_USERUSER;
1619 gsm48_decode_useruser(&user.useruser,
1620 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1621 }
1622 /* more data */
1623 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1624 user.more = 1;
1625
1626 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1627}
1628
1629static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
1630 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
1631 uint32_t payload_msg_type)
1632{
1633 uint8_t data[sizeof(struct gsm_mncc)];
1634 struct gsm_mncc_rtp *rtp;
1635
1636 memset(&data, 0, sizeof(data));
1637 rtp = (struct gsm_mncc_rtp *) &data[0];
1638
1639 rtp->callref = callref;
1640 rtp->msg_type = cmd;
Philipp Maiere2497f72018-09-28 10:37:17 +02001641 rtp->ip = osmo_htonl(addr);
Harald Welte27989d42018-06-21 20:39:20 +02001642 rtp->port = port;
1643 rtp->payload_type = payload_type;
1644 rtp->payload_msg_type = payload_msg_type;
1645 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
1646}
1647
1648static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
1649{
1650 int msg_type;
1651
1652 /* FIXME This has to be set to some meaningful value.
1653 * Possible options are:
1654 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
1655 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
1656 * (0 if unknown) */
1657 msg_type = GSM_TCHF_FRAME;
1658
1659 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
1660 uint16_t port = trans->conn->rtp.local_port_cn;
1661
Harald Welte27989d42018-06-21 20:39:20 +02001662 if (addr == INADDR_NONE) {
1663 LOGP(DMNCC, LOGL_ERROR,
1664 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
1665 vlr_subscr_name(trans->vsub));
1666 return;
1667 }
1668 if (port == 0) {
1669 LOGP(DMNCC, LOGL_ERROR,
1670 "(subscriber:%s) external MNCC is signalling invalid Port\n",
1671 vlr_subscr_name(trans->vsub));
1672 return;
1673 }
1674
1675 /* FIXME: This has to be set to some meaningful value,
1676 * before the MSC-Split, this value was pulled from
1677 * lchan->abis_ip.rtp_payload */
1678 uint32_t payload_type = 0;
1679
1680 return mncc_recv_rtp(net, trans->callref, cmd,
1681 addr,
1682 port,
1683 payload_type,
1684 msg_type);
1685}
1686
1687static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
1688{
1689 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
1690}
1691
1692static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1693{
1694 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001695
1696 /* Find callref */
1697 trans = trans_find_by_callref(net, callref);
1698 if (!trans) {
1699 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
1700 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
1701 return -EIO;
1702 }
1703 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1704 if (!trans->conn) {
1705 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
1706 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
1707 return 0;
1708 }
1709
Harald Welte27989d42018-06-21 20:39:20 +02001710 /* When we call msc_mgcp_call_assignment() we will trigger, depending
1711 * on the RAN type the call assignment on the A or Iu interface.
1712 * msc_mgcp_call_assignment() also takes care about sending the CRCX
1713 * command to the MGCP-GW. The CRCX will return the port number,
1714 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
1715 * have to return this port number back to the MNCC by sending
1716 * it back with the TCH_RTP_CREATE message. To make sure that
1717 * this message is sent AFTER the response to CRCX from the
1718 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
1719 * to take care of this by setting trans->tch_rtp_create to true.
1720 * This will make sure that gsm48_tch_rtp_create() (below) is
1721 * called as soon as the local port number has become known. */
1722 trans->tch_rtp_create = true;
1723
1724 /* Assign call (if not done yet) */
Neels Hofmeyrb16259f2018-12-20 02:57:56 +01001725 return msc_mgcp_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001726}
1727
1728/* Trigger TCH_RTP_CREATE acknowledgement */
1729int gsm48_tch_rtp_create(struct gsm_trans *trans)
1730{
1731 /* This function is called as soon as the port, on which the
1732 * mgcp-gw expects the incoming RTP stream from the remote
1733 * end (e.g. Asterisk) is known. */
1734
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001735 struct ran_conn *conn = trans->conn;
Harald Welte27989d42018-06-21 20:39:20 +02001736 struct gsm_network *network = conn->network;
1737
1738 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
1739 return 0;
1740}
1741
Maxc4e4fa72018-12-17 12:55:59 +01001742static int tch_rtp_connect(struct gsm_network *net, struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001743{
1744 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001745 struct in_addr addr;
1746
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001747 /* FIXME: in *rtp we should get the codec information of the remote
1748 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1749 * meaningful value based on this information but unfortunately we
1750 * can't do that yet because the mncc API can not signal dynamic
1751 * payload types yet. This must be fixed first. Also there may be
1752 * additional members necessary in trans->conn->rtp because we
1753 * somehow need to deal with dynamic payload types that do not
1754 * comply to 3gpp's assumptions of payload type numbers on the A
1755 * interface. See also related tickets: OS#3399 and OS1683 */
1756
Harald Welte27989d42018-06-21 20:39:20 +02001757 /* Find callref */
1758 trans = trans_find_by_callref(net, rtp->callref);
1759 if (!trans) {
1760 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
1761 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
1762 return -EIO;
1763 }
1764 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1765 if (!trans->conn) {
1766 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
1767 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
1768 return 0;
1769 }
1770
1771 addr.s_addr = osmo_htonl(rtp->ip);
1772 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
1773}
1774
1775static struct downstate {
1776 uint32_t states;
1777 int type;
1778 int (*rout) (struct gsm_trans *trans, void *arg);
1779} downstatelist[] = {
1780 /* mobile originating call establishment */
1781 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1782 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1783 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1784 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1785 {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 */
1786 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1787 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1788 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1789 /* mobile terminating call establishment */
1790 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1791 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1792 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1793 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1794 /* signalling during call */
1795 {SBIT(GSM_CSTATE_ACTIVE),
1796 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1797 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1798 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1799 {ALL_STATES,
1800 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1801 {ALL_STATES,
1802 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1803 {ALL_STATES,
1804 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1805 {SBIT(GSM_CSTATE_ACTIVE),
1806 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1807 {SBIT(GSM_CSTATE_ACTIVE),
1808 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1809 {SBIT(GSM_CSTATE_ACTIVE),
1810 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1811 {SBIT(GSM_CSTATE_ACTIVE),
1812 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1813 {SBIT(GSM_CSTATE_ACTIVE),
1814 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1815 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1816 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1817 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1818 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1819 {SBIT(GSM_CSTATE_ACTIVE),
1820 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1821 /* clearing */
1822 {SBIT(GSM_CSTATE_INITIATED),
1823 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1824 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1825 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1826 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1827 MNCC_REL_REQ, gsm48_cc_tx_release},
1828};
1829
1830#define DOWNSLLEN \
1831 (sizeof(downstatelist) / sizeof(struct downstate))
1832
1833
1834int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
1835{
1836 int i, rc = 0;
1837 struct gsm_trans *trans = NULL, *transt;
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001838 struct ran_conn *conn = NULL;
Harald Welte27989d42018-06-21 20:39:20 +02001839 struct gsm_mncc *data = arg, rel;
1840
1841 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
1842
1843 /* handle special messages */
1844 switch(msg_type) {
1845 case MNCC_BRIDGE:
1846 rc = tch_bridge(net, arg);
1847 if (rc < 0)
1848 disconnect_bridge(net, arg, -rc);
1849 return rc;
1850 case MNCC_RTP_CREATE:
1851 return tch_rtp_create(net, data->callref);
1852 case MNCC_RTP_CONNECT:
1853 return tch_rtp_connect(net, arg);
1854 case MNCC_RTP_FREE:
1855 /* unused right now */
1856 return -EIO;
1857
1858 case MNCC_FRAME_DROP:
1859 case MNCC_FRAME_RECV:
1860 case GSM_TCHF_FRAME:
1861 case GSM_TCHF_FRAME_EFR:
1862 case GSM_TCHH_FRAME:
1863 case GSM_TCH_FRAME_AMR:
1864 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
1865 get_mncc_name(msg_type));
1866 return -ENOTSUP;
1867 }
1868
1869 memset(&rel, 0, sizeof(struct gsm_mncc));
1870 rel.callref = data->callref;
1871
1872 /* Find callref */
1873 trans = trans_find_by_callref(net, data->callref);
1874
1875 /* Callref unknown */
1876 if (!trans) {
1877 struct vlr_subscr *vsub;
1878
1879 if (msg_type != MNCC_SETUP_REQ) {
1880 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1881 "Received '%s' from MNCC with "
1882 "unknown callref %d\n", data->called.number,
1883 get_mncc_name(msg_type), data->callref);
1884 /* Invalid call reference */
1885 return mncc_release_ind(net, NULL, data->callref,
1886 GSM48_CAUSE_LOC_PRN_S_LU,
1887 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1888 }
1889 if (!data->called.number[0] && !data->imsi[0]) {
1890 DEBUGP(DCC, "(bts - trx - ts - ti) "
1891 "Received '%s' from MNCC with "
1892 "no number or IMSI\n", get_mncc_name(msg_type));
1893 /* Invalid number */
1894 return mncc_release_ind(net, NULL, data->callref,
1895 GSM48_CAUSE_LOC_PRN_S_LU,
1896 GSM48_CC_CAUSE_INV_NR_FORMAT);
1897 }
1898 /* New transaction due to setup, find subscriber */
1899 if (data->called.number[0])
1900 vsub = vlr_subscr_find_by_msisdn(net->vlr,
1901 data->called.number);
1902 else
1903 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
1904
1905 /* update the subscriber we deal with */
1906 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1907
1908 /* If subscriber is not found */
1909 if (!vsub) {
1910 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1911 "Received '%s' from MNCC with "
1912 "unknown subscriber %s\n", data->called.number,
1913 get_mncc_name(msg_type), data->called.number);
1914 /* Unknown subscriber */
1915 return mncc_release_ind(net, NULL, data->callref,
1916 GSM48_CAUSE_LOC_PRN_S_LU,
1917 GSM48_CC_CAUSE_UNASSIGNED_NR);
1918 }
1919 /* If subscriber is not "attached" */
Max7d41d872018-12-19 11:48:33 +01001920 if (!vsub->cgi.lai.lac) {
Harald Welte27989d42018-06-21 20:39:20 +02001921 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1922 "Received '%s' from MNCC with "
1923 "detached subscriber %s\n", data->called.number,
1924 get_mncc_name(msg_type), vlr_subscr_name(vsub));
1925 vlr_subscr_put(vsub);
1926 /* Temporarily out of order */
1927 return mncc_release_ind(net, NULL, data->callref,
1928 GSM48_CAUSE_LOC_PRN_S_LU,
1929 GSM48_CC_CAUSE_DEST_OOO);
1930 }
1931 /* Create transaction */
1932 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
1933 if (!trans) {
Max3614fd62019-01-17 13:38:10 +01001934 LOGP(DCC, LOGL_ERROR, "No memory for trans.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001935 vlr_subscr_put(vsub);
1936 /* Ressource unavailable */
1937 mncc_release_ind(net, NULL, data->callref,
1938 GSM48_CAUSE_LOC_PRN_S_LU,
1939 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1940 return -ENOMEM;
1941 }
1942
1943 /* Find conn */
1944 conn = connection_for_subscr(vsub);
1945
1946 /* If subscriber has no conn */
1947 if (!conn) {
1948 /* find transaction with this subscriber already paging */
1949 llist_for_each_entry(transt, &net->trans_list, entry) {
1950 /* Transaction of our conn? */
1951 if (transt == trans ||
1952 transt->vsub != vsub)
1953 continue;
1954 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1955 "Received '%s' from MNCC with "
1956 "unallocated channel, paging already "
1957 "started for lac %d.\n",
1958 data->called.number,
Max7d41d872018-12-19 11:48:33 +01001959 get_mncc_name(msg_type), vsub->cgi.lai.lac);
Harald Welte27989d42018-06-21 20:39:20 +02001960 vlr_subscr_put(vsub);
1961 trans_free(trans);
1962 return 0;
1963 }
1964 /* store setup information until paging succeeds */
1965 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1966
1967 /* Request a channel */
1968 trans->paging_request = subscr_request_conn(
1969 vsub,
1970 setup_trig_pag_evt,
1971 trans,
Harald Welte0df904d2018-12-03 11:00:04 +01001972 "MNCC: establish call",
1973 SGSAP_SERV_IND_CS_CALL);
Harald Welte27989d42018-06-21 20:39:20 +02001974 if (!trans->paging_request) {
1975 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
1976 vlr_subscr_put(vsub);
1977 trans_free(trans);
1978 return 0;
1979 }
1980 vlr_subscr_put(vsub);
1981 return 0;
1982 }
1983
1984 /* Assign conn */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001985 trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001986 trans->dlci = 0x00; /* SAPI=0, not SACCH */
1987 vlr_subscr_put(vsub);
1988 } else {
1989 /* update the subscriber we deal with */
1990 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1991 }
1992
Philipp Maier9ca7b312018-10-10 17:00:49 +02001993 gsm48_start_guard_timer(trans);
1994
Harald Welte27989d42018-06-21 20:39:20 +02001995 if (trans->conn)
1996 conn = trans->conn;
1997
1998 /* if paging did not respond yet */
1999 if (!conn) {
2000 DEBUGP(DCC, "(sub %s) "
2001 "Received '%s' from MNCC in paging state\n",
2002 vlr_subscr_msisdn_or_name(trans->vsub),
2003 get_mncc_name(msg_type));
2004 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2005 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
2006 if (msg_type == MNCC_REL_REQ)
2007 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2008 else
2009 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2010 trans->callref = 0;
2011 trans_free(trans);
2012 return rc;
2013 } else {
2014 DEBUGP(DCC, "(ti %02x sub %s) "
2015 "Received '%s' from MNCC in state %d (%s)\n",
2016 trans->transaction_id,
2017 vlr_subscr_msisdn_or_name(trans->conn->vsub),
2018 get_mncc_name(msg_type), trans->cc.state,
2019 gsm48_cc_state_name(trans->cc.state));
2020 }
2021
2022 /* Find function for current state and message */
2023 for (i = 0; i < DOWNSLLEN; i++)
2024 if ((msg_type == downstatelist[i].type)
2025 && ((1 << trans->cc.state) & downstatelist[i].states))
2026 break;
2027 if (i == DOWNSLLEN) {
Max964da932018-12-17 16:00:22 +01002028 DEBUGP(DCC, "Message '%s' unhandled at state '%s'\n", get_mncc_name(msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002029 return 0;
2030 }
2031
2032 rc = downstatelist[i].rout(trans, arg);
2033
2034 return rc;
2035}
2036
2037
2038static struct datastate {
2039 uint32_t states;
2040 int type;
2041 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2042} datastatelist[] = {
2043 /* mobile originating call establishment */
2044 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2045 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2046 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2047 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2048 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2049 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2050 /* mobile terminating call establishment */
2051 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2052 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2053 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2054 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2055 {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 */
2056 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2057 /* signalling during call */
2058 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2059 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2060 {SBIT(GSM_CSTATE_ACTIVE),
2061 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2062 {ALL_STATES,
2063 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2064 {ALL_STATES,
2065 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2066 {ALL_STATES,
2067 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2068 {SBIT(GSM_CSTATE_ACTIVE),
2069 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2070 {SBIT(GSM_CSTATE_ACTIVE),
2071 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2072 {SBIT(GSM_CSTATE_ACTIVE),
2073 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2074 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2075 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2076 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2077 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2078 {SBIT(GSM_CSTATE_ACTIVE),
2079 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2080 /* clearing */
2081 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2082 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2083 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2084 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2085 {ALL_STATES, /* 5.4.3.4 */
2086 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2087};
2088
2089#define DATASLLEN \
2090 (sizeof(datastatelist) / sizeof(struct datastate))
2091
Neels Hofmeyrc036b792018-11-29 22:37:51 +01002092int gsm0408_rcv_cc(struct ran_conn *conn, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002093{
2094 struct gsm48_hdr *gh = msgb_l3(msg);
2095 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2096 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2097 struct gsm_trans *trans = NULL;
2098 int i, rc = 0;
2099
2100 if (msg_type & 0x80) {
2101 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
2102 return -EINVAL;
2103 }
2104
2105 if (!conn->vsub) {
2106 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
2107 return -EINVAL;
2108 }
2109
2110 /* Find transaction */
2111 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
2112
2113#if BEFORE_MSCSPLIT
2114 /* Re-enable this log output once we can obtain this information via
2115 * A-interface, see OS#2391. */
2116 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
2117 "Received '%s' from MS in state %d (%s)\n",
2118 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
2119 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
2120 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
2121 gsm48_cc_state_name(trans?(trans->cc.state):0));
2122#endif
2123
2124 /* Create transaction */
2125 if (!trans) {
2126 DEBUGP(DCC, "Unknown transaction ID %x, "
2127 "creating new trans.\n", transaction_id);
2128 /* Create transaction */
2129 trans = trans_alloc(conn->network, conn->vsub,
2130 GSM48_PDISC_CC,
2131 transaction_id, new_callref++);
2132 if (!trans) {
Max3614fd62019-01-17 13:38:10 +01002133 LOGP(DCC, LOGL_ERROR, "No memory for trans.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002134 rc = gsm48_tx_simple(conn,
2135 GSM48_PDISC_CC | (transaction_id << 4),
2136 GSM48_MT_CC_RELEASE_COMPL);
2137 return -ENOMEM;
2138 }
2139 /* Assign transaction */
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01002140 trans->conn = ran_conn_get(conn, RAN_CONN_USE_TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002141 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
2142 cm_service_request_concludes(conn, msg);
2143 }
2144
2145 /* find function for current state and message */
2146 for (i = 0; i < DATASLLEN; i++)
2147 if ((msg_type == datastatelist[i].type)
2148 && ((1 << trans->cc.state) & datastatelist[i].states))
2149 break;
2150 if (i == DATASLLEN) {
2151 DEBUGP(DCC, "Message unhandled at this state.\n");
2152 return 0;
2153 }
2154
2155 assert(trans->vsub);
2156
2157 rc = datastatelist[i].rout(trans, msg);
2158
Neels Hofmeyrc036b792018-11-29 22:37:51 +01002159 ran_conn_communicating(conn);
Harald Welte27989d42018-06-21 20:39:20 +02002160 return rc;
2161}