blob: 2d3139a299a92b2e89f17cc982db9b5386bdc2b4 [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>
Neels Hofmeyr9a539f32022-01-13 21:39:11 +010058#include <osmocom/msc/codec_sdp_cc_t9n.h>
Harald Welte27989d42018-06-21 20:39:20 +020059
60#include <osmocom/gsm/gsm48.h>
61#include <osmocom/gsm/gsm0480.h>
62#include <osmocom/gsm/gsm_utils.h>
63#include <osmocom/gsm/protocol/gsm_04_08.h>
64#include <osmocom/core/msgb.h>
65#include <osmocom/core/talloc.h>
66#include <osmocom/core/utils.h>
67#include <osmocom/core/byteswap.h>
68#include <osmocom/gsm/tlv.h>
69#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020070
71#include <assert.h>
72
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010073static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
75static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
76
77static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
78{
79 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
80 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
81 OMSC_LINKID_CB(msg) = trans->dlci;
82
83 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
84}
85
86uint32_t msc_cc_next_outgoing_callref() {
87 static uint32_t last_callref = 0x80000000;
88 last_callref++;
89 if (last_callref < 0x80000001)
90 last_callref = 0x80000001;
91 return last_callref;
92}
Harald Welte27989d42018-06-21 20:39:20 +020093
Philipp Maier9ca7b312018-10-10 17:00:49 +020094static void gsm48_cc_guard_timeout(void *arg)
95{
96 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010097 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020098 trans_free(trans);
99 return;
100}
101
102static void gsm48_stop_guard_timer(struct gsm_trans *trans)
103{
104 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100105 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200106 osmo_timer_del(&trans->cc.timer_guard);
107 }
108}
109
110static void gsm48_start_guard_timer(struct gsm_trans *trans)
111{
112 /* NOTE: The purpose of this timer is to prevent the cc state machine
113 * from hanging in cases where mncc, gsm48 or both become unresponsive
114 * for some reason. The timer is started initially with the setup from
115 * the gsm48 side and then re-started with every incoming mncc message.
116 * Once the mncc state reaches its active state the timer is stopped.
117 * So if the cc state machine does not show any activity for an
118 * extended amount of time during call setup or teardown the guard
119 * timer will time out and hard-clear the connection. */
120 if (osmo_timer_pending(&trans->cc.timer_guard))
121 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100122 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200123 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
124 osmo_timer_schedule(&trans->cc.timer_guard,
125 trans->net->mncc_guard_timeout, 0);
126}
Harald Welte27989d42018-06-21 20:39:20 +0200127
128/* Call Control */
129
130void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
131{
132 net->mncc_recv(net, msg);
133}
134
135int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
136{
137 struct gsm48_hdr *gh;
138 struct msgb *ss_notify;
139
140 ss_notify = gsm0480_create_notifySS(message);
141 if (!ss_notify)
142 return -1;
143
144 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
145 uint8_t *data = msgb_push(ss_notify, 1);
146 data[0] = ss_notify->len - 1;
147 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
148 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100149 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200150}
151
152/* FIXME: this count_statistics is a state machine behaviour. we should convert
153 * the complete call control into a state machine. Afterwards we can move this
154 * code into state transitions.
155 */
156static void count_statistics(struct gsm_trans *trans, int new_state)
157{
158 int old_state = trans->cc.state;
159 struct rate_ctr_group *msc = trans->net->msc_ctrs;
160
161 if (old_state == new_state)
162 return;
163
164 /* state incoming */
165 switch (new_state) {
166 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200167 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
168 1);
169 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200170 break;
171 }
172
173 /* state outgoing */
174 switch (old_state) {
175 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200176 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
177 1);
Harald Welte27989d42018-06-21 20:39:20 +0200178 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
179 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200180 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200181 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200182 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200183 break;
184 }
185}
186
Harald Welte27989d42018-06-21 20:39:20 +0200187static void new_cc_state(struct gsm_trans *trans, int state)
188{
189 if (state > 31 || state < 0)
190 return;
191
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100192 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
193 gsm48_cc_state_name(trans->cc.state),
194 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200195
196 count_statistics(trans, state);
197 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200198
199 /* Stop the guard timer when a call reaches the active state */
200 if (state == GSM_CSTATE_ACTIVE)
201 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200202}
203
204static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
205{
206 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
207 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
208 uint8_t *cause, *call_state;
209
210 gh->msg_type = GSM48_MT_CC_STATUS;
211
212 cause = msgb_put(msg, 3);
213 cause[0] = 2;
214 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
215 cause[2] = 0x80 | 30; /* response to status inquiry */
216
217 call_state = msgb_put(msg, 1);
218 call_state[0] = 0xc0 | 0x00;
219
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100220 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200221}
222
223static void gsm48_stop_cc_timer(struct gsm_trans *trans)
224{
225 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100226 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200227 osmo_timer_del(&trans->cc.timer);
228 trans->cc.Tcurrent = 0;
229 }
230}
231
Neels Hofmeyrad19b452022-08-07 02:43:15 +0200232#define mncc_recvmsg(net, trans, msg_type, mncc) \
233 _mncc_recvmsg(__FILE__, __LINE__, net, trans, msg_type, mncc)
234static int _mncc_recvmsg(const char *file, int line, struct gsm_network *net, struct gsm_trans *trans,
Harald Welte27989d42018-06-21 20:39:20 +0200235 int msg_type, struct gsm_mncc *mncc)
236{
237 struct msgb *msg;
238 unsigned char *data;
239
Neels Hofmeyre3b99c92022-01-14 03:04:56 +0100240 switch (msg_type) {
241 case MNCC_DISC_IND:
242 case MNCC_REL_IND:
243 if (trans) {
244 if (trans->cc.mncc_release_sent) {
245 LOG_TRANS_CAT_SRC(trans, DMNCC, file, line, LOGL_DEBUG,
246 "Already released, skipping tx %s\n", get_mncc_name(msg_type));
247 return 0;
248 }
249 trans->cc.mncc_release_sent = true;
250 }
251 break;
252 default:
253 break;
254 }
255
Neels Hofmeyrad19b452022-08-07 02:43:15 +0200256 LOG_TRANS_CAT_SRC(trans, DMNCC, file, line, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200257
258 mncc->msg_type = msg_type;
259
260 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
261 if (!msg)
262 return -ENOMEM;
263
264 data = msgb_put(msg, sizeof(struct gsm_mncc));
265 memcpy(data, mncc, sizeof(struct gsm_mncc));
266
267 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200268 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
269 if (trans)
270 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200271
272 return 0;
273}
274
275int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
276 uint32_t callref, int location, int value)
277{
278 struct gsm_mncc rel;
279
280 memset(&rel, 0, sizeof(rel));
281 rel.callref = callref;
282 mncc_set_cause(&rel, location, value);
283 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
284 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
285 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
286}
287
288/* Call Control Specific transaction release.
289 * gets called by trans_free, DO NOT CALL YOURSELF! */
290void _gsm48_cc_trans_free(struct gsm_trans *trans)
291{
292 gsm48_stop_cc_timer(trans);
293
Harald Welte27989d42018-06-21 20:39:20 +0200294 /* send release to L4, if callref still exists */
295 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100296 /* Send MNCC REL.ind (cause='Resource unavailable') */
297 if (trans->cc.mncc_initiated) {
298 mncc_release_ind(trans->net, trans, trans->callref,
299 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100300 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
301 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100302 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
303 }
304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100305 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
306 * CC Release to the MS if it gets freed here. Hack it to do so. */
307 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
308 struct gsm_mncc rel = {};
309 rel.callref = trans->callref;
310 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
311 gsm48_cc_tx_release(trans, &rel);
312 }
Harald Welte27989d42018-06-21 20:39:20 +0200313 /* This is a final freeing of the transaction. The MNCC release may have triggered the
314 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
315 gsm48_stop_cc_timer(trans);
316 }
317 if (trans->cc.state != GSM_CSTATE_NULL)
318 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200319
320 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100321
322 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
323 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200324}
325
Harald Welte27989d42018-06-21 20:39:20 +0200326/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100327static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200328{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100329 if (trans->msc_a) {
330 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
331 "Handle paging error: transaction already associated with subscriber,"
332 " apparently it was already handled. Skip.\n");
333 return;
Harald Welte27989d42018-06-21 20:39:20 +0200334 }
335
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100336 if (msc_a) {
337 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
338 /* Assign conn */
339 msc_a_get(msc_a, MSC_A_USE_CC);
340 trans->msc_a = msc_a;
341 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200342
Keith Whytea1a70be2021-05-16 02:59:52 +0200343
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100344 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
345 /* send SETUP request to called party */
346 gsm48_cc_tx_setup(trans, &trans->cc.msg);
347 } else {
348 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
349 /* Temporarily out of order */
350 mncc_release_ind(trans->net, trans,
351 trans->callref,
352 GSM48_CAUSE_LOC_PRN_S_LU,
353 GSM48_CC_CAUSE_DEST_OOO);
354 trans->callref = 0;
355 trans->paging_request = NULL;
356 trans_free(trans);
357 }
Harald Welte27989d42018-06-21 20:39:20 +0200358}
359
360/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100361static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200362{
363 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
364 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100365 struct call_leg *cl1;
366 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200367
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100368 if (!trans1 || !trans2) {
369 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200370 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100371 }
Harald Welte27989d42018-06-21 20:39:20 +0200372
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100373 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100374 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
375 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 +0200376 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100377 }
378
379 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
380 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200381
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100382 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
383 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
384 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
385 * If we can't, then we must give up. */
386 cl1 = trans1->msc_a->cc.call_leg;
387 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200388
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100389 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200390}
391
392static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
393{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100394 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200395 return gsm48_cc_tx_status(trans, msg);
396}
397
Harald Welte27989d42018-06-21 20:39:20 +0200398static void gsm48_cc_timeout(void *arg)
399{
400 struct gsm_trans *trans = arg;
401 int disconnect = 0, release = 0;
402 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
403 int mo_location = GSM48_CAUSE_LOC_USER;
404 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
405 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
406 struct gsm_mncc mo_rel, l4_rel;
407
Neels Hofmeyrac7d9a62022-08-06 14:16:55 +0200408 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
409
Harald Welte27989d42018-06-21 20:39:20 +0200410 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
411 mo_rel.callref = trans->callref;
412 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
413 l4_rel.callref = trans->callref;
414
415 switch(trans->cc.Tcurrent) {
416 case 0x303:
417 release = 1;
418 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
419 break;
420 case 0x310:
421 disconnect = 1;
422 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
423 break;
424 case 0x313:
425 disconnect = 1;
426 /* unknown, did not find it in the specs */
427 break;
428 case 0x301:
429 disconnect = 1;
430 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
431 break;
432 case 0x308:
433 if (!trans->cc.T308_second) {
434 /* restart T308 a second time */
435 gsm48_cc_tx_release(trans, &trans->cc.msg);
436 trans->cc.T308_second = 1;
437 break; /* stay in release state */
438 }
439 trans_free(trans);
440 return;
441 case 0x306:
442 release = 1;
443 mo_cause = trans->cc.msg.cause.value;
444 mo_location = trans->cc.msg.cause.location;
445 break;
446 case 0x323:
447 disconnect = 1;
448 break;
449 default:
450 release = 1;
451 }
452
453 if (release && trans->callref) {
454 /* process release towards layer 4 */
455 mncc_release_ind(trans->net, trans, trans->callref,
456 l4_location, l4_cause);
457 trans->callref = 0;
458 }
459
460 if (disconnect && trans->callref) {
461 /* process disconnect towards layer 4 */
462 mncc_set_cause(&l4_rel, l4_location, l4_cause);
463 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
464 }
465
466 /* process disconnect towards mobile station */
467 if (disconnect || release) {
468 mncc_set_cause(&mo_rel, mo_location, mo_cause);
469 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
470 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
471 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
472 mo_rel.cause.diag_len = 3;
473
474 if (disconnect)
475 gsm48_cc_tx_disconnect(trans, &mo_rel);
476 if (release)
477 gsm48_cc_tx_release(trans, &mo_rel);
478 }
479
480}
481
482/* disconnect both calls from the bridge */
483static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100484 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200485{
486 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
487 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
488 struct gsm_mncc mx_rel;
489 if (!trans0 || !trans1)
490 return;
491
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100492 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
493 trans0->callref, trans1->callref, strerror(err));
494 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200495 trans0->callref, trans1->callref, strerror(err));
496
497 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
498 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
499 GSM48_CC_CAUSE_CHAN_UNACCEPT);
500
501 mx_rel.callref = trans0->callref;
502 gsm48_cc_tx_disconnect(trans0, &mx_rel);
503
504 mx_rel.callref = trans1->callref;
505 gsm48_cc_tx_disconnect(trans1, &mx_rel);
506}
507
508static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
509 int sec, int micro)
510{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100511 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200512 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
513 osmo_timer_schedule(&trans->cc.timer, sec, micro);
514 trans->cc.Tcurrent = current;
515}
516
517static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
518{
519 struct gsm48_hdr *gh = msgb_l3(msg);
520 uint8_t msg_type = gsm48_hdr_msg_type(gh);
521 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
522 struct tlv_parsed tp;
523 struct gsm_mncc setup;
524
Philipp Maier9ca7b312018-10-10 17:00:49 +0200525 gsm48_start_guard_timer(trans);
526
Harald Welte27989d42018-06-21 20:39:20 +0200527 memset(&setup, 0, sizeof(struct gsm_mncc));
528 setup.callref = trans->callref;
529
Keith Whytea1a70be2021-05-16 02:59:52 +0200530 /* New Global Call Reference */
531 if (!trans->cc.lcls)
532 trans->cc.lcls = trans_lcls_compose(trans, true);
533
534 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300535 if (trans->cc.lcls) {
536 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
537 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
538 int rc;
539
540 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
541 memcpy(&setup.gcr[0], gcr_msg->data, rc);
542 setup.fields |= MNCC_F_GCR;
543 } else
544 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
545 msgb_free(gcr_msg);
546 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200547
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100548 OSMO_ASSERT(trans->msc_a);
549
Harald Welte27989d42018-06-21 20:39:20 +0200550 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
551 /* emergency setup is identified by msg_type */
552 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
553 setup.fields |= MNCC_F_EMERGENCY;
554 setup.emergency = 1;
555 /* use destination number as configured by user (if any) */
556 if (trans->net->emergency.route_to_msisdn) {
557 setup.fields |= MNCC_F_CALLED;
558 setup.called.type = 0; /* unknown */
559 setup.called.plan = 0; /* unknown */
560 OSMO_STRLCPY_ARRAY(setup.called.number,
561 trans->net->emergency.route_to_msisdn);
562 }
563 }
564
565 /* use subscriber as calling party number */
566 setup.fields |= MNCC_F_CALLING;
567 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
568 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
569
570 /* bearer capability */
571 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
572 setup.fields |= MNCC_F_BEARER_CAP;
573 gsm48_decode_bearer_cap(&setup.bearer_cap,
574 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
575
576 /* Create a copy of the bearer capability
577 * in the transaction struct, so we can use
578 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100579 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200580 sizeof(trans->bearer_cap));
581 }
582 /* facility */
583 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
584 setup.fields |= MNCC_F_FACILITY;
585 gsm48_decode_facility(&setup.facility,
586 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
587 }
588 /* called party bcd number */
589 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
590 setup.fields |= MNCC_F_CALLED;
591 gsm48_decode_called(&setup.called,
592 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
593 }
594 /* user-user */
595 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
596 setup.fields |= MNCC_F_USERUSER;
597 gsm48_decode_useruser(&setup.useruser,
598 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
599 }
600 /* ss-version */
601 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
602 setup.fields |= MNCC_F_SSVERSION;
603 gsm48_decode_ssversion(&setup.ssversion,
604 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
605 }
606 /* CLIR suppression */
607 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
608 setup.clir.sup = 1;
609 /* CLIR invocation */
610 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
611 setup.clir.inv = 1;
612 /* cc cap */
613 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
614 setup.fields |= MNCC_F_CCCAP;
615 gsm48_decode_cccap(&setup.cccap,
616 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
617 }
618
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100619 /* MO call leg starting, gather all codec information so far known: */
620 codec_filter_init(&trans->cc.codecs);
621 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
622 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
623 if (setup.fields & MNCC_F_BEARER_CAP)
624 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
625 codec_filter_run(&trans->cc.codecs);
626
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100627 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
628 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100629 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200630
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200631 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200632
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100633 new_cc_state(trans, GSM_CSTATE_INITIATED);
634
635 /* To complete the MNCC_SETUP_IND, we need to provide an RTP address and port. First instruct the MGW to create
636 * a CN-side RTP conn, and continue with MNCC_SETUP_IND once that is done. Leave trans.cc in GSM_CSTATE_NULL and
637 * note down the msg_type to indicate that we indeed composed an MNCC_SETUP_IND for later. */
638 setup.msg_type = MNCC_SETUP_IND;
639 trans->cc.msg = setup;
640 return msc_a_try_call_assignment(trans);
641 /* continue in gsm48_cc_rx_setup_cn_local_rtp_port_known() */
642}
643
644/* Callback for MNCC_SETUP_IND waiting for the core network RTP port to be established by the MGW (via msc_a) */
645void gsm48_cc_rx_setup_cn_local_rtp_port_known(struct gsm_trans *trans)
646{
647 struct msc_a *msc_a = trans->msc_a;
648 struct gsm_mncc setup = trans->cc.msg;
649 struct osmo_sockaddr_str *rtp_cn_local;
650 struct sdp_msg *sdp;
651 int rc;
652
653 if (trans->cc.state != GSM_CSTATE_INITIATED
654 || setup.msg_type != MNCC_SETUP_IND) {
655 LOG_TRANS(trans, LOGL_ERROR,
656 "Unexpected CC state. Expected GSM_CSTATE_NULL and a buffered MNCC_SETUP_IND message,"
657 " found CC state %d and msg_type %s\n",
658 trans->cc.state, get_mncc_name(setup.msg_type));
659 trans->callref = 0;
660 trans_free(trans);
661 return;
662 }
663
664 if (!msc_a) {
665 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
666 trans->callref = 0;
667 trans_free(trans);
668 return;
669 }
670
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100671
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100672 /* Insert the CN side RTP port now available into SDP and compose SDP string */
673 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
674 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
675 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100676 trans_free(trans);
677 return;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100678 }
679
680 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
681 codec_filter_run(&trans->cc.codecs);
682 sdp = trans->cc.codecs.result.audio_codecs.count ? &trans->cc.codecs.result : NULL;
683 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
684 if (rc >= sizeof(setup.sdp)) {
685 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
686 trans_free(trans);
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100687 return;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100688 }
689
Harald Welte27989d42018-06-21 20:39:20 +0200690 /* indicate setup to MNCC */
691 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte27989d42018-06-21 20:39:20 +0200692}
693
694static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
695{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100696 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200697 struct gsm48_hdr *gh;
698 struct gsm_mncc *setup = arg;
699 int rc, trans_id;
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100700 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200701
702 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
703
704 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700705 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100706 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200707 "This is not allowed!\n");
708 /* Temporarily out of order */
709 rc = mncc_release_ind(trans->net, trans, trans->callref,
710 GSM48_CAUSE_LOC_PRN_S_LU,
711 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
712 trans->callref = 0;
713 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200714 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200715 return rc;
716 }
717
718 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100719 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200720 if (trans_id < 0) {
721 /* no free transaction ID */
722 rc = mncc_release_ind(trans->net, trans, trans->callref,
723 GSM48_CAUSE_LOC_PRN_S_LU,
724 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
725 trans->callref = 0;
726 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200727 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200728 return rc;
729 }
730 trans->transaction_id = trans_id;
731
732 gh->msg_type = GSM48_MT_CC_SETUP;
733
Neels Hofmeyra4195db2022-01-13 21:40:58 +0100734 /* MT call leg is starting. Gather all codecs information so far known.
735 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
736 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
737 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
738 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
739 * Capabilities. */
740 codec_filter_init(&trans->cc.codecs);
741 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
742 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
743 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
744 if (setup->sdp[0]) {
745 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
746 if (rc)
747 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
748 }
749 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
750 * MNCC, if any. */
751 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
752 trans->cc.codecs.remote = (struct sdp_msg){};
753 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
754 &setup->bearer_cap);
755 }
756 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
757 if (!trans->cc.codecs.remote.audio_codecs.count)
758 LOG_TRANS(trans, LOGL_ERROR,
759 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
760
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100761 /* Compose outgoing Bearer Capabilities: translate SDP to bearer capability Speech Version entries.
762 * Send only codecs that remain according to the codec filter. */
Harald Welte27989d42018-06-21 20:39:20 +0200763 /* Create a copy of the bearer capability in the transaction struct, so we
764 * can use this information later */
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100765 codec_filter_run(&trans->cc.codecs);
766 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
767 bearer_cap = (struct gsm_mncc_bearer_cap){
768 .speech_ver = { -1 },
769 };
770 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.codecs.result.audio_codecs);
771 rc = bearer_cap_set_radio(&bearer_cap);
772 if (rc) {
773 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
774 trans_free(trans);
775 msgb_free(msg);
776 return rc;
Harald Welte27989d42018-06-21 20:39:20 +0200777 }
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100778 /* (As earlier code did, keep a copy in trans->bearer_cap) */
779 trans->bearer_cap = bearer_cap;
780 /* If no resulting codecs remain, error out. If the MGW were able to transcode, we would just use unidentical
781 * codecs on each conn of the MGW endpoint. */
782 if (bearer_cap.speech_ver[0] == -1) {
783 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
784 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
785
786 /* incompatible codecs */
787 rc = mncc_release_ind(trans->net, trans, trans->callref,
788 GSM48_CAUSE_LOC_PRN_S_LU,
789 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
790 trans_free(trans);
791 msgb_free(msg);
792 return rc;
793 }
794 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
795
Harald Welte27989d42018-06-21 20:39:20 +0200796 /* facility */
797 if (setup->fields & MNCC_F_FACILITY)
798 gsm48_encode_facility(msg, 0, &setup->facility);
799 /* progress */
800 if (setup->fields & MNCC_F_PROGRESS)
801 gsm48_encode_progress(msg, 0, &setup->progress);
802 /* calling party BCD number */
803 if (setup->fields & MNCC_F_CALLING)
804 gsm48_encode_calling(msg, &setup->calling);
805 /* called party BCD number */
806 if (setup->fields & MNCC_F_CALLED)
807 gsm48_encode_called(msg, &setup->called);
808 /* user-user */
809 if (setup->fields & MNCC_F_USERUSER)
810 gsm48_encode_useruser(msg, 0, &setup->useruser);
811 /* redirecting party BCD number */
812 if (setup->fields & MNCC_F_REDIRECTING)
813 gsm48_encode_redirecting(msg, &setup->redirecting);
814 /* signal */
815 if (setup->fields & MNCC_F_SIGNAL)
816 gsm48_encode_signal(msg, setup->signal);
817
818 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
819
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200820 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200821
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100822 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
823
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100824 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200825}
826
827static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
828{
829 struct gsm48_hdr *gh = msgb_l3(msg);
830 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
831 struct tlv_parsed tp;
832 struct gsm_mncc call_conf;
833 int rc;
834
835 gsm48_stop_cc_timer(trans);
836 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
837
838 memset(&call_conf, 0, sizeof(struct gsm_mncc));
839 call_conf.callref = trans->callref;
840
841 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
842#if 0
843 /* repeat */
844 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
845 call_conf.repeat = 1;
846 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
847 call_conf.repeat = 2;
848#endif
849 /* bearer capability */
850 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
851 call_conf.fields |= MNCC_F_BEARER_CAP;
852 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
853 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
854
855 /* Create a copy of the bearer capability
856 * in the transaction struct, so we can use
857 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100858 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200859 sizeof(trans->bearer_cap));
Neels Hofmeyrdb61f732022-01-13 19:59:02 +0100860
861 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
862 * Store in codecs filter. */
863 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200864 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100865
Harald Welte27989d42018-06-21 20:39:20 +0200866 /* cause */
867 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
868 call_conf.fields |= MNCC_F_CAUSE;
869 gsm48_decode_cause(&call_conf.cause,
870 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
871 }
872 /* cc cap */
873 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
874 call_conf.fields |= MNCC_F_CCCAP;
875 gsm48_decode_cccap(&call_conf.cccap,
876 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
877 }
878
879 /* IMSI of called subscriber */
880 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
881
Harald Welte27989d42018-06-21 20:39:20 +0200882 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100883 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200884
885 /* don't continue, if there were problems with
886 * the call assignment. */
887 if (rc)
888 return rc;
889
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100890 /* Directly ack with MNCC_CALL_CONF_IND, not yet containing SDP or RTP IP:port information. */
891 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
892 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND, &call_conf);
893}
894
895static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
896 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
897 uint32_t payload_msg_type, const struct sdp_msg *sdp);
898
Neels Hofmeyr201fae42022-08-09 02:14:42 +0200899static int gsm48_cc_mt_rtp_port_and_codec_known(struct gsm_trans *trans)
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100900{
901 struct msc_a *msc_a = trans->msc_a;
902 struct osmo_sockaddr_str *rtp_cn_local;
903 struct gsm_mncc_rtp;
904
905 if (!msc_a) {
906 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
907 trans->callref = 0;
908 trans_free(trans);
909 return -EINVAL;
910 }
911
912 /* Insert the CN side RTP port now available into SDP */
913 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
914 if (!rtp_cn_local) {
915 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_RTP_CREATE: no RTP set up for the CN side\n");
916 trans_free(trans);
917 return -EINVAL;
918 }
919 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
920
921 codec_filter_run(&trans->cc.codecs);
922 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
923
924 if (!sdp_audio_codec_is_set(&trans->cc.codecs.assignment))
925 return 0;
926
927 return mncc_recv_rtp(msc_a_net(msc_a), trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, 0, 0,
928 &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +0200929}
930
931static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
932{
933 struct gsm_mncc *proceeding = arg;
934 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
935 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
936 int rc;
937
938 gh->msg_type = GSM48_MT_CC_CALL_PROC;
939
940 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
941
942 /* bearer capability */
943 if (proceeding->fields & MNCC_F_BEARER_CAP) {
944 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
945 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
946 }
947 /* facility */
948 if (proceeding->fields & MNCC_F_FACILITY)
949 gsm48_encode_facility(msg, 0, &proceeding->facility);
950 /* progress */
951 if (proceeding->fields & MNCC_F_PROGRESS)
952 gsm48_encode_progress(msg, 0, &proceeding->progress);
953
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100954 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200955 if (rc)
956 return rc;
957
958 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100959 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200960}
961
962static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
963{
964 struct gsm48_hdr *gh = msgb_l3(msg);
965 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
966 struct tlv_parsed tp;
967 struct gsm_mncc alerting;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100968 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200969
970 gsm48_stop_cc_timer(trans);
971 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
972
973 memset(&alerting, 0, sizeof(struct gsm_mncc));
974 alerting.callref = trans->callref;
975 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
976 /* facility */
977 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
978 alerting.fields |= MNCC_F_FACILITY;
979 gsm48_decode_facility(&alerting.facility,
980 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
981 }
982
983 /* progress */
984 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
985 alerting.fields |= MNCC_F_PROGRESS;
986 gsm48_decode_progress(&alerting.progress,
987 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
988 }
989 /* ss-version */
990 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
991 alerting.fields |= MNCC_F_SSVERSION;
992 gsm48_decode_ssversion(&alerting.ssversion,
993 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
994 }
995
996 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
997
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100998 codec_filter_run(&trans->cc.codecs);
999 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1000 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
1001 if (rc >= sizeof(alerting.sdp)) {
1002 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
1003 rc, sizeof(alerting.sdp));
1004 trans_free(trans);
1005 return -EINVAL;
1006 }
1007
Harald Welte27989d42018-06-21 20:39:20 +02001008 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
1009 &alerting);
1010}
1011
1012static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1013{
1014 struct gsm_mncc *alerting = arg;
1015 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1016 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1017
1018 gh->msg_type = GSM48_MT_CC_ALERTING;
1019
1020 /* facility */
1021 if (alerting->fields & MNCC_F_FACILITY)
1022 gsm48_encode_facility(msg, 0, &alerting->facility);
1023 /* progress */
1024 if (alerting->fields & MNCC_F_PROGRESS)
1025 gsm48_encode_progress(msg, 0, &alerting->progress);
1026 /* user-user */
1027 if (alerting->fields & MNCC_F_USERUSER)
1028 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1029
1030 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1031
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001032 if (alerting->sdp[0]) {
1033 struct call_leg *cl = trans->msc_a->cc.call_leg;
1034 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1035 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
1036 codec_filter_run(&trans->cc.codecs);
1037 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1038 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
1039 if (rtp_cn) {
1040 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1041 rtp_stream_commit(rtp_cn);
1042 }
1043 }
1044
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001045 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001046}
1047
1048static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1049{
1050 struct gsm_mncc *progress = arg;
1051 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1052 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1053
1054 gh->msg_type = GSM48_MT_CC_PROGRESS;
1055
1056 /* progress */
1057 gsm48_encode_progress(msg, 1, &progress->progress);
1058 /* user-user */
1059 if (progress->fields & MNCC_F_USERUSER)
1060 gsm48_encode_useruser(msg, 0, &progress->useruser);
1061
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001062 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001063}
1064
1065static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1066{
1067 struct gsm_mncc *connect = arg;
1068 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1069 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1070
1071 gh->msg_type = GSM48_MT_CC_CONNECT;
1072
1073 gsm48_stop_cc_timer(trans);
1074 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1075
1076 /* facility */
1077 if (connect->fields & MNCC_F_FACILITY)
1078 gsm48_encode_facility(msg, 0, &connect->facility);
1079 /* progress */
1080 if (connect->fields & MNCC_F_PROGRESS)
1081 gsm48_encode_progress(msg, 0, &connect->progress);
1082 /* connected number */
1083 if (connect->fields & MNCC_F_CONNECTED)
1084 gsm48_encode_connected(msg, &connect->connected);
1085 /* user-user */
1086 if (connect->fields & MNCC_F_USERUSER)
1087 gsm48_encode_useruser(msg, 0, &connect->useruser);
1088
1089 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1090
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001091 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
1092 if (connect->sdp[0]) {
1093 struct call_leg *cl = trans->msc_a->cc.call_leg;
1094 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1095 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, connect->sdp);
1096 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1097 get_mncc_name(connect->msg_type),
1098 codec_filter_to_str(&trans->cc.codecs));
1099 if (rtp_cn) {
1100 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1101 rtp_stream_commit(rtp_cn);
1102 }
1103 }
1104
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001105 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001106}
1107
1108static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1109{
1110 struct gsm48_hdr *gh = msgb_l3(msg);
1111 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1112 struct tlv_parsed tp;
1113 struct gsm_mncc connect;
1114
1115 gsm48_stop_cc_timer(trans);
1116
1117 memset(&connect, 0, sizeof(struct gsm_mncc));
1118 connect.callref = trans->callref;
1119 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1120 /* use subscriber as connected party number */
1121 connect.fields |= MNCC_F_CONNECTED;
1122 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1123 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1124
1125 /* facility */
1126 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1127 connect.fields |= MNCC_F_FACILITY;
1128 gsm48_decode_facility(&connect.facility,
1129 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1130 }
1131 /* user-user */
1132 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1133 connect.fields |= MNCC_F_USERUSER;
1134 gsm48_decode_useruser(&connect.useruser,
1135 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1136 }
1137 /* ss-version */
1138 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1139 connect.fields |= MNCC_F_SSVERSION;
1140 gsm48_decode_ssversion(&connect.ssversion,
1141 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1142 }
1143
1144 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001145 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001146
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001147 codec_filter_run(&trans->cc.codecs);
1148 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001149 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1150}
1151
1152
1153static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1154{
1155 struct gsm_mncc connect_ack;
1156
1157 gsm48_stop_cc_timer(trans);
1158
1159 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001160 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 +02001161
1162 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1163 connect_ack.callref = trans->callref;
1164
1165 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1166 &connect_ack);
1167}
1168
1169static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1170{
1171 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1172 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1173
1174 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1175
1176 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1177
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001178 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001179}
1180
1181static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1182{
1183 struct gsm48_hdr *gh = msgb_l3(msg);
1184 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1185 struct tlv_parsed tp;
1186 struct gsm_mncc disc;
1187
1188 gsm48_stop_cc_timer(trans);
1189
1190 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1191
1192 memset(&disc, 0, sizeof(struct gsm_mncc));
1193 disc.callref = trans->callref;
1194 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1195 /* cause */
1196 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1197 disc.fields |= MNCC_F_CAUSE;
1198 gsm48_decode_cause(&disc.cause,
1199 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1200 }
1201 /* facility */
1202 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1203 disc.fields |= MNCC_F_FACILITY;
1204 gsm48_decode_facility(&disc.facility,
1205 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1206 }
1207 /* user-user */
1208 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1209 disc.fields |= MNCC_F_USERUSER;
1210 gsm48_decode_useruser(&disc.useruser,
1211 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1212 }
1213 /* ss-version */
1214 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1215 disc.fields |= MNCC_F_SSVERSION;
1216 gsm48_decode_ssversion(&disc.ssversion,
1217 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1218 }
1219
1220 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001221}
1222
1223static struct gsm_mncc_cause default_cause = {
1224 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1225 .coding = 0,
1226 .rec = 0,
1227 .rec_val = 0,
1228 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1229 .diag_len = 0,
1230 .diag = { 0 },
1231};
1232
1233static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1234{
1235 struct gsm_mncc *disc = arg;
1236 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1237 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1238
1239 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1240
1241 gsm48_stop_cc_timer(trans);
1242 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1243
1244 /* cause */
1245 if (disc->fields & MNCC_F_CAUSE)
1246 gsm48_encode_cause(msg, 1, &disc->cause);
1247 else
1248 gsm48_encode_cause(msg, 1, &default_cause);
1249
1250 /* facility */
1251 if (disc->fields & MNCC_F_FACILITY)
1252 gsm48_encode_facility(msg, 0, &disc->facility);
1253 /* progress */
1254 if (disc->fields & MNCC_F_PROGRESS)
1255 gsm48_encode_progress(msg, 0, &disc->progress);
1256 /* user-user */
1257 if (disc->fields & MNCC_F_USERUSER)
1258 gsm48_encode_useruser(msg, 0, &disc->useruser);
1259
1260 /* store disconnect cause for T306 expiry */
1261 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1262
1263 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1264
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001265 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001266}
1267
1268static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1269{
1270 struct gsm48_hdr *gh = msgb_l3(msg);
1271 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1272 struct tlv_parsed tp;
1273 struct gsm_mncc rel;
1274 int rc;
1275
1276 gsm48_stop_cc_timer(trans);
1277
1278 memset(&rel, 0, sizeof(struct gsm_mncc));
1279 rel.callref = trans->callref;
1280 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1281 /* cause */
1282 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1283 rel.fields |= MNCC_F_CAUSE;
1284 gsm48_decode_cause(&rel.cause,
1285 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1286 }
1287 /* facility */
1288 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1289 rel.fields |= MNCC_F_FACILITY;
1290 gsm48_decode_facility(&rel.facility,
1291 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1292 }
1293 /* user-user */
1294 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1295 rel.fields |= MNCC_F_USERUSER;
1296 gsm48_decode_useruser(&rel.useruser,
1297 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1298 }
1299 /* ss-version */
1300 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1301 rel.fields |= MNCC_F_SSVERSION;
1302 gsm48_decode_ssversion(&rel.ssversion,
1303 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1304 }
1305
1306 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1307 /* release collision 5.4.5 */
1308 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1309 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001310 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001311 GSM48_PDISC_CC | (trans->transaction_id << 4),
1312 GSM48_MT_CC_RELEASE_COMPL);
1313 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1314 }
1315
1316 new_cc_state(trans, GSM_CSTATE_NULL);
1317
1318 trans->callref = 0;
1319 trans_free(trans);
1320
1321 return rc;
1322}
1323
1324static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1325{
1326 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001327 struct msgb *msg;
1328 struct gsm48_hdr *gh;
1329
1330 if (!trans->msc_a) {
1331 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1332 return -EINVAL;
1333 }
1334
1335 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1336 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001337
1338 gh->msg_type = GSM48_MT_CC_RELEASE;
1339
1340 gsm48_stop_cc_timer(trans);
1341 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1342
1343 /* cause */
1344 if (rel->fields & MNCC_F_CAUSE)
1345 gsm48_encode_cause(msg, 0, &rel->cause);
1346 /* facility */
1347 if (rel->fields & MNCC_F_FACILITY)
1348 gsm48_encode_facility(msg, 0, &rel->facility);
1349 /* user-user */
1350 if (rel->fields & MNCC_F_USERUSER)
1351 gsm48_encode_useruser(msg, 0, &rel->useruser);
1352
1353 trans->cc.T308_second = 0;
1354 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1355
1356 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1357 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1358
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001359 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001360}
1361
1362static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1363{
1364 struct gsm48_hdr *gh = msgb_l3(msg);
1365 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1366 struct tlv_parsed tp;
1367 struct gsm_mncc rel;
1368 int rc = 0;
1369
1370 gsm48_stop_cc_timer(trans);
1371
1372 memset(&rel, 0, sizeof(struct gsm_mncc));
1373 rel.callref = trans->callref;
1374 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1375 /* cause */
1376 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1377 rel.fields |= MNCC_F_CAUSE;
1378 gsm48_decode_cause(&rel.cause,
1379 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1380 }
1381 /* facility */
1382 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1383 rel.fields |= MNCC_F_FACILITY;
1384 gsm48_decode_facility(&rel.facility,
1385 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1386 }
1387 /* user-user */
1388 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1389 rel.fields |= MNCC_F_USERUSER;
1390 gsm48_decode_useruser(&rel.useruser,
1391 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1392 }
1393 /* ss-version */
1394 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1395 rel.fields |= MNCC_F_SSVERSION;
1396 gsm48_decode_ssversion(&rel.ssversion,
1397 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1398 }
1399
1400 if (trans->callref) {
1401 switch (trans->cc.state) {
1402 case GSM_CSTATE_CALL_PRESENT:
1403 rc = mncc_recvmsg(trans->net, trans,
1404 MNCC_REJ_IND, &rel);
1405 break;
1406 case GSM_CSTATE_RELEASE_REQ:
1407 rc = mncc_recvmsg(trans->net, trans,
1408 MNCC_REL_CNF, &rel);
1409 break;
1410 default:
1411 rc = mncc_recvmsg(trans->net, trans,
1412 MNCC_REL_IND, &rel);
1413 }
1414 }
1415
1416 trans->callref = 0;
1417 trans_free(trans);
1418
1419 return rc;
1420}
1421
1422static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1423{
1424 struct gsm_mncc *rel = arg;
1425 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1426 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1427 int ret;
1428
1429 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1430
1431 trans->callref = 0;
1432
1433 gsm48_stop_cc_timer(trans);
1434
1435 /* cause */
1436 if (rel->fields & MNCC_F_CAUSE)
1437 gsm48_encode_cause(msg, 0, &rel->cause);
1438 /* facility */
1439 if (rel->fields & MNCC_F_FACILITY)
1440 gsm48_encode_facility(msg, 0, &rel->facility);
1441 /* user-user */
1442 if (rel->fields & MNCC_F_USERUSER)
1443 gsm48_encode_useruser(msg, 0, &rel->useruser);
1444
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001445 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001446
1447 trans_free(trans);
1448
1449 return ret;
1450}
1451
1452static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1453{
1454 struct gsm48_hdr *gh = msgb_l3(msg);
1455 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1456 struct tlv_parsed tp;
1457 struct gsm_mncc fac;
1458
1459 memset(&fac, 0, sizeof(struct gsm_mncc));
1460 fac.callref = trans->callref;
1461 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1462 /* facility */
1463 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1464 fac.fields |= MNCC_F_FACILITY;
1465 gsm48_decode_facility(&fac.facility,
1466 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1467 }
1468 /* ss-version */
1469 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1470 fac.fields |= MNCC_F_SSVERSION;
1471 gsm48_decode_ssversion(&fac.ssversion,
1472 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1473 }
1474
1475 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1476}
1477
1478static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1479{
1480 struct gsm_mncc *fac = arg;
1481 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1482 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1483
1484 gh->msg_type = GSM48_MT_CC_FACILITY;
1485
1486 /* facility */
1487 gsm48_encode_facility(msg, 1, &fac->facility);
1488
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001489 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001490}
1491
1492static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1493{
1494 struct gsm_mncc hold;
1495
1496 memset(&hold, 0, sizeof(struct gsm_mncc));
1497 hold.callref = trans->callref;
1498 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1499}
1500
1501static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1502{
1503 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1504 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1505
1506 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1507
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001508 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001509}
1510
1511static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1512{
1513 struct gsm_mncc *hold_rej = arg;
1514 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1515 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1516
1517 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1518
1519 /* cause */
1520 if (hold_rej->fields & MNCC_F_CAUSE)
1521 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1522 else
1523 gsm48_encode_cause(msg, 1, &default_cause);
1524
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001525 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001526}
1527
1528static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1529{
1530 struct gsm_mncc retrieve;
1531
1532 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1533 retrieve.callref = trans->callref;
1534 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1535 &retrieve);
1536}
1537
1538static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1539{
1540 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1541 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1542
1543 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1544
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001545 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001546}
1547
1548static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1549{
1550 struct gsm_mncc *retrieve_rej = arg;
1551 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1553
1554 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1555
1556 /* cause */
1557 if (retrieve_rej->fields & MNCC_F_CAUSE)
1558 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1559 else
1560 gsm48_encode_cause(msg, 1, &default_cause);
1561
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001562 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001563}
1564
1565static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1566{
1567 struct gsm48_hdr *gh = msgb_l3(msg);
1568 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1569 struct tlv_parsed tp;
1570 struct gsm_mncc dtmf;
1571
1572 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1573 dtmf.callref = trans->callref;
1574 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1575 /* keypad facility */
1576 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1577 dtmf.fields |= MNCC_F_KEYPAD;
1578 gsm48_decode_keypad(&dtmf.keypad,
1579 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1580 }
1581
1582 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1583}
1584
1585static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1586{
1587 struct gsm_mncc *dtmf = arg;
1588 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1589 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1590
1591 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1592
1593 /* keypad */
1594 if (dtmf->fields & MNCC_F_KEYPAD)
1595 gsm48_encode_keypad(msg, dtmf->keypad);
1596
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001597 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001598}
1599
1600static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1601{
1602 struct gsm_mncc *dtmf = arg;
1603 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1604 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1605
1606 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1607
1608 /* cause */
1609 if (dtmf->fields & MNCC_F_CAUSE)
1610 gsm48_encode_cause(msg, 1, &dtmf->cause);
1611 else
1612 gsm48_encode_cause(msg, 1, &default_cause);
1613
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001614 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001615}
1616
1617static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1618{
1619 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1620 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1621
1622 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1623
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001624 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001625}
1626
1627static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1628{
1629 struct gsm_mncc dtmf;
1630
1631 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1632 dtmf.callref = trans->callref;
1633
1634 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1635}
1636
1637static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1638{
1639 struct gsm48_hdr *gh = msgb_l3(msg);
1640 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1641 struct tlv_parsed tp;
1642 struct gsm_mncc modify;
1643
1644 memset(&modify, 0, sizeof(struct gsm_mncc));
1645 modify.callref = trans->callref;
1646 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1647 /* bearer capability */
1648 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1649 modify.fields |= MNCC_F_BEARER_CAP;
1650 gsm48_decode_bearer_cap(&modify.bearer_cap,
1651 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1652
1653 /* Create a copy of the bearer capability
1654 * in the transaction struct, so we can use
1655 * this information later */
1656 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1657 sizeof(trans->bearer_cap));
1658 }
1659
1660 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1661
1662 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1663}
1664
1665static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1666{
1667 struct gsm_mncc *modify = arg;
1668 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1670
1671 gh->msg_type = GSM48_MT_CC_MODIFY;
1672
1673 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1674
1675 /* bearer capability */
1676 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1677 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1678
1679 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1680
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001681 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001682}
1683
1684static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1685{
1686 struct gsm48_hdr *gh = msgb_l3(msg);
1687 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1688 struct tlv_parsed tp;
1689 struct gsm_mncc modify;
1690
1691 gsm48_stop_cc_timer(trans);
1692
1693 memset(&modify, 0, sizeof(struct gsm_mncc));
1694 modify.callref = trans->callref;
1695 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1696 /* bearer capability */
1697 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1698 modify.fields |= MNCC_F_BEARER_CAP;
1699 gsm48_decode_bearer_cap(&modify.bearer_cap,
1700 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1701
1702 /* Create a copy of the bearer capability
1703 * in the transaction struct, so we can use
1704 * this information later */
1705 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1706 sizeof(trans->bearer_cap));
1707 }
1708
1709 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1710
1711 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1712}
1713
1714static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1715{
1716 struct gsm_mncc *modify = arg;
1717 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1719
1720 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1721
1722 /* bearer capability */
1723 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1724 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1725
1726 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1727
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001728 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001729}
1730
1731static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1732{
1733 struct gsm48_hdr *gh = msgb_l3(msg);
1734 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1735 struct tlv_parsed tp;
1736 struct gsm_mncc modify;
1737
1738 gsm48_stop_cc_timer(trans);
1739
1740 memset(&modify, 0, sizeof(struct gsm_mncc));
1741 modify.callref = trans->callref;
1742 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1743 /* bearer capability */
1744 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1745 modify.fields |= GSM48_IE_BEARER_CAP;
1746 gsm48_decode_bearer_cap(&modify.bearer_cap,
1747 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1748
1749 /* Create a copy of the bearer capability
1750 * in the transaction struct, so we can use
1751 * this information later */
1752 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1753 sizeof(trans->bearer_cap));
1754 }
1755 /* cause */
1756 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1757 modify.fields |= MNCC_F_CAUSE;
1758 gsm48_decode_cause(&modify.cause,
1759 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1760 }
1761
1762 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1763
1764 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1765}
1766
1767static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1768{
1769 struct gsm_mncc *modify = arg;
1770 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1771 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1772
1773 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1774
1775 /* bearer capability */
1776 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1777 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1778 /* cause */
1779 gsm48_encode_cause(msg, 1, &modify->cause);
1780
1781 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1782
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001783 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001784}
1785
1786static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1787{
1788 struct gsm_mncc *notify = arg;
1789 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1790 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1791
1792 gh->msg_type = GSM48_MT_CC_NOTIFY;
1793
1794 /* notify */
1795 gsm48_encode_notify(msg, notify->notify);
1796
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001797 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001798}
1799
1800static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1801{
1802 struct gsm48_hdr *gh = msgb_l3(msg);
1803 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1804// struct tlv_parsed tp;
1805 struct gsm_mncc notify;
1806
1807 memset(&notify, 0, sizeof(struct gsm_mncc));
1808 notify.callref = trans->callref;
1809// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1810 if (payload_len >= 1)
1811 gsm48_decode_notify(&notify.notify, gh->data);
1812
1813 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1814}
1815
1816static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1817{
1818 struct gsm_mncc *user = arg;
1819 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1820 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1821
1822 gh->msg_type = GSM48_MT_CC_USER_INFO;
1823
1824 /* user-user */
1825 if (user->fields & MNCC_F_USERUSER)
1826 gsm48_encode_useruser(msg, 1, &user->useruser);
1827 /* more data */
1828 if (user->more)
1829 gsm48_encode_more(msg);
1830
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001831 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001832}
1833
1834static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1835{
1836 struct gsm48_hdr *gh = msgb_l3(msg);
1837 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1838 struct tlv_parsed tp;
1839 struct gsm_mncc user;
1840
1841 memset(&user, 0, sizeof(struct gsm_mncc));
1842 user.callref = trans->callref;
1843 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1844 /* user-user */
1845 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1846 user.fields |= MNCC_F_USERUSER;
1847 gsm48_decode_useruser(&user.useruser,
1848 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1849 }
1850 /* more data */
1851 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1852 user.more = 1;
1853
1854 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1855}
1856
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001857static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1858 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001859 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001860{
1861 uint8_t data[sizeof(struct gsm_mncc)];
1862 struct gsm_mncc_rtp *rtp;
1863
1864 memset(&data, 0, sizeof(data));
1865 rtp = (struct gsm_mncc_rtp *) &data[0];
1866
1867 rtp->callref = callref;
1868 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001869 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001870 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1871 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001872 }
Harald Welte27989d42018-06-21 20:39:20 +02001873 rtp->payload_type = payload_type;
1874 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001875 if (sdp) {
1876 LOG_TRANS(trans, LOGL_DEBUG, "%s SDP: %s\n", get_mncc_name(rtp->msg_type), sdp_msg_to_str(sdp));
1877 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
1878 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001879 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001880}
1881
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001882static 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 +02001883{
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001884 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001885}
1886
1887static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1888{
1889 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001890
1891 /* Find callref */
1892 trans = trans_find_by_callref(net, callref);
1893 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001894 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001895 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001896 return -EIO;
1897 }
1898 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001899 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001900 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001901 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001902 return 0;
1903 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001904 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001905
Harald Welte27989d42018-06-21 20:39:20 +02001906 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001907 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001908}
1909
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +01001910int cc_cn_local_rtp_port_known(struct gsm_trans *cc_trans)
1911{
1912 switch(cc_trans->cc.state) {
1913 case GSM_CSTATE_INITIATED:
1914 if (cc_trans->cc.msg.msg_type != MNCC_SETUP_IND) {
1915 LOG_TRANS(cc_trans, LOGL_ERROR, "Assuming MO call, expected MNCC_SETUP_IND to be prepared\n");
1916 return -EINVAL;
1917 }
1918 /* This is the MO call leg, waiting for a CN RTP be able to send initial MNCC_SETUP_IND. */
1919 gsm48_cc_rx_setup_cn_local_rtp_port_known(cc_trans);
1920 return 0;
1921
1922 case GSM_CSTATE_MO_TERM_CALL_CONF:
1923 /* This is the MT call leg, waiting for a CN RTP to be able to send MNCC_CALL_CONF_IND. */
1924 return gsm48_cc_mt_rtp_port_and_codec_known(cc_trans);
1925
1926 default:
1927 LOG_TRANS(cc_trans, LOGL_ERROR, "CN RTP address available, but in unexpected state %d\n",
1928 cc_trans->cc.state);
1929 return -EINVAL;
1930 }
1931}
1932
1933int cc_assignment_done(struct gsm_trans *trans)
1934{
1935 struct msc_a *msc_a = trans->msc_a;
1936
1937 switch (trans->cc.state) {
1938 case GSM_CSTATE_INITIATED:
1939 case GSM_CSTATE_MO_CALL_PROC:
1940 /* MO call */
1941 break;
1942
1943 case GSM_CSTATE_CALL_RECEIVED:
1944 case GSM_CSTATE_MO_TERM_CALL_CONF:
1945 /* MT call */
1946 break;
1947
1948 case GSM_CSTATE_ACTIVE:
1949 /* already active. MNCC finished before Abis completed the Assignment. */
1950 break;
1951
1952 default:
1953 LOG_TRANS(trans, LOGL_ERROR, "Assignment done in unexpected CC state: %d\n", trans->cc.state);
1954 return -EINVAL;
1955 }
1956
1957 if (!call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN)) {
1958 LOG_TRANS(trans, LOGL_DEBUG,
1959 "Assignment complete, but still waiting for the CRCX OK on the CN side RTP\n");
1960 return 0;
1961 }
1962 return gsm48_tch_rtp_create(trans);
1963}
1964
Harald Welte27989d42018-06-21 20:39:20 +02001965/* Trigger TCH_RTP_CREATE acknowledgement */
1966int gsm48_tch_rtp_create(struct gsm_trans *trans)
1967{
1968 /* This function is called as soon as the port, on which the
1969 * mgcp-gw expects the incoming RTP stream from the remote
1970 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001971 struct msc_a *msc_a = trans->msc_a;
1972 struct gsm_network *net = msc_a_net(msc_a);
1973 struct call_leg *cl = msc_a->cc.call_leg;
1974 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001975 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001976 int mncc_payload_msg_type;
1977 struct sdp_audio_codec *codec;
1978 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001979
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001980 if (!rtp_cn) {
1981 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1982 return -EINVAL;
1983 }
1984
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001985 codec_filter_run(&trans->cc.codecs);
1986 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1987
1988 if (!trans->cc.codecs.result.audio_codecs.count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001989 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001990 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001991 return -EINVAL;
1992 }
1993
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001994 /* Modify the MGW endpoint if necessary, usually this should already match and not cause MGCP. */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001995 rtp_stream_set_codecs(rtp_cn, &trans->cc.codecs.result.audio_codecs);
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001996 rtp_stream_commit(rtp_cn);
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001997
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001998 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001999 codec = &rtp_cn->codecs.codec[0];
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002000 m = codec_mapping_by_subtype_name(codec->subtype_name);
2001 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02002002
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002003 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
2004 if (!rtp_cn_local) {
2005 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
2006 return -EINVAL;
2007 }
2008
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002009 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002010 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02002011}
2012
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002013static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002014{
2015 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016 struct call_leg *cl;
2017 struct rtp_stream *rtps;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002018 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02002019
Philipp Maier8ad3dac2018-08-07 13:00:14 +02002020 /* FIXME: in *rtp we should get the codec information of the remote
2021 * leg. We will have to populate trans->conn->rtp.codec_cn with a
2022 * meaningful value based on this information but unfortunately we
2023 * can't do that yet because the mncc API can not signal dynamic
2024 * payload types yet. This must be fixed first. Also there may be
2025 * additional members necessary in trans->conn->rtp because we
2026 * somehow need to deal with dynamic payload types that do not
2027 * comply to 3gpp's assumptions of payload type numbers on the A
2028 * interface. See also related tickets: OS#3399 and OS1683 */
2029
Harald Welte27989d42018-06-21 20:39:20 +02002030 /* Find callref */
2031 trans = trans_find_by_callref(net, rtp->callref);
2032 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002033 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002034 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02002035 return -EIO;
2036 }
2037 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002038 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002039 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002040 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002041 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02002042 }
2043
Neels Hofmeyr90933d42022-01-13 20:10:52 +01002044 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(rtp->msg_type),
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002045 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
2046 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002047
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002048 cl = trans->msc_a->cc.call_leg;
2049 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
2050
2051 if (!rtps) {
2052 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
2053 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2054 return -EINVAL;
2055 }
2056
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002057 if (rtp->sdp[0]) {
2058 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, rtp->sdp);
2059 LOG_TRANS(trans, LOGL_DEBUG, "%s contained SDP %s\n",
2060 get_mncc_name(rtp->msg_type),
2061 sdp_msg_to_str(&trans->cc.codecs.remote));
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002062 }
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002063
2064 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
2065
2066 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
2067 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
2068 struct osmo_sockaddr_str rtp_addr;
2069 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
2070 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
2071 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2072 return -EINVAL;
2073 }
2074 rtp_stream_set_remote_addr(rtps, &rtp_addr);
2075 }
2076
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002077 rtp_stream_commit(rtps);
2078 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02002079}
2080
2081static struct downstate {
2082 uint32_t states;
2083 int type;
2084 int (*rout) (struct gsm_trans *trans, void *arg);
2085} downstatelist[] = {
2086 /* mobile originating call establishment */
2087 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2088 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2089 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2090 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2091 {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 */
2092 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2093 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2094 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2095 /* mobile terminating call establishment */
2096 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2097 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2098 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2099 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2100 /* signalling during call */
2101 {SBIT(GSM_CSTATE_ACTIVE),
2102 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2103 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2104 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2105 {ALL_STATES,
2106 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2107 {ALL_STATES,
2108 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2109 {ALL_STATES,
2110 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2111 {SBIT(GSM_CSTATE_ACTIVE),
2112 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2113 {SBIT(GSM_CSTATE_ACTIVE),
2114 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2115 {SBIT(GSM_CSTATE_ACTIVE),
2116 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2117 {SBIT(GSM_CSTATE_ACTIVE),
2118 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2119 {SBIT(GSM_CSTATE_ACTIVE),
2120 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2121 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2122 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2123 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2124 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2125 {SBIT(GSM_CSTATE_ACTIVE),
2126 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2127 /* clearing */
2128 {SBIT(GSM_CSTATE_INITIATED),
2129 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2130 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2131 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2132 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2133 MNCC_REL_REQ, gsm48_cc_tx_release},
2134};
2135
2136#define DOWNSLLEN \
2137 (sizeof(downstatelist) / sizeof(struct downstate))
2138
2139
Philipp Maiercd64af72019-08-01 09:46:40 +02002140static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002141{
2142 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002143 struct msc_a *msc_a = NULL;
2144 struct gsm_trans *trans = NULL;
2145 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002146
Harald Welte27989d42018-06-21 20:39:20 +02002147 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002148 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002149 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002150 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002151 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002152 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002153 return rc;
2154 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002155 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02002156 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002157 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002158 case MNCC_RTP_FREE:
2159 /* unused right now */
2160 return -EIO;
2161
2162 case MNCC_FRAME_DROP:
2163 case MNCC_FRAME_RECV:
2164 case GSM_TCHF_FRAME:
2165 case GSM_TCHF_FRAME_EFR:
2166 case GSM_TCHH_FRAME:
2167 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002168 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002169 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002170 return -ENOTSUP;
2171 }
2172
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002173 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002174
2175 /* Find callref */
2176 trans = trans_find_by_callref(net, data->callref);
2177
2178 /* Callref unknown */
2179 if (!trans) {
2180 struct vlr_subscr *vsub;
2181
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002182 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002183 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002184 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002185 /* Invalid call reference */
2186 return mncc_release_ind(net, NULL, data->callref,
2187 GSM48_CAUSE_LOC_PRN_S_LU,
2188 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2189 }
2190 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002191 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002192 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002193 /* Invalid number */
2194 return mncc_release_ind(net, NULL, data->callref,
2195 GSM48_CAUSE_LOC_PRN_S_LU,
2196 GSM48_CC_CAUSE_INV_NR_FORMAT);
2197 }
2198 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002199 if (data->called.number[0]) {
2200 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2201 if (!vsub)
2202 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002203 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002204 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002205 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002206 if (!vsub)
2207 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002208 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002209 }
2210 if (!vsub)
2211 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002212 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002213 /* update the subscriber we deal with */
2214 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2215
Harald Welte27989d42018-06-21 20:39:20 +02002216 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002217 if (!vsub->lu_complete) {
2218 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002219 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002220 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002221 /* Temporarily out of order */
2222 return mncc_release_ind(net, NULL, data->callref,
2223 GSM48_CAUSE_LOC_PRN_S_LU,
2224 GSM48_CC_CAUSE_DEST_OOO);
2225 }
Keith Whyte991bb422019-08-08 15:43:40 +02002226
2227 /* Find valid conn */
2228 msc_a = msc_a_for_vsub(vsub, true);
2229
2230 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2231 if (!net->call_waiting && msc_a) {
2232 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2233 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2234 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2235 "rx '%s' for subscriber %s with trans state (%s)"
2236 " rejecting with USER_BUSY\n",
2237 get_mncc_name(msg->msg_type), data->called.number,
2238 gsm48_cc_state_name(existing_cc_trans->cc.state));
2239 return mncc_release_ind(net, NULL, data->callref,
2240 GSM48_CAUSE_LOC_PRN_S_LU,
2241 GSM48_CC_CAUSE_USER_BUSY);
2242 }
2243 }
2244
Harald Welte27989d42018-06-21 20:39:20 +02002245 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002246 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002247 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002248 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002249 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002250 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002251 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002252 mncc_release_ind(net, NULL, data->callref,
2253 GSM48_CAUSE_LOC_PRN_S_LU,
2254 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2255 return -ENOMEM;
2256 }
2257
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002258 /* Remember remote SDP, if any */
2259 if (data->sdp[0]) {
2260 if (sdp_msg_from_sdp_str(&trans->cc.codecs.remote, data->sdp)) {
2261 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse incoming SDP: %s\n",
2262 osmo_quote_str(data->sdp, -1));
2263 vlr_subscr_put(vsub, __func__);
2264 mncc_release_ind(net, NULL, data->callref,
2265 GSM48_CAUSE_LOC_PRN_S_LU,
2266 GSM48_CC_CAUSE_NORMAL_UNSPEC);
2267 return -EINVAL;
2268 }
2269 }
2270
Harald Welte27989d42018-06-21 20:39:20 +02002271 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002272 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002273 /* This condition will return before the common logging of the received MNCC message below, so
2274 * log it now. */
2275 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002276
Harald Welte27989d42018-06-21 20:39:20 +02002277 /* store setup information until paging succeeds */
2278 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2279
Keith Whyte0dbbec32022-10-15 01:18:43 +01002280 /* Store the Global Call Reference in the transaction if MNCC sent it */
2281 if (data->fields & MNCC_F_GCR) {
2282 /* The trans has no msc_a yet so compose will return NULL
2283 // trans->cc.lcls = trans_lcls_compose(trans, true); */
2284 trans->cc.lcls = talloc_zero(trans, struct osmo_lcls);
2285 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
2286 &data->gcr[0],
2287 sizeof(data->gcr));
2288 if (rc < 0) {
2289 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Failed to parse GCR\n");
2290 } else {
2291 trans->cc.lcls->config = GSM0808_LCLS_CFG_BOTH_WAY;
2292 trans->cc.lcls->control = GSM0808_LCLS_CSC_DO_NOT_CONNECT;
2293 trans->cc.lcls->corr_needed = true;
2294 trans->cc.lcls->gcr_available = true;
2295 }
2296 }
2297
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002298 /* Request a channel. If Paging already started, paging_request_start() will append the new
2299 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002300 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2301 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002302 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002303 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002304 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002305 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002306 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002307 return 0;
2308 }
2309
2310 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002311 trans->msc_a = msc_a;
2312 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002313 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002314 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002315 } else {
2316 /* update the subscriber we deal with */
2317 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2318 }
2319
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002320 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002321
Philipp Maier9ca7b312018-10-10 17:00:49 +02002322 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002323 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002324
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002325 if (trans->msc_a)
2326 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002327
2328 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002329 if (!msc_a) {
2330 struct gsm_mncc rel = {
2331 .callref = data->callref,
2332 };
2333 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002334 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2335 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002336 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002337 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2338 else
2339 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2340 trans->callref = 0;
2341 trans_free(trans);
2342 return rc;
2343 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002344 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002345 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002346 }
2347
2348 /* Find function for current state and message */
2349 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002350 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002351 && ((1 << trans->cc.state) & downstatelist[i].states))
2352 break;
2353 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002354 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002355 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002356 return 0;
2357 }
2358
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002359 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002360
2361 return rc;
2362}
2363
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002364struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2365{
2366 uint32_t callref;
2367
2368 switch (msg->msg_type) {
2369 case MNCC_BRIDGE:
2370 callref = msg->bridge.callref[0];
2371 break;
2372 case MNCC_RTP_CREATE:
2373 case MNCC_RTP_CONNECT:
2374 callref = msg->rtp.callref;
2375 break;
2376
2377 case MNCC_RTP_FREE:
2378 case MNCC_FRAME_DROP:
2379 case MNCC_FRAME_RECV:
2380 case GSM_TCHF_FRAME:
2381 case GSM_TCHF_FRAME_EFR:
2382 case GSM_TCHH_FRAME:
2383 case GSM_TCH_FRAME_AMR:
2384 return NULL;
2385
2386 default:
2387 callref = msg->signal.callref;
2388 break;
2389 }
2390
2391 return mncc_call_find_by_callref(callref);
2392}
2393
2394/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002395int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002396{
2397 const union mncc_msg *msg = arg;
2398 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002399
2400 if (msg->msg_type == MNCC_SETUP_REQ) {
2401 /* Incoming call to forward for inter-MSC Handover? */
2402 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2403 if (mncc_call)
2404 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2405 "Incoming call matches pending inter-MSC Handover Number\n");
2406 }
2407 if (!mncc_call) {
2408 /* Find already active MNCC FSM for this callref.
2409 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2410 * MNCC<->GSM-CC call handling. */
2411 mncc_call = mncc_find_by_callref_from_msg(msg);
2412 }
2413 if (mncc_call) {
2414 mncc_call_rx(mncc_call, msg);
2415 return 0;
2416 }
2417
2418 /* None of the above? Then it must be a normal GSM CC call related message. */
2419 return mncc_tx_to_gsm_cc(net, msg);
2420}
Harald Welte27989d42018-06-21 20:39:20 +02002421
2422static struct datastate {
2423 uint32_t states;
2424 int type;
2425 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2426} datastatelist[] = {
2427 /* mobile originating call establishment */
2428 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2429 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2430 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2431 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2432 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2433 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2434 /* mobile terminating call establishment */
2435 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2436 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2437 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2438 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2439 {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 */
2440 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2441 /* signalling during call */
2442 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2443 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2444 {SBIT(GSM_CSTATE_ACTIVE),
2445 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2446 {ALL_STATES,
2447 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2448 {ALL_STATES,
2449 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2450 {ALL_STATES,
2451 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2452 {SBIT(GSM_CSTATE_ACTIVE),
2453 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2454 {SBIT(GSM_CSTATE_ACTIVE),
2455 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2456 {SBIT(GSM_CSTATE_ACTIVE),
2457 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2458 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2459 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2460 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2461 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2462 {SBIT(GSM_CSTATE_ACTIVE),
2463 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2464 /* clearing */
2465 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2466 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2467 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2468 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2469 {ALL_STATES, /* 5.4.3.4 */
2470 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2471};
2472
2473#define DATASLLEN \
2474 (sizeof(datastatelist) / sizeof(struct datastate))
2475
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002476int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002477{
2478 struct gsm48_hdr *gh = msgb_l3(msg);
2479 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2480 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2481 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002482 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2483 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002484 int i, rc = 0;
2485
2486 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002487 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002488 return -EINVAL;
2489 }
2490
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002491 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002492 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002493 return -EINVAL;
2494 }
2495
2496 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002497 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002498
Harald Welte27989d42018-06-21 20:39:20 +02002499 /* Create transaction */
2500 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002501 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002502 trans = trans_alloc(net, vsub,
2503 TRANS_CC,
2504 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002505 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002506 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002507 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002508 GSM48_PDISC_CC | (transaction_id << 4),
2509 GSM48_MT_CC_RELEASE_COMPL);
2510 return -ENOMEM;
2511 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002512 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2513 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2514 trans_free(trans);
2515 return -EINVAL;
2516 }
2517
Harald Welte27989d42018-06-21 20:39:20 +02002518 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002519 msc_a_get(msc_a, MSC_A_USE_CC);
2520 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002521 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002522
2523 /* An earlier CM Service Request for this CC message now has concluded */
2524 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2525 LOG_MSC_A(msc_a, LOGL_ERROR,
2526 "Creating new CC transaction without prior CM Service Request\n");
2527 else
2528 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002529 }
2530
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002531 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2532 gsm48_cc_state_name(trans->cc.state));
2533
Harald Welte27989d42018-06-21 20:39:20 +02002534 /* find function for current state and message */
2535 for (i = 0; i < DATASLLEN; i++)
2536 if ((msg_type == datastatelist[i].type)
2537 && ((1 << trans->cc.state) & datastatelist[i].states))
2538 break;
2539 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002540 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002541
2542 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2543 * transaction right away. */
2544 if (trans->cc.state == GSM_CSTATE_NULL) {
2545 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2546 " -- disarding new CC transaction right away\n");
2547 trans_free(trans);
2548 }
Harald Welte27989d42018-06-21 20:39:20 +02002549 return 0;
2550 }
2551
2552 assert(trans->vsub);
2553
2554 rc = datastatelist[i].rout(trans, msg);
2555
Harald Welte27989d42018-06-21 20:39:20 +02002556 return rc;
2557}