blob: d6a2864e72a150f0bed91c7631dbb6650815c6ff [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
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +020033#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
34
Harald Welte27989d42018-06-21 20:39:20 +020035#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>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010051#include <osmocom/msc/msub.h>
52#include <osmocom/msc/msc_a.h>
53#include <osmocom/msc/paging.h>
54#include <osmocom/msc/call_leg.h>
55#include <osmocom/msc/rtp_stream.h>
56#include <osmocom/msc/mncc_call.h>
57#include <osmocom/msc/msc_t.h>
Harald Welte27989d42018-06-21 20:39:20 +020058
59#include <osmocom/gsm/gsm48.h>
60#include <osmocom/gsm/gsm0480.h>
61#include <osmocom/gsm/gsm_utils.h>
62#include <osmocom/gsm/protocol/gsm_04_08.h>
63#include <osmocom/core/msgb.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/core/utils.h>
66#include <osmocom/core/byteswap.h>
67#include <osmocom/gsm/tlv.h>
68#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020069
70#include <assert.h>
71
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010072static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
73static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
75
76static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
77{
78 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
79 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
80 OMSC_LINKID_CB(msg) = trans->dlci;
81
82 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
83}
84
85uint32_t msc_cc_next_outgoing_callref() {
86 static uint32_t last_callref = 0x80000000;
87 last_callref++;
88 if (last_callref < 0x80000001)
89 last_callref = 0x80000001;
90 return last_callref;
91}
Harald Welte27989d42018-06-21 20:39:20 +020092
Philipp Maier9ca7b312018-10-10 17:00:49 +020093static void gsm48_cc_guard_timeout(void *arg)
94{
95 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010096 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020097 trans_free(trans);
98 return;
99}
100
101static void gsm48_stop_guard_timer(struct gsm_trans *trans)
102{
103 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100104 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200105 osmo_timer_del(&trans->cc.timer_guard);
106 }
107}
108
109static void gsm48_start_guard_timer(struct gsm_trans *trans)
110{
111 /* NOTE: The purpose of this timer is to prevent the cc state machine
112 * from hanging in cases where mncc, gsm48 or both become unresponsive
113 * for some reason. The timer is started initially with the setup from
114 * the gsm48 side and then re-started with every incoming mncc message.
115 * Once the mncc state reaches its active state the timer is stopped.
116 * So if the cc state machine does not show any activity for an
117 * extended amount of time during call setup or teardown the guard
118 * timer will time out and hard-clear the connection. */
119 if (osmo_timer_pending(&trans->cc.timer_guard))
120 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100121 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200122 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
123 osmo_timer_schedule(&trans->cc.timer_guard,
124 trans->net->mncc_guard_timeout, 0);
125}
Harald Welte27989d42018-06-21 20:39:20 +0200126
127/* Call Control */
128
129void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
130{
131 net->mncc_recv(net, msg);
132}
133
134int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
135{
136 struct gsm48_hdr *gh;
137 struct msgb *ss_notify;
138
139 ss_notify = gsm0480_create_notifySS(message);
140 if (!ss_notify)
141 return -1;
142
143 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
144 uint8_t *data = msgb_push(ss_notify, 1);
145 data[0] = ss_notify->len - 1;
146 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
147 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100148 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200149}
150
151/* FIXME: this count_statistics is a state machine behaviour. we should convert
152 * the complete call control into a state machine. Afterwards we can move this
153 * code into state transitions.
154 */
155static void count_statistics(struct gsm_trans *trans, int new_state)
156{
157 int old_state = trans->cc.state;
158 struct rate_ctr_group *msc = trans->net->msc_ctrs;
159
160 if (old_state == new_state)
161 return;
162
163 /* state incoming */
164 switch (new_state) {
165 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200166 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
167 1);
168 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200169 break;
170 }
171
172 /* state outgoing */
173 switch (old_state) {
174 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200175 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
176 1);
Harald Welte27989d42018-06-21 20:39:20 +0200177 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
178 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200179 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200180 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200181 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200182 break;
183 }
184}
185
Harald Welte27989d42018-06-21 20:39:20 +0200186static void new_cc_state(struct gsm_trans *trans, int state)
187{
188 if (state > 31 || state < 0)
189 return;
190
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100191 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
192 gsm48_cc_state_name(trans->cc.state),
193 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200194
195 count_statistics(trans, state);
196 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200197
198 /* Stop the guard timer when a call reaches the active state */
199 if (state == GSM_CSTATE_ACTIVE)
200 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200201}
202
203static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
204{
205 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
207 uint8_t *cause, *call_state;
208
209 gh->msg_type = GSM48_MT_CC_STATUS;
210
211 cause = msgb_put(msg, 3);
212 cause[0] = 2;
213 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
214 cause[2] = 0x80 | 30; /* response to status inquiry */
215
216 call_state = msgb_put(msg, 1);
217 call_state[0] = 0xc0 | 0x00;
218
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100219 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200220}
221
222static void gsm48_stop_cc_timer(struct gsm_trans *trans)
223{
224 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100225 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200226 osmo_timer_del(&trans->cc.timer);
227 trans->cc.Tcurrent = 0;
228 }
229}
230
231static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
232 int msg_type, struct gsm_mncc *mncc)
233{
234 struct msgb *msg;
235 unsigned char *data;
236
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100237 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200238
239 mncc->msg_type = msg_type;
240
241 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
242 if (!msg)
243 return -ENOMEM;
244
245 data = msgb_put(msg, sizeof(struct gsm_mncc));
246 memcpy(data, mncc, sizeof(struct gsm_mncc));
247
248 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200249 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
250 if (trans)
251 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200252
253 return 0;
254}
255
256int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
257 uint32_t callref, int location, int value)
258{
259 struct gsm_mncc rel;
260
261 memset(&rel, 0, sizeof(rel));
262 rel.callref = callref;
263 mncc_set_cause(&rel, location, value);
264 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
265 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
266 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
267}
268
269/* Call Control Specific transaction release.
270 * gets called by trans_free, DO NOT CALL YOURSELF! */
271void _gsm48_cc_trans_free(struct gsm_trans *trans)
272{
273 gsm48_stop_cc_timer(trans);
274
Harald Welte27989d42018-06-21 20:39:20 +0200275 /* send release to L4, if callref still exists */
276 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100277 /* Send MNCC REL.ind (cause='Resource unavailable') */
278 if (trans->cc.mncc_initiated) {
279 mncc_release_ind(trans->net, trans, trans->callref,
280 GSM48_CAUSE_LOC_PRN_S_LU,
281 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
282 }
283
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100284 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
285 * CC Release to the MS if it gets freed here. Hack it to do so. */
286 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
287 struct gsm_mncc rel = {};
288 rel.callref = trans->callref;
289 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
290 gsm48_cc_tx_release(trans, &rel);
291 }
Harald Welte27989d42018-06-21 20:39:20 +0200292 /* This is a final freeing of the transaction. The MNCC release may have triggered the
293 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
294 gsm48_stop_cc_timer(trans);
295 }
296 if (trans->cc.state != GSM_CSTATE_NULL)
297 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200298
299 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100300
301 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
302 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200303}
304
Harald Welte27989d42018-06-21 20:39:20 +0200305/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100306static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200307{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100308 if (trans->msc_a) {
309 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
310 "Handle paging error: transaction already associated with subscriber,"
311 " apparently it was already handled. Skip.\n");
312 return;
Harald Welte27989d42018-06-21 20:39:20 +0200313 }
314
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100315 if (msc_a) {
316 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
317 /* Assign conn */
318 msc_a_get(msc_a, MSC_A_USE_CC);
319 trans->msc_a = msc_a;
320 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200321
322 /* Get the GCR from the MO call leg (if any). */
323 if (!trans->cc.lcls) {
324 trans->cc.lcls = trans_lcls_compose(trans, true);
325 if (trans->cc.lcls) {
326 trans->cc.lcls->gcr = trans->cc.msg.gcr;
327 trans->cc.lcls->gcr_available = true;
328 }
329 }
330
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100331 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
332 /* send SETUP request to called party */
333 gsm48_cc_tx_setup(trans, &trans->cc.msg);
334 } else {
335 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
336 /* Temporarily out of order */
337 mncc_release_ind(trans->net, trans,
338 trans->callref,
339 GSM48_CAUSE_LOC_PRN_S_LU,
340 GSM48_CC_CAUSE_DEST_OOO);
341 trans->callref = 0;
342 trans->paging_request = NULL;
343 trans_free(trans);
344 }
Harald Welte27989d42018-06-21 20:39:20 +0200345}
346
347/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100348static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200349{
350 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
351 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100352 struct call_leg *cl1;
353 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200354
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100355 if (!trans1 || !trans2) {
356 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200357 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100358 }
Harald Welte27989d42018-06-21 20:39:20 +0200359
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100360 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100361 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
362 LOG_TRANS(trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
Harald Welte27989d42018-06-21 20:39:20 +0200363 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100364 }
365
366 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
367 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200368
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100369 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
370 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
371 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
372 * If we can't, then we must give up. */
373 cl1 = trans1->msc_a->cc.call_leg;
374 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200375
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100376 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200377}
378
379static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
380{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100381 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200382 return gsm48_cc_tx_status(trans, msg);
383}
384
Harald Welte27989d42018-06-21 20:39:20 +0200385static void gsm48_cc_timeout(void *arg)
386{
387 struct gsm_trans *trans = arg;
388 int disconnect = 0, release = 0;
389 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
390 int mo_location = GSM48_CAUSE_LOC_USER;
391 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
392 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
393 struct gsm_mncc mo_rel, l4_rel;
394
395 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
396 mo_rel.callref = trans->callref;
397 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
398 l4_rel.callref = trans->callref;
399
400 switch(trans->cc.Tcurrent) {
401 case 0x303:
402 release = 1;
403 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
404 break;
405 case 0x310:
406 disconnect = 1;
407 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
408 break;
409 case 0x313:
410 disconnect = 1;
411 /* unknown, did not find it in the specs */
412 break;
413 case 0x301:
414 disconnect = 1;
415 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
416 break;
417 case 0x308:
418 if (!trans->cc.T308_second) {
419 /* restart T308 a second time */
420 gsm48_cc_tx_release(trans, &trans->cc.msg);
421 trans->cc.T308_second = 1;
422 break; /* stay in release state */
423 }
424 trans_free(trans);
425 return;
426 case 0x306:
427 release = 1;
428 mo_cause = trans->cc.msg.cause.value;
429 mo_location = trans->cc.msg.cause.location;
430 break;
431 case 0x323:
432 disconnect = 1;
433 break;
434 default:
435 release = 1;
436 }
437
438 if (release && trans->callref) {
439 /* process release towards layer 4 */
440 mncc_release_ind(trans->net, trans, trans->callref,
441 l4_location, l4_cause);
442 trans->callref = 0;
443 }
444
445 if (disconnect && trans->callref) {
446 /* process disconnect towards layer 4 */
447 mncc_set_cause(&l4_rel, l4_location, l4_cause);
448 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
449 }
450
451 /* process disconnect towards mobile station */
452 if (disconnect || release) {
453 mncc_set_cause(&mo_rel, mo_location, mo_cause);
454 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
455 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
456 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
457 mo_rel.cause.diag_len = 3;
458
459 if (disconnect)
460 gsm48_cc_tx_disconnect(trans, &mo_rel);
461 if (release)
462 gsm48_cc_tx_release(trans, &mo_rel);
463 }
464
465}
466
467/* disconnect both calls from the bridge */
468static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100469 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200470{
471 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
472 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
473 struct gsm_mncc mx_rel;
474 if (!trans0 || !trans1)
475 return;
476
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100477 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
478 trans0->callref, trans1->callref, strerror(err));
479 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200480 trans0->callref, trans1->callref, strerror(err));
481
482 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
483 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
484 GSM48_CC_CAUSE_CHAN_UNACCEPT);
485
486 mx_rel.callref = trans0->callref;
487 gsm48_cc_tx_disconnect(trans0, &mx_rel);
488
489 mx_rel.callref = trans1->callref;
490 gsm48_cc_tx_disconnect(trans1, &mx_rel);
491}
492
493static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
494 int sec, int micro)
495{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100496 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200497 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
498 osmo_timer_schedule(&trans->cc.timer, sec, micro);
499 trans->cc.Tcurrent = current;
500}
501
502static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
503{
504 struct gsm48_hdr *gh = msgb_l3(msg);
505 uint8_t msg_type = gsm48_hdr_msg_type(gh);
506 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
507 struct tlv_parsed tp;
508 struct gsm_mncc setup;
509
Philipp Maier9ca7b312018-10-10 17:00:49 +0200510 gsm48_start_guard_timer(trans);
511
Harald Welte27989d42018-06-21 20:39:20 +0200512 memset(&setup, 0, sizeof(struct gsm_mncc));
513 setup.callref = trans->callref;
514
Keith Whytea1a70be2021-05-16 02:59:52 +0200515 /* New Global Call Reference */
516 if (!trans->cc.lcls)
517 trans->cc.lcls = trans_lcls_compose(trans, true);
518
519 /* Pass the LCLS GCR on to the MT call leg via MNCC */
520 if (trans->cc.lcls)
521 setup.gcr = trans->cc.lcls->gcr;
522
Harald Welte27989d42018-06-21 20:39:20 +0200523 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
524 /* emergency setup is identified by msg_type */
525 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
526 setup.fields |= MNCC_F_EMERGENCY;
527 setup.emergency = 1;
528 /* use destination number as configured by user (if any) */
529 if (trans->net->emergency.route_to_msisdn) {
530 setup.fields |= MNCC_F_CALLED;
531 setup.called.type = 0; /* unknown */
532 setup.called.plan = 0; /* unknown */
533 OSMO_STRLCPY_ARRAY(setup.called.number,
534 trans->net->emergency.route_to_msisdn);
535 }
536 }
537
538 /* use subscriber as calling party number */
539 setup.fields |= MNCC_F_CALLING;
540 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
541 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
542
543 /* bearer capability */
544 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
545 setup.fields |= MNCC_F_BEARER_CAP;
546 gsm48_decode_bearer_cap(&setup.bearer_cap,
547 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
548
549 /* Create a copy of the bearer capability
550 * in the transaction struct, so we can use
551 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100552 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200553 sizeof(trans->bearer_cap));
554 }
555 /* facility */
556 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
557 setup.fields |= MNCC_F_FACILITY;
558 gsm48_decode_facility(&setup.facility,
559 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
560 }
561 /* called party bcd number */
562 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
563 setup.fields |= MNCC_F_CALLED;
564 gsm48_decode_called(&setup.called,
565 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
566 }
567 /* user-user */
568 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
569 setup.fields |= MNCC_F_USERUSER;
570 gsm48_decode_useruser(&setup.useruser,
571 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
572 }
573 /* ss-version */
574 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
575 setup.fields |= MNCC_F_SSVERSION;
576 gsm48_decode_ssversion(&setup.ssversion,
577 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
578 }
579 /* CLIR suppression */
580 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
581 setup.clir.sup = 1;
582 /* CLIR invocation */
583 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
584 setup.clir.inv = 1;
585 /* cc cap */
586 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
587 setup.fields |= MNCC_F_CCCAP;
588 gsm48_decode_cccap(&setup.cccap,
589 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
590 }
591
592 new_cc_state(trans, GSM_CSTATE_INITIATED);
593
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100594 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
595 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200596
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200597 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200598
599 /* indicate setup to MNCC */
600 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
601
602 /* MNCC code will modify the channel asynchronously, we should
603 * ipaccess-bind only after the modification has been made to the
604 * lchan->tch_mode */
605 return 0;
606}
607
608static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
609{
610 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
611 struct gsm48_hdr *gh;
612 struct gsm_mncc *setup = arg;
613 int rc, trans_id;
614
615 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
616
617 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700618 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100619 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200620 "This is not allowed!\n");
621 /* Temporarily out of order */
622 rc = mncc_release_ind(trans->net, trans, trans->callref,
623 GSM48_CAUSE_LOC_PRN_S_LU,
624 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
625 trans->callref = 0;
626 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200627 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200628 return rc;
629 }
630
631 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100632 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200633 if (trans_id < 0) {
634 /* no free transaction ID */
635 rc = mncc_release_ind(trans->net, trans, trans->callref,
636 GSM48_CAUSE_LOC_PRN_S_LU,
637 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
638 trans->callref = 0;
639 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200640 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200641 return rc;
642 }
643 trans->transaction_id = trans_id;
644
645 gh->msg_type = GSM48_MT_CC_SETUP;
646
647 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
648
649 /* bearer capability */
650 if (setup->fields & MNCC_F_BEARER_CAP) {
651 /* Create a copy of the bearer capability in the transaction struct, so we
652 * can use this information later */
653 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
654 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
655 }
656 /* facility */
657 if (setup->fields & MNCC_F_FACILITY)
658 gsm48_encode_facility(msg, 0, &setup->facility);
659 /* progress */
660 if (setup->fields & MNCC_F_PROGRESS)
661 gsm48_encode_progress(msg, 0, &setup->progress);
662 /* calling party BCD number */
663 if (setup->fields & MNCC_F_CALLING)
664 gsm48_encode_calling(msg, &setup->calling);
665 /* called party BCD number */
666 if (setup->fields & MNCC_F_CALLED)
667 gsm48_encode_called(msg, &setup->called);
668 /* user-user */
669 if (setup->fields & MNCC_F_USERUSER)
670 gsm48_encode_useruser(msg, 0, &setup->useruser);
671 /* redirecting party BCD number */
672 if (setup->fields & MNCC_F_REDIRECTING)
673 gsm48_encode_redirecting(msg, &setup->redirecting);
674 /* signal */
675 if (setup->fields & MNCC_F_SIGNAL)
676 gsm48_encode_signal(msg, setup->signal);
677
678 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
679
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200680 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200681
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100682 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200683}
684
685static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
686{
687 struct gsm48_hdr *gh = msgb_l3(msg);
688 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
689 struct tlv_parsed tp;
690 struct gsm_mncc call_conf;
691 int rc;
692
693 gsm48_stop_cc_timer(trans);
694 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
695
696 memset(&call_conf, 0, sizeof(struct gsm_mncc));
697 call_conf.callref = trans->callref;
698
699 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
700#if 0
701 /* repeat */
702 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
703 call_conf.repeat = 1;
704 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
705 call_conf.repeat = 2;
706#endif
707 /* bearer capability */
708 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
709 call_conf.fields |= MNCC_F_BEARER_CAP;
710 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
711 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
712
713 /* Create a copy of the bearer capability
714 * in the transaction struct, so we can use
715 * this information later */
716 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
717 sizeof(trans->bearer_cap));
718 }
719 /* cause */
720 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
721 call_conf.fields |= MNCC_F_CAUSE;
722 gsm48_decode_cause(&call_conf.cause,
723 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
724 }
725 /* cc cap */
726 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
727 call_conf.fields |= MNCC_F_CCCAP;
728 gsm48_decode_cccap(&call_conf.cccap,
729 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
730 }
731
732 /* IMSI of called subscriber */
733 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
734
735 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
736
737 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100738 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200739
740 /* don't continue, if there were problems with
741 * the call assignment. */
742 if (rc)
743 return rc;
744
745 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
746 &call_conf);
747}
748
749static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
750{
751 struct gsm_mncc *proceeding = arg;
752 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
753 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
754 int rc;
755
756 gh->msg_type = GSM48_MT_CC_CALL_PROC;
757
758 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
759
760 /* bearer capability */
761 if (proceeding->fields & MNCC_F_BEARER_CAP) {
762 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
763 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
764 }
765 /* facility */
766 if (proceeding->fields & MNCC_F_FACILITY)
767 gsm48_encode_facility(msg, 0, &proceeding->facility);
768 /* progress */
769 if (proceeding->fields & MNCC_F_PROGRESS)
770 gsm48_encode_progress(msg, 0, &proceeding->progress);
771
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100772 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200773 if (rc)
774 return rc;
775
776 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100777 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200778}
779
780static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
781{
782 struct gsm48_hdr *gh = msgb_l3(msg);
783 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
784 struct tlv_parsed tp;
785 struct gsm_mncc alerting;
786
787 gsm48_stop_cc_timer(trans);
788 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
789
790 memset(&alerting, 0, sizeof(struct gsm_mncc));
791 alerting.callref = trans->callref;
792 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
793 /* facility */
794 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
795 alerting.fields |= MNCC_F_FACILITY;
796 gsm48_decode_facility(&alerting.facility,
797 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
798 }
799
800 /* progress */
801 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
802 alerting.fields |= MNCC_F_PROGRESS;
803 gsm48_decode_progress(&alerting.progress,
804 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
805 }
806 /* ss-version */
807 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
808 alerting.fields |= MNCC_F_SSVERSION;
809 gsm48_decode_ssversion(&alerting.ssversion,
810 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
811 }
812
813 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
814
815 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
816 &alerting);
817}
818
819static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
820{
821 struct gsm_mncc *alerting = arg;
822 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
823 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
824
825 gh->msg_type = GSM48_MT_CC_ALERTING;
826
827 /* facility */
828 if (alerting->fields & MNCC_F_FACILITY)
829 gsm48_encode_facility(msg, 0, &alerting->facility);
830 /* progress */
831 if (alerting->fields & MNCC_F_PROGRESS)
832 gsm48_encode_progress(msg, 0, &alerting->progress);
833 /* user-user */
834 if (alerting->fields & MNCC_F_USERUSER)
835 gsm48_encode_useruser(msg, 0, &alerting->useruser);
836
837 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
838
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100839 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200840}
841
842static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
843{
844 struct gsm_mncc *progress = arg;
845 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
846 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
847
848 gh->msg_type = GSM48_MT_CC_PROGRESS;
849
850 /* progress */
851 gsm48_encode_progress(msg, 1, &progress->progress);
852 /* user-user */
853 if (progress->fields & MNCC_F_USERUSER)
854 gsm48_encode_useruser(msg, 0, &progress->useruser);
855
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100856 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200857}
858
859static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
860{
861 struct gsm_mncc *connect = arg;
862 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
864
865 gh->msg_type = GSM48_MT_CC_CONNECT;
866
867 gsm48_stop_cc_timer(trans);
868 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
869
870 /* facility */
871 if (connect->fields & MNCC_F_FACILITY)
872 gsm48_encode_facility(msg, 0, &connect->facility);
873 /* progress */
874 if (connect->fields & MNCC_F_PROGRESS)
875 gsm48_encode_progress(msg, 0, &connect->progress);
876 /* connected number */
877 if (connect->fields & MNCC_F_CONNECTED)
878 gsm48_encode_connected(msg, &connect->connected);
879 /* user-user */
880 if (connect->fields & MNCC_F_USERUSER)
881 gsm48_encode_useruser(msg, 0, &connect->useruser);
882
883 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
884
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100885 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200886}
887
888static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
889{
890 struct gsm48_hdr *gh = msgb_l3(msg);
891 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
892 struct tlv_parsed tp;
893 struct gsm_mncc connect;
894
895 gsm48_stop_cc_timer(trans);
896
897 memset(&connect, 0, sizeof(struct gsm_mncc));
898 connect.callref = trans->callref;
899 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
900 /* use subscriber as connected party number */
901 connect.fields |= MNCC_F_CONNECTED;
902 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
903 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
904
905 /* facility */
906 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
907 connect.fields |= MNCC_F_FACILITY;
908 gsm48_decode_facility(&connect.facility,
909 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
910 }
911 /* user-user */
912 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
913 connect.fields |= MNCC_F_USERUSER;
914 gsm48_decode_useruser(&connect.useruser,
915 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
916 }
917 /* ss-version */
918 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
919 connect.fields |= MNCC_F_SSVERSION;
920 gsm48_decode_ssversion(&connect.ssversion,
921 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
922 }
923
924 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200925 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200926
927 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
928}
929
930
931static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
932{
933 struct gsm_mncc connect_ack;
934
935 gsm48_stop_cc_timer(trans);
936
937 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200938 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_CONNECT_ACK));
Harald Welte27989d42018-06-21 20:39:20 +0200939
940 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
941 connect_ack.callref = trans->callref;
942
943 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
944 &connect_ack);
945}
946
947static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
948{
949 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
950 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
951
952 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
953
954 new_cc_state(trans, GSM_CSTATE_ACTIVE);
955
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100956 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200957}
958
959static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
960{
961 struct gsm48_hdr *gh = msgb_l3(msg);
962 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
963 struct tlv_parsed tp;
964 struct gsm_mncc disc;
965
966 gsm48_stop_cc_timer(trans);
967
968 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
969
970 memset(&disc, 0, sizeof(struct gsm_mncc));
971 disc.callref = trans->callref;
972 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
973 /* cause */
974 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
975 disc.fields |= MNCC_F_CAUSE;
976 gsm48_decode_cause(&disc.cause,
977 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
978 }
979 /* facility */
980 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
981 disc.fields |= MNCC_F_FACILITY;
982 gsm48_decode_facility(&disc.facility,
983 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
984 }
985 /* user-user */
986 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
987 disc.fields |= MNCC_F_USERUSER;
988 gsm48_decode_useruser(&disc.useruser,
989 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
990 }
991 /* ss-version */
992 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
993 disc.fields |= MNCC_F_SSVERSION;
994 gsm48_decode_ssversion(&disc.ssversion,
995 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
996 }
997
998 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +0200999}
1000
1001static struct gsm_mncc_cause default_cause = {
1002 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1003 .coding = 0,
1004 .rec = 0,
1005 .rec_val = 0,
1006 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1007 .diag_len = 0,
1008 .diag = { 0 },
1009};
1010
1011static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1012{
1013 struct gsm_mncc *disc = arg;
1014 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1015 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1016
1017 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1018
1019 gsm48_stop_cc_timer(trans);
1020 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1021
1022 /* cause */
1023 if (disc->fields & MNCC_F_CAUSE)
1024 gsm48_encode_cause(msg, 1, &disc->cause);
1025 else
1026 gsm48_encode_cause(msg, 1, &default_cause);
1027
1028 /* facility */
1029 if (disc->fields & MNCC_F_FACILITY)
1030 gsm48_encode_facility(msg, 0, &disc->facility);
1031 /* progress */
1032 if (disc->fields & MNCC_F_PROGRESS)
1033 gsm48_encode_progress(msg, 0, &disc->progress);
1034 /* user-user */
1035 if (disc->fields & MNCC_F_USERUSER)
1036 gsm48_encode_useruser(msg, 0, &disc->useruser);
1037
1038 /* store disconnect cause for T306 expiry */
1039 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1040
1041 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1042
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001043 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001044}
1045
1046static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1047{
1048 struct gsm48_hdr *gh = msgb_l3(msg);
1049 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1050 struct tlv_parsed tp;
1051 struct gsm_mncc rel;
1052 int rc;
1053
1054 gsm48_stop_cc_timer(trans);
1055
1056 memset(&rel, 0, sizeof(struct gsm_mncc));
1057 rel.callref = trans->callref;
1058 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1059 /* cause */
1060 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1061 rel.fields |= MNCC_F_CAUSE;
1062 gsm48_decode_cause(&rel.cause,
1063 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1064 }
1065 /* facility */
1066 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1067 rel.fields |= MNCC_F_FACILITY;
1068 gsm48_decode_facility(&rel.facility,
1069 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1070 }
1071 /* user-user */
1072 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1073 rel.fields |= MNCC_F_USERUSER;
1074 gsm48_decode_useruser(&rel.useruser,
1075 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1076 }
1077 /* ss-version */
1078 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1079 rel.fields |= MNCC_F_SSVERSION;
1080 gsm48_decode_ssversion(&rel.ssversion,
1081 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1082 }
1083
1084 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1085 /* release collision 5.4.5 */
1086 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1087 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001088 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001089 GSM48_PDISC_CC | (trans->transaction_id << 4),
1090 GSM48_MT_CC_RELEASE_COMPL);
1091 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1092 }
1093
1094 new_cc_state(trans, GSM_CSTATE_NULL);
1095
1096 trans->callref = 0;
1097 trans_free(trans);
1098
1099 return rc;
1100}
1101
1102static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1103{
1104 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001105 struct msgb *msg;
1106 struct gsm48_hdr *gh;
1107
1108 if (!trans->msc_a) {
1109 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1110 return -EINVAL;
1111 }
1112
1113 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1114 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001115
1116 gh->msg_type = GSM48_MT_CC_RELEASE;
1117
1118 gsm48_stop_cc_timer(trans);
1119 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1120
1121 /* cause */
1122 if (rel->fields & MNCC_F_CAUSE)
1123 gsm48_encode_cause(msg, 0, &rel->cause);
1124 /* facility */
1125 if (rel->fields & MNCC_F_FACILITY)
1126 gsm48_encode_facility(msg, 0, &rel->facility);
1127 /* user-user */
1128 if (rel->fields & MNCC_F_USERUSER)
1129 gsm48_encode_useruser(msg, 0, &rel->useruser);
1130
1131 trans->cc.T308_second = 0;
1132 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1133
1134 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1135 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1136
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001137 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001138}
1139
1140static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1141{
1142 struct gsm48_hdr *gh = msgb_l3(msg);
1143 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1144 struct tlv_parsed tp;
1145 struct gsm_mncc rel;
1146 int rc = 0;
1147
1148 gsm48_stop_cc_timer(trans);
1149
1150 memset(&rel, 0, sizeof(struct gsm_mncc));
1151 rel.callref = trans->callref;
1152 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1153 /* cause */
1154 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1155 rel.fields |= MNCC_F_CAUSE;
1156 gsm48_decode_cause(&rel.cause,
1157 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1158 }
1159 /* facility */
1160 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1161 rel.fields |= MNCC_F_FACILITY;
1162 gsm48_decode_facility(&rel.facility,
1163 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1164 }
1165 /* user-user */
1166 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1167 rel.fields |= MNCC_F_USERUSER;
1168 gsm48_decode_useruser(&rel.useruser,
1169 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1170 }
1171 /* ss-version */
1172 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1173 rel.fields |= MNCC_F_SSVERSION;
1174 gsm48_decode_ssversion(&rel.ssversion,
1175 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1176 }
1177
1178 if (trans->callref) {
1179 switch (trans->cc.state) {
1180 case GSM_CSTATE_CALL_PRESENT:
1181 rc = mncc_recvmsg(trans->net, trans,
1182 MNCC_REJ_IND, &rel);
1183 break;
1184 case GSM_CSTATE_RELEASE_REQ:
1185 rc = mncc_recvmsg(trans->net, trans,
1186 MNCC_REL_CNF, &rel);
1187 break;
1188 default:
1189 rc = mncc_recvmsg(trans->net, trans,
1190 MNCC_REL_IND, &rel);
1191 }
1192 }
1193
1194 trans->callref = 0;
1195 trans_free(trans);
1196
1197 return rc;
1198}
1199
1200static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1201{
1202 struct gsm_mncc *rel = arg;
1203 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1205 int ret;
1206
1207 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1208
1209 trans->callref = 0;
1210
1211 gsm48_stop_cc_timer(trans);
1212
1213 /* cause */
1214 if (rel->fields & MNCC_F_CAUSE)
1215 gsm48_encode_cause(msg, 0, &rel->cause);
1216 /* facility */
1217 if (rel->fields & MNCC_F_FACILITY)
1218 gsm48_encode_facility(msg, 0, &rel->facility);
1219 /* user-user */
1220 if (rel->fields & MNCC_F_USERUSER)
1221 gsm48_encode_useruser(msg, 0, &rel->useruser);
1222
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001223 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001224
1225 trans_free(trans);
1226
1227 return ret;
1228}
1229
1230static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1231{
1232 struct gsm48_hdr *gh = msgb_l3(msg);
1233 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1234 struct tlv_parsed tp;
1235 struct gsm_mncc fac;
1236
1237 memset(&fac, 0, sizeof(struct gsm_mncc));
1238 fac.callref = trans->callref;
1239 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1240 /* facility */
1241 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1242 fac.fields |= MNCC_F_FACILITY;
1243 gsm48_decode_facility(&fac.facility,
1244 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1245 }
1246 /* ss-version */
1247 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1248 fac.fields |= MNCC_F_SSVERSION;
1249 gsm48_decode_ssversion(&fac.ssversion,
1250 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1251 }
1252
1253 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1254}
1255
1256static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1257{
1258 struct gsm_mncc *fac = arg;
1259 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1260 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1261
1262 gh->msg_type = GSM48_MT_CC_FACILITY;
1263
1264 /* facility */
1265 gsm48_encode_facility(msg, 1, &fac->facility);
1266
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001267 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001268}
1269
1270static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1271{
1272 struct gsm_mncc hold;
1273
1274 memset(&hold, 0, sizeof(struct gsm_mncc));
1275 hold.callref = trans->callref;
1276 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1277}
1278
1279static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1280{
1281 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1282 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1283
1284 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1285
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001286 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001287}
1288
1289static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1290{
1291 struct gsm_mncc *hold_rej = arg;
1292 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1293 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1294
1295 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1296
1297 /* cause */
1298 if (hold_rej->fields & MNCC_F_CAUSE)
1299 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1300 else
1301 gsm48_encode_cause(msg, 1, &default_cause);
1302
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001303 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001304}
1305
1306static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1307{
1308 struct gsm_mncc retrieve;
1309
1310 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1311 retrieve.callref = trans->callref;
1312 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1313 &retrieve);
1314}
1315
1316static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1317{
1318 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1319 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1320
1321 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1322
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001323 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001324}
1325
1326static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1327{
1328 struct gsm_mncc *retrieve_rej = arg;
1329 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1330 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1331
1332 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1333
1334 /* cause */
1335 if (retrieve_rej->fields & MNCC_F_CAUSE)
1336 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1337 else
1338 gsm48_encode_cause(msg, 1, &default_cause);
1339
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001340 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001341}
1342
1343static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1344{
1345 struct gsm48_hdr *gh = msgb_l3(msg);
1346 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1347 struct tlv_parsed tp;
1348 struct gsm_mncc dtmf;
1349
1350 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1351 dtmf.callref = trans->callref;
1352 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1353 /* keypad facility */
1354 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1355 dtmf.fields |= MNCC_F_KEYPAD;
1356 gsm48_decode_keypad(&dtmf.keypad,
1357 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1358 }
1359
1360 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1361}
1362
1363static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1364{
1365 struct gsm_mncc *dtmf = arg;
1366 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1367 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1368
1369 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1370
1371 /* keypad */
1372 if (dtmf->fields & MNCC_F_KEYPAD)
1373 gsm48_encode_keypad(msg, dtmf->keypad);
1374
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001375 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001376}
1377
1378static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1379{
1380 struct gsm_mncc *dtmf = arg;
1381 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1383
1384 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1385
1386 /* cause */
1387 if (dtmf->fields & MNCC_F_CAUSE)
1388 gsm48_encode_cause(msg, 1, &dtmf->cause);
1389 else
1390 gsm48_encode_cause(msg, 1, &default_cause);
1391
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001392 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001393}
1394
1395static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1396{
1397 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1398 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1399
1400 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1401
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001402 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001403}
1404
1405static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1406{
1407 struct gsm_mncc dtmf;
1408
1409 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1410 dtmf.callref = trans->callref;
1411
1412 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1413}
1414
1415static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1416{
1417 struct gsm48_hdr *gh = msgb_l3(msg);
1418 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1419 struct tlv_parsed tp;
1420 struct gsm_mncc modify;
1421
1422 memset(&modify, 0, sizeof(struct gsm_mncc));
1423 modify.callref = trans->callref;
1424 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1425 /* bearer capability */
1426 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1427 modify.fields |= MNCC_F_BEARER_CAP;
1428 gsm48_decode_bearer_cap(&modify.bearer_cap,
1429 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1430
1431 /* Create a copy of the bearer capability
1432 * in the transaction struct, so we can use
1433 * this information later */
1434 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1435 sizeof(trans->bearer_cap));
1436 }
1437
1438 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1439
1440 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1441}
1442
1443static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1444{
1445 struct gsm_mncc *modify = arg;
1446 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1447 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1448
1449 gh->msg_type = GSM48_MT_CC_MODIFY;
1450
1451 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1452
1453 /* bearer capability */
1454 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1455 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1456
1457 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1458
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001459 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001460}
1461
1462static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1463{
1464 struct gsm48_hdr *gh = msgb_l3(msg);
1465 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1466 struct tlv_parsed tp;
1467 struct gsm_mncc modify;
1468
1469 gsm48_stop_cc_timer(trans);
1470
1471 memset(&modify, 0, sizeof(struct gsm_mncc));
1472 modify.callref = trans->callref;
1473 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1474 /* bearer capability */
1475 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1476 modify.fields |= MNCC_F_BEARER_CAP;
1477 gsm48_decode_bearer_cap(&modify.bearer_cap,
1478 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1479
1480 /* Create a copy of the bearer capability
1481 * in the transaction struct, so we can use
1482 * this information later */
1483 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1484 sizeof(trans->bearer_cap));
1485 }
1486
1487 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1488
1489 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1490}
1491
1492static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1493{
1494 struct gsm_mncc *modify = arg;
1495 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1496 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1497
1498 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1499
1500 /* bearer capability */
1501 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1502 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1503
1504 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1505
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001506 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001507}
1508
1509static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1510{
1511 struct gsm48_hdr *gh = msgb_l3(msg);
1512 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1513 struct tlv_parsed tp;
1514 struct gsm_mncc modify;
1515
1516 gsm48_stop_cc_timer(trans);
1517
1518 memset(&modify, 0, sizeof(struct gsm_mncc));
1519 modify.callref = trans->callref;
1520 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1521 /* bearer capability */
1522 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1523 modify.fields |= GSM48_IE_BEARER_CAP;
1524 gsm48_decode_bearer_cap(&modify.bearer_cap,
1525 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1526
1527 /* Create a copy of the bearer capability
1528 * in the transaction struct, so we can use
1529 * this information later */
1530 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1531 sizeof(trans->bearer_cap));
1532 }
1533 /* cause */
1534 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1535 modify.fields |= MNCC_F_CAUSE;
1536 gsm48_decode_cause(&modify.cause,
1537 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1538 }
1539
1540 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1541
1542 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1543}
1544
1545static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1546{
1547 struct gsm_mncc *modify = arg;
1548 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1549 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1550
1551 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1552
1553 /* bearer capability */
1554 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1555 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1556 /* cause */
1557 gsm48_encode_cause(msg, 1, &modify->cause);
1558
1559 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1560
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001561 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001562}
1563
1564static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1565{
1566 struct gsm_mncc *notify = arg;
1567 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1568 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1569
1570 gh->msg_type = GSM48_MT_CC_NOTIFY;
1571
1572 /* notify */
1573 gsm48_encode_notify(msg, notify->notify);
1574
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001575 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001576}
1577
1578static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1579{
1580 struct gsm48_hdr *gh = msgb_l3(msg);
1581 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1582// struct tlv_parsed tp;
1583 struct gsm_mncc notify;
1584
1585 memset(&notify, 0, sizeof(struct gsm_mncc));
1586 notify.callref = trans->callref;
1587// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1588 if (payload_len >= 1)
1589 gsm48_decode_notify(&notify.notify, gh->data);
1590
1591 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1592}
1593
1594static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1595{
1596 struct gsm_mncc *user = arg;
1597 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1598 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1599
1600 gh->msg_type = GSM48_MT_CC_USER_INFO;
1601
1602 /* user-user */
1603 if (user->fields & MNCC_F_USERUSER)
1604 gsm48_encode_useruser(msg, 1, &user->useruser);
1605 /* more data */
1606 if (user->more)
1607 gsm48_encode_more(msg);
1608
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001609 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001610}
1611
1612static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1613{
1614 struct gsm48_hdr *gh = msgb_l3(msg);
1615 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1616 struct tlv_parsed tp;
1617 struct gsm_mncc user;
1618
1619 memset(&user, 0, sizeof(struct gsm_mncc));
1620 user.callref = trans->callref;
1621 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1622 /* user-user */
1623 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1624 user.fields |= MNCC_F_USERUSER;
1625 gsm48_decode_useruser(&user.useruser,
1626 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1627 }
1628 /* more data */
1629 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1630 user.more = 1;
1631
1632 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1633}
1634
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001635static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1636 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1637 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001638{
1639 uint8_t data[sizeof(struct gsm_mncc)];
1640 struct gsm_mncc_rtp *rtp;
1641
1642 memset(&data, 0, sizeof(data));
1643 rtp = (struct gsm_mncc_rtp *) &data[0];
1644
1645 rtp->callref = callref;
1646 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001647 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001648 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1649 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001650 }
Harald Welte27989d42018-06-21 20:39:20 +02001651 rtp->payload_type = payload_type;
1652 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001653 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001654}
1655
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001656static void mncc_recv_rtp_err(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref, int cmd)
Harald Welte27989d42018-06-21 20:39:20 +02001657{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001658 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001659}
1660
1661static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1662{
1663 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001664
1665 /* Find callref */
1666 trans = trans_find_by_callref(net, callref);
1667 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001668 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001669 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001670 return -EIO;
1671 }
1672 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001673 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001674 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001675 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001676 return 0;
1677 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001678 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001679
Harald Welte27989d42018-06-21 20:39:20 +02001680 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001681 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001682}
1683
1684/* Trigger TCH_RTP_CREATE acknowledgement */
1685int gsm48_tch_rtp_create(struct gsm_trans *trans)
1686{
1687 /* This function is called as soon as the port, on which the
1688 * mgcp-gw expects the incoming RTP stream from the remote
1689 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001690 struct msc_a *msc_a = trans->msc_a;
1691 struct gsm_network *net = msc_a_net(msc_a);
1692 struct call_leg *cl = msc_a->cc.call_leg;
1693 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001694 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1695 uint32_t payload_type;
1696 int payload_msg_type;
1697 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001698
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001699 if (!rtp_cn) {
1700 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1701 return -EINVAL;
1702 }
1703
1704 if (!rtp_cn->codec_known) {
1705 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1706 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1707 return -EINVAL;
1708 }
1709
1710 /* Codec */
1711 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1712
1713 /* Payload Type number */
1714 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001715 if (mgcp_info && mgcp_info->ptmap_len)
1716 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1717 else
1718 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001719
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001720 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1721 if (!rtp_cn_local) {
1722 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1723 return -EINVAL;
1724 }
1725
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001726 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, payload_type, payload_msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001727}
1728
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001729static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001730{
1731 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001732 struct call_leg *cl;
1733 struct rtp_stream *rtps;
1734 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001735 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001736
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001737 /* FIXME: in *rtp we should get the codec information of the remote
1738 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1739 * meaningful value based on this information but unfortunately we
1740 * can't do that yet because the mncc API can not signal dynamic
1741 * payload types yet. This must be fixed first. Also there may be
1742 * additional members necessary in trans->conn->rtp because we
1743 * somehow need to deal with dynamic payload types that do not
1744 * comply to 3gpp's assumptions of payload type numbers on the A
1745 * interface. See also related tickets: OS#3399 and OS1683 */
1746
Harald Welte27989d42018-06-21 20:39:20 +02001747 /* Find callref */
1748 trans = trans_find_by_callref(net, rtp->callref);
1749 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001750 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001751 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001752 return -EIO;
1753 }
1754 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001755 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001756 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001757 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001758 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001759 }
1760
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001761 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(MNCC_RTP_CONNECT),
1762 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1763 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001764
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001765 cl = trans->msc_a->cc.call_leg;
1766 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1767
1768 if (!rtps) {
1769 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1770 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1771 return -EINVAL;
1772 }
1773
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001774 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1775 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1776 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1777 return -EINVAL;
1778 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001779 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1780 rtp_stream_commit(rtps);
1781 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001782}
1783
1784static struct downstate {
1785 uint32_t states;
1786 int type;
1787 int (*rout) (struct gsm_trans *trans, void *arg);
1788} downstatelist[] = {
1789 /* mobile originating call establishment */
1790 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1791 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1792 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1793 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1794 {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 */
1795 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1796 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1797 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1798 /* mobile terminating call establishment */
1799 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1800 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1801 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1802 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1803 /* signalling during call */
1804 {SBIT(GSM_CSTATE_ACTIVE),
1805 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1806 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1807 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1808 {ALL_STATES,
1809 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1810 {ALL_STATES,
1811 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1812 {ALL_STATES,
1813 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1814 {SBIT(GSM_CSTATE_ACTIVE),
1815 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1816 {SBIT(GSM_CSTATE_ACTIVE),
1817 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1818 {SBIT(GSM_CSTATE_ACTIVE),
1819 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1820 {SBIT(GSM_CSTATE_ACTIVE),
1821 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1822 {SBIT(GSM_CSTATE_ACTIVE),
1823 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1824 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1825 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1826 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1827 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1828 {SBIT(GSM_CSTATE_ACTIVE),
1829 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1830 /* clearing */
1831 {SBIT(GSM_CSTATE_INITIATED),
1832 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1833 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1834 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1835 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1836 MNCC_REL_REQ, gsm48_cc_tx_release},
1837};
1838
1839#define DOWNSLLEN \
1840 (sizeof(downstatelist) / sizeof(struct downstate))
1841
1842
Philipp Maiercd64af72019-08-01 09:46:40 +02001843static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001844{
1845 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001846 struct msc_a *msc_a = NULL;
1847 struct gsm_trans *trans = NULL;
1848 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001849
Harald Welte27989d42018-06-21 20:39:20 +02001850 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001851 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001852 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001853 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001854 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001855 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001856 return rc;
1857 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001858 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001859 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001860 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001861 case MNCC_RTP_FREE:
1862 /* unused right now */
1863 return -EIO;
1864
1865 case MNCC_FRAME_DROP:
1866 case MNCC_FRAME_RECV:
1867 case GSM_TCHF_FRAME:
1868 case GSM_TCHF_FRAME_EFR:
1869 case GSM_TCHH_FRAME:
1870 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001871 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001872 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001873 return -ENOTSUP;
1874 }
1875
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001876 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001877
1878 /* Find callref */
1879 trans = trans_find_by_callref(net, data->callref);
1880
1881 /* Callref unknown */
1882 if (!trans) {
1883 struct vlr_subscr *vsub;
1884
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001885 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001886 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001887 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001888 /* Invalid call reference */
1889 return mncc_release_ind(net, NULL, data->callref,
1890 GSM48_CAUSE_LOC_PRN_S_LU,
1891 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1892 }
1893 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001894 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001895 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001896 /* Invalid number */
1897 return mncc_release_ind(net, NULL, data->callref,
1898 GSM48_CAUSE_LOC_PRN_S_LU,
1899 GSM48_CC_CAUSE_INV_NR_FORMAT);
1900 }
1901 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001902 if (data->called.number[0]) {
1903 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1904 if (!vsub)
1905 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001906 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001907 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001908 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001909 if (!vsub)
1910 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001911 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001912 }
1913 if (!vsub)
1914 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001915 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001916 /* update the subscriber we deal with */
1917 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1918
Harald Welte27989d42018-06-21 20:39:20 +02001919 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001920 if (!vsub->lu_complete) {
1921 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001922 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001923 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001924 /* Temporarily out of order */
1925 return mncc_release_ind(net, NULL, data->callref,
1926 GSM48_CAUSE_LOC_PRN_S_LU,
1927 GSM48_CC_CAUSE_DEST_OOO);
1928 }
Keith Whyte991bb422019-08-08 15:43:40 +02001929
1930 /* Find valid conn */
1931 msc_a = msc_a_for_vsub(vsub, true);
1932
1933 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1934 if (!net->call_waiting && msc_a) {
1935 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1936 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1937 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1938 "rx '%s' for subscriber %s with trans state (%s)"
1939 " rejecting with USER_BUSY\n",
1940 get_mncc_name(msg->msg_type), data->called.number,
1941 gsm48_cc_state_name(existing_cc_trans->cc.state));
1942 return mncc_release_ind(net, NULL, data->callref,
1943 GSM48_CAUSE_LOC_PRN_S_LU,
1944 GSM48_CC_CAUSE_USER_BUSY);
1945 }
1946 }
1947
Harald Welte27989d42018-06-21 20:39:20 +02001948 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001949 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001950 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001951 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001952 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001953 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001954 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001955 mncc_release_ind(net, NULL, data->callref,
1956 GSM48_CAUSE_LOC_PRN_S_LU,
1957 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1958 return -ENOMEM;
1959 }
1960
Harald Welte27989d42018-06-21 20:39:20 +02001961 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001962 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001963 /* This condition will return before the common logging of the received MNCC message below, so
1964 * log it now. */
1965 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001966
Harald Welte27989d42018-06-21 20:39:20 +02001967 /* store setup information until paging succeeds */
1968 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1969
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001970 /* Request a channel. If Paging already started, paging_request_start() will append the new
1971 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001972 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1973 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001974 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001975 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001976 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001977 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001978 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001979 return 0;
1980 }
1981
1982 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001983 trans->msc_a = msc_a;
1984 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001985 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001986 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001987 } else {
1988 /* update the subscriber we deal with */
1989 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1990 }
1991
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001992 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001993
Philipp Maier9ca7b312018-10-10 17:00:49 +02001994 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02001995 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02001996
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997 if (trans->msc_a)
1998 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02001999
2000 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002001 if (!msc_a) {
2002 struct gsm_mncc rel = {
2003 .callref = data->callref,
2004 };
2005 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002006 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2007 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002008 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002009 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2010 else
2011 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2012 trans->callref = 0;
2013 trans_free(trans);
2014 return rc;
2015 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002016 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002017 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002018 }
2019
2020 /* Find function for current state and message */
2021 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002022 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002023 && ((1 << trans->cc.state) & downstatelist[i].states))
2024 break;
2025 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002026 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002027 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002028 return 0;
2029 }
2030
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002031 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002032
2033 return rc;
2034}
2035
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002036struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2037{
2038 uint32_t callref;
2039
2040 switch (msg->msg_type) {
2041 case MNCC_BRIDGE:
2042 callref = msg->bridge.callref[0];
2043 break;
2044 case MNCC_RTP_CREATE:
2045 case MNCC_RTP_CONNECT:
2046 callref = msg->rtp.callref;
2047 break;
2048
2049 case MNCC_RTP_FREE:
2050 case MNCC_FRAME_DROP:
2051 case MNCC_FRAME_RECV:
2052 case GSM_TCHF_FRAME:
2053 case GSM_TCHF_FRAME_EFR:
2054 case GSM_TCHH_FRAME:
2055 case GSM_TCH_FRAME_AMR:
2056 return NULL;
2057
2058 default:
2059 callref = msg->signal.callref;
2060 break;
2061 }
2062
2063 return mncc_call_find_by_callref(callref);
2064}
2065
2066/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002067int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002068{
2069 const union mncc_msg *msg = arg;
2070 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002071
2072 if (msg->msg_type == MNCC_SETUP_REQ) {
2073 /* Incoming call to forward for inter-MSC Handover? */
2074 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2075 if (mncc_call)
2076 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2077 "Incoming call matches pending inter-MSC Handover Number\n");
2078 }
2079 if (!mncc_call) {
2080 /* Find already active MNCC FSM for this callref.
2081 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2082 * MNCC<->GSM-CC call handling. */
2083 mncc_call = mncc_find_by_callref_from_msg(msg);
2084 }
2085 if (mncc_call) {
2086 mncc_call_rx(mncc_call, msg);
2087 return 0;
2088 }
2089
2090 /* None of the above? Then it must be a normal GSM CC call related message. */
2091 return mncc_tx_to_gsm_cc(net, msg);
2092}
Harald Welte27989d42018-06-21 20:39:20 +02002093
2094static struct datastate {
2095 uint32_t states;
2096 int type;
2097 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2098} datastatelist[] = {
2099 /* mobile originating call establishment */
2100 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2101 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2102 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2103 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2104 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2105 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2106 /* mobile terminating call establishment */
2107 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2108 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2109 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2110 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2111 {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 */
2112 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2113 /* signalling during call */
2114 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2115 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2116 {SBIT(GSM_CSTATE_ACTIVE),
2117 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2118 {ALL_STATES,
2119 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2120 {ALL_STATES,
2121 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2122 {ALL_STATES,
2123 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2124 {SBIT(GSM_CSTATE_ACTIVE),
2125 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2126 {SBIT(GSM_CSTATE_ACTIVE),
2127 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2128 {SBIT(GSM_CSTATE_ACTIVE),
2129 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2130 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2131 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2132 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2133 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2134 {SBIT(GSM_CSTATE_ACTIVE),
2135 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2136 /* clearing */
2137 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2138 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2139 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2140 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2141 {ALL_STATES, /* 5.4.3.4 */
2142 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2143};
2144
2145#define DATASLLEN \
2146 (sizeof(datastatelist) / sizeof(struct datastate))
2147
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002148int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002149{
2150 struct gsm48_hdr *gh = msgb_l3(msg);
2151 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2152 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2153 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002154 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2155 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002156 int i, rc = 0;
2157
2158 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002159 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002160 return -EINVAL;
2161 }
2162
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002163 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002164 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002165 return -EINVAL;
2166 }
2167
2168 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002169 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002170
Harald Welte27989d42018-06-21 20:39:20 +02002171 /* Create transaction */
2172 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002173 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002174 trans = trans_alloc(net, vsub,
2175 TRANS_CC,
2176 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002177 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002178 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002179 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002180 GSM48_PDISC_CC | (transaction_id << 4),
2181 GSM48_MT_CC_RELEASE_COMPL);
2182 return -ENOMEM;
2183 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002184 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2185 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2186 trans_free(trans);
2187 return -EINVAL;
2188 }
2189
Harald Welte27989d42018-06-21 20:39:20 +02002190 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002191 msc_a_get(msc_a, MSC_A_USE_CC);
2192 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002193 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002194
2195 /* An earlier CM Service Request for this CC message now has concluded */
2196 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2197 LOG_MSC_A(msc_a, LOGL_ERROR,
2198 "Creating new CC transaction without prior CM Service Request\n");
2199 else
2200 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002201 }
2202
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002203 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2204 gsm48_cc_state_name(trans->cc.state));
2205
Harald Welte27989d42018-06-21 20:39:20 +02002206 /* find function for current state and message */
2207 for (i = 0; i < DATASLLEN; i++)
2208 if ((msg_type == datastatelist[i].type)
2209 && ((1 << trans->cc.state) & datastatelist[i].states))
2210 break;
2211 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002212 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002213
2214 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2215 * transaction right away. */
2216 if (trans->cc.state == GSM_CSTATE_NULL) {
2217 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2218 " -- disarding new CC transaction right away\n");
2219 trans_free(trans);
2220 }
Harald Welte27989d42018-06-21 20:39:20 +02002221 return 0;
2222 }
2223
2224 assert(trans->vsub);
2225
2226 rc = datastatelist[i].rout(trans, msg);
2227
Harald Welte27989d42018-06-21 20:39:20 +02002228 return rc;
2229}