blob: fe34127ff37599c4b4aa07b5b3ec4028b6977d44 [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>
Oliver Smith5375f782023-05-23 13:38:33 +020046#include <osmocom/msc/transaction_cc.h>
Harald Welte27989d42018-06-21 20:39:20 +020047#include <osmocom/msc/silent_call.h>
Harald Welte27989d42018-06-21 20:39:20 +020048#include <osmocom/msc/mncc_int.h>
49#include <osmocom/abis/e1_input.h>
50#include <osmocom/core/bitvec.h>
51#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010052#include <osmocom/msc/msub.h>
53#include <osmocom/msc/msc_a.h>
54#include <osmocom/msc/paging.h>
55#include <osmocom/msc/call_leg.h>
56#include <osmocom/msc/rtp_stream.h>
57#include <osmocom/msc/mncc_call.h>
58#include <osmocom/msc/msc_t.h>
Neels Hofmeyr58f40882023-03-08 04:04:27 +010059#include <osmocom/msc/sdp_msg.h>
Neels Hofmeyra001a702022-10-31 17:57:30 +010060#include <osmocom/msc/codec_mapping.h>
Harald Welte27989d42018-06-21 20:39:20 +020061
62#include <osmocom/gsm/gsm48.h>
63#include <osmocom/gsm/gsm0480.h>
64#include <osmocom/gsm/gsm_utils.h>
65#include <osmocom/gsm/protocol/gsm_04_08.h>
66#include <osmocom/core/msgb.h>
67#include <osmocom/core/talloc.h>
68#include <osmocom/core/utils.h>
69#include <osmocom/core/byteswap.h>
70#include <osmocom/gsm/tlv.h>
71#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020072
73#include <assert.h>
74
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010075static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
76static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
77static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
78
79static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
80{
81 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
82 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
83 OMSC_LINKID_CB(msg) = trans->dlci;
84
85 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
86}
87
88uint32_t msc_cc_next_outgoing_callref() {
89 static uint32_t last_callref = 0x80000000;
90 last_callref++;
91 if (last_callref < 0x80000001)
92 last_callref = 0x80000001;
93 return last_callref;
94}
Harald Welte27989d42018-06-21 20:39:20 +020095
Philipp Maier9ca7b312018-10-10 17:00:49 +020096static void gsm48_cc_guard_timeout(void *arg)
97{
98 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010099 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200100 trans_free(trans);
101 return;
102}
103
104static void gsm48_stop_guard_timer(struct gsm_trans *trans)
105{
106 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100107 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200108 osmo_timer_del(&trans->cc.timer_guard);
109 }
110}
111
112static void gsm48_start_guard_timer(struct gsm_trans *trans)
113{
114 /* NOTE: The purpose of this timer is to prevent the cc state machine
115 * from hanging in cases where mncc, gsm48 or both become unresponsive
116 * for some reason. The timer is started initially with the setup from
117 * the gsm48 side and then re-started with every incoming mncc message.
118 * Once the mncc state reaches its active state the timer is stopped.
119 * So if the cc state machine does not show any activity for an
120 * extended amount of time during call setup or teardown the guard
121 * timer will time out and hard-clear the connection. */
122 if (osmo_timer_pending(&trans->cc.timer_guard))
123 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100124 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200125 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
126 osmo_timer_schedule(&trans->cc.timer_guard,
127 trans->net->mncc_guard_timeout, 0);
128}
Harald Welte27989d42018-06-21 20:39:20 +0200129
130/* Call Control */
131
Neels Hofmeyre80f5562023-06-28 02:21:15 +0200132static void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +0200133{
134 net->mncc_recv(net, msg);
135}
136
137int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
138{
139 struct gsm48_hdr *gh;
140 struct msgb *ss_notify;
141
142 ss_notify = gsm0480_create_notifySS(message);
143 if (!ss_notify)
144 return -1;
145
146 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
147 uint8_t *data = msgb_push(ss_notify, 1);
148 data[0] = ss_notify->len - 1;
149 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
150 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100151 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200152}
153
154/* FIXME: this count_statistics is a state machine behaviour. we should convert
155 * the complete call control into a state machine. Afterwards we can move this
156 * code into state transitions.
157 */
158static void count_statistics(struct gsm_trans *trans, int new_state)
159{
160 int old_state = trans->cc.state;
161 struct rate_ctr_group *msc = trans->net->msc_ctrs;
162
163 if (old_state == new_state)
164 return;
165
166 /* state incoming */
167 switch (new_state) {
168 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200169 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
170 1);
171 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200172 break;
173 }
174
175 /* state outgoing */
176 switch (old_state) {
177 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200178 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
179 1);
Harald Welte27989d42018-06-21 20:39:20 +0200180 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
181 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200182 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200183 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200184 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200185 break;
186 }
187}
188
Harald Welte27989d42018-06-21 20:39:20 +0200189static void new_cc_state(struct gsm_trans *trans, int state)
190{
191 if (state > 31 || state < 0)
192 return;
193
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100194 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
195 gsm48_cc_state_name(trans->cc.state),
196 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200197
198 count_statistics(trans, state);
199 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200200
201 /* Stop the guard timer when a call reaches the active state */
202 if (state == GSM_CSTATE_ACTIVE)
203 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200204}
205
206static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
207{
208 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
210 uint8_t *cause, *call_state;
211
212 gh->msg_type = GSM48_MT_CC_STATUS;
213
214 cause = msgb_put(msg, 3);
215 cause[0] = 2;
216 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
217 cause[2] = 0x80 | 30; /* response to status inquiry */
218
219 call_state = msgb_put(msg, 1);
220 call_state[0] = 0xc0 | 0x00;
221
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100222 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200223}
224
225static void gsm48_stop_cc_timer(struct gsm_trans *trans)
226{
227 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100228 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200229 osmo_timer_del(&trans->cc.timer);
230 trans->cc.Tcurrent = 0;
231 }
232}
233
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100234/* Log the MNCC tx and rx events.
235 * Depending on msg_type, also log whether RTP information is passed on.
236 * (This is particularly interesting for the doc/sequence_charts/msc_log_to_ladder.py)
237 */
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200238#define log_mncc_rx_tx(ARGS...) _log_mncc_rx_tx(__FILE__, __LINE__, ##ARGS)
239static void _log_mncc_rx_tx(const char *file, int line,
240 struct gsm_trans *trans, const char *rx_tx, const union mncc_msg *mncc)
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100241{
242 const char *sdp = NULL;
243 struct sdp_msg sdp_msg = {};
244 struct osmo_sockaddr addr = {};
245
246 if (!log_check_level(DMNCC, LOGL_DEBUG))
247 return;
248
249 switch (mncc->msg_type) {
250 case MNCC_RTP_CREATE:
251 case MNCC_RTP_CONNECT:
252 addr = (struct osmo_sockaddr){ .u.sas = mncc->rtp.addr };
253 sdp = mncc->rtp.sdp;
254 break;
255
256 case MNCC_SETUP_IND:
257 case MNCC_SETUP_REQ:
258 case MNCC_SETUP_COMPL_IND:
259 case MNCC_SETUP_COMPL_REQ:
260 case MNCC_SETUP_RSP:
261 case MNCC_SETUP_CNF:
262 case MNCC_CALL_CONF_IND:
263 case MNCC_CALL_PROC_REQ:
264 case MNCC_ALERT_IND:
265 case MNCC_ALERT_REQ:
266 sdp = mncc->signal.sdp;
267 break;
268
269 default:
270 break;
271 }
272
273 if (sdp && sdp[0] && (sdp_msg_from_sdp_str(&sdp_msg, sdp) == 0)) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200274 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
275 rx_tx,
276 get_mncc_name(mncc->msg_type),
277 sdp_msg_to_str(&sdp_msg));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100278 return;
279 }
280
281 if (osmo_sockaddr_is_any(&addr) == 0) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200282 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
283 rx_tx,
284 get_mncc_name(mncc->msg_type),
285 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100286 return;
287 }
288
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200289 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s\n", rx_tx, get_mncc_name(mncc->msg_type));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100290}
291
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200292#define mncc_recvmsg(ARGS...) _mncc_recvmsg(__FILE__, __LINE__, ##ARGS)
293static int _mncc_recvmsg(const char *file, int line,
294 struct gsm_network *net, struct gsm_trans *trans, int msg_type, struct gsm_mncc *mncc)
Harald Welte27989d42018-06-21 20:39:20 +0200295{
296 struct msgb *msg;
297 unsigned char *data;
298
Harald Welte27989d42018-06-21 20:39:20 +0200299 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100300 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200301
302 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
303 if (!msg)
304 return -ENOMEM;
305
306 data = msgb_put(msg, sizeof(struct gsm_mncc));
307 memcpy(data, mncc, sizeof(struct gsm_mncc));
308
309 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200310 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
311 if (trans)
312 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200313
314 return 0;
315}
316
317int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
318 uint32_t callref, int location, int value)
319{
320 struct gsm_mncc rel;
321
322 memset(&rel, 0, sizeof(rel));
323 rel.callref = callref;
324 mncc_set_cause(&rel, location, value);
325 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
326 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
327 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
328}
329
330/* Call Control Specific transaction release.
331 * gets called by trans_free, DO NOT CALL YOURSELF! */
332void _gsm48_cc_trans_free(struct gsm_trans *trans)
333{
334 gsm48_stop_cc_timer(trans);
335
Harald Welte27989d42018-06-21 20:39:20 +0200336 /* send release to L4, if callref still exists */
337 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100338 /* Send MNCC REL.ind (cause='Resource unavailable') */
339 if (trans->cc.mncc_initiated) {
340 mncc_release_ind(trans->net, trans, trans->callref,
341 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100342 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
343 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100344 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
345 }
346
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100347 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
348 * CC Release to the MS if it gets freed here. Hack it to do so. */
349 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
350 struct gsm_mncc rel = {};
351 rel.callref = trans->callref;
352 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
353 gsm48_cc_tx_release(trans, &rel);
354 }
Harald Welte27989d42018-06-21 20:39:20 +0200355 /* This is a final freeing of the transaction. The MNCC release may have triggered the
356 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
357 gsm48_stop_cc_timer(trans);
358 }
359 if (trans->cc.state != GSM_CSTATE_NULL)
360 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200361
362 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100363
364 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
365 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200366}
367
Harald Welte27989d42018-06-21 20:39:20 +0200368/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100369static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200370{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100371 if (trans->msc_a) {
372 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
373 "Handle paging error: transaction already associated with subscriber,"
374 " apparently it was already handled. Skip.\n");
375 return;
Harald Welte27989d42018-06-21 20:39:20 +0200376 }
377
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100378 if (msc_a) {
379 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
380 /* Assign conn */
381 msc_a_get(msc_a, MSC_A_USE_CC);
382 trans->msc_a = msc_a;
383 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200384
385 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300386 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200387 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300388 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
389 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
390 &trans->cc.msg.gcr[0],
391 sizeof(trans->cc.msg.gcr));
392 if (rc < 0)
393 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
394 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200395 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200396 }
397
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100398 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
399 /* send SETUP request to called party */
400 gsm48_cc_tx_setup(trans, &trans->cc.msg);
401 } else {
402 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
403 /* Temporarily out of order */
404 mncc_release_ind(trans->net, trans,
405 trans->callref,
406 GSM48_CAUSE_LOC_PRN_S_LU,
407 GSM48_CC_CAUSE_DEST_OOO);
408 trans->callref = 0;
409 trans->paging_request = NULL;
410 trans_free(trans);
411 }
Harald Welte27989d42018-06-21 20:39:20 +0200412}
413
414/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100415static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200416{
Andreas Eversberg7e4b0322023-04-23 11:43:13 +0200417 struct gsm_trans *trans1 = trans_find_by_callref(net, TRANS_CC, bridge->callref[0]);
418 struct gsm_trans *trans2 = trans_find_by_callref(net, TRANS_CC, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100419 struct call_leg *cl1;
420 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200421
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100422 if (!trans1 || !trans2) {
423 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200424 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100425 }
Harald Welte27989d42018-06-21 20:39:20 +0200426
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100427 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100428 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
429 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 +0200430 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100431 }
432
433 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
434 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200435
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100436 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
437 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
438 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
439 * If we can't, then we must give up. */
440 cl1 = trans1->msc_a->cc.call_leg;
441 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200442
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200443 return call_leg_local_bridge(cl1, trans1->call_id, trans1, cl2, trans2->call_id, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200444}
445
446static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
447{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100448 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200449 return gsm48_cc_tx_status(trans, msg);
450}
451
Harald Welte27989d42018-06-21 20:39:20 +0200452static void gsm48_cc_timeout(void *arg)
453{
454 struct gsm_trans *trans = arg;
455 int disconnect = 0, release = 0;
456 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
457 int mo_location = GSM48_CAUSE_LOC_USER;
458 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
459 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
460 struct gsm_mncc mo_rel, l4_rel;
461
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200462 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
463
Harald Welte27989d42018-06-21 20:39:20 +0200464 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
465 mo_rel.callref = trans->callref;
466 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
467 l4_rel.callref = trans->callref;
468
469 switch(trans->cc.Tcurrent) {
470 case 0x303:
471 release = 1;
472 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
473 break;
474 case 0x310:
475 disconnect = 1;
476 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
477 break;
478 case 0x313:
479 disconnect = 1;
480 /* unknown, did not find it in the specs */
481 break;
482 case 0x301:
483 disconnect = 1;
484 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
485 break;
486 case 0x308:
487 if (!trans->cc.T308_second) {
488 /* restart T308 a second time */
489 gsm48_cc_tx_release(trans, &trans->cc.msg);
490 trans->cc.T308_second = 1;
491 break; /* stay in release state */
492 }
493 trans_free(trans);
494 return;
495 case 0x306:
496 release = 1;
497 mo_cause = trans->cc.msg.cause.value;
498 mo_location = trans->cc.msg.cause.location;
499 break;
500 case 0x323:
501 disconnect = 1;
502 break;
503 default:
504 release = 1;
505 }
506
507 if (release && trans->callref) {
508 /* process release towards layer 4 */
509 mncc_release_ind(trans->net, trans, trans->callref,
510 l4_location, l4_cause);
511 trans->callref = 0;
512 }
513
514 if (disconnect && trans->callref) {
515 /* process disconnect towards layer 4 */
516 mncc_set_cause(&l4_rel, l4_location, l4_cause);
517 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
518 }
519
520 /* process disconnect towards mobile station */
521 if (disconnect || release) {
522 mncc_set_cause(&mo_rel, mo_location, mo_cause);
523 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
524 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
525 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
526 mo_rel.cause.diag_len = 3;
527
528 if (disconnect)
529 gsm48_cc_tx_disconnect(trans, &mo_rel);
530 if (release)
531 gsm48_cc_tx_release(trans, &mo_rel);
532 }
533
534}
535
536/* disconnect both calls from the bridge */
537static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100538 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200539{
Andreas Eversberg7e4b0322023-04-23 11:43:13 +0200540 struct gsm_trans *trans0 = trans_find_by_callref(net, TRANS_CC, bridge->callref[0]);
541 struct gsm_trans *trans1 = trans_find_by_callref(net, TRANS_CC, bridge->callref[1]);
Harald Welte27989d42018-06-21 20:39:20 +0200542 struct gsm_mncc mx_rel;
543 if (!trans0 || !trans1)
544 return;
545
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100546 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
547 trans0->callref, trans1->callref, strerror(err));
548 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200549 trans0->callref, trans1->callref, strerror(err));
550
551 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
552 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
553 GSM48_CC_CAUSE_CHAN_UNACCEPT);
554
555 mx_rel.callref = trans0->callref;
556 gsm48_cc_tx_disconnect(trans0, &mx_rel);
557
558 mx_rel.callref = trans1->callref;
559 gsm48_cc_tx_disconnect(trans1, &mx_rel);
560}
561
562static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
563 int sec, int micro)
564{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100565 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200566 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
567 osmo_timer_schedule(&trans->cc.timer, sec, micro);
568 trans->cc.Tcurrent = current;
569}
570
571static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
572{
573 struct gsm48_hdr *gh = msgb_l3(msg);
574 uint8_t msg_type = gsm48_hdr_msg_type(gh);
575 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
576 struct tlv_parsed tp;
577 struct gsm_mncc setup;
578
Philipp Maier9ca7b312018-10-10 17:00:49 +0200579 gsm48_start_guard_timer(trans);
580
Harald Welte27989d42018-06-21 20:39:20 +0200581 memset(&setup, 0, sizeof(struct gsm_mncc));
582 setup.callref = trans->callref;
583
Keith Whytea1a70be2021-05-16 02:59:52 +0200584 /* New Global Call Reference */
585 if (!trans->cc.lcls)
586 trans->cc.lcls = trans_lcls_compose(trans, true);
587
588 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300589 if (trans->cc.lcls) {
590 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
591 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
592 int rc;
593
594 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
595 memcpy(&setup.gcr[0], gcr_msg->data, rc);
596 setup.fields |= MNCC_F_GCR;
597 } else
598 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
599 msgb_free(gcr_msg);
600 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200601
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100602 OSMO_ASSERT(trans->msc_a);
603
Harald Welte27989d42018-06-21 20:39:20 +0200604 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
605 /* emergency setup is identified by msg_type */
606 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
607 setup.fields |= MNCC_F_EMERGENCY;
608 setup.emergency = 1;
609 /* use destination number as configured by user (if any) */
610 if (trans->net->emergency.route_to_msisdn) {
611 setup.fields |= MNCC_F_CALLED;
612 setup.called.type = 0; /* unknown */
613 setup.called.plan = 0; /* unknown */
614 OSMO_STRLCPY_ARRAY(setup.called.number,
615 trans->net->emergency.route_to_msisdn);
616 }
617 }
618
619 /* use subscriber as calling party number */
620 setup.fields |= MNCC_F_CALLING;
621 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
622 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
623
624 /* bearer capability */
625 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
626 setup.fields |= MNCC_F_BEARER_CAP;
627 gsm48_decode_bearer_cap(&setup.bearer_cap,
628 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
629
630 /* Create a copy of the bearer capability
631 * in the transaction struct, so we can use
632 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100633 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200634 sizeof(trans->bearer_cap));
635 }
636 /* facility */
637 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
638 setup.fields |= MNCC_F_FACILITY;
639 gsm48_decode_facility(&setup.facility,
640 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
641 }
642 /* called party bcd number */
643 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
644 setup.fields |= MNCC_F_CALLED;
645 gsm48_decode_called(&setup.called,
646 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
647 }
648 /* user-user */
649 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
650 setup.fields |= MNCC_F_USERUSER;
651 gsm48_decode_useruser(&setup.useruser,
652 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
653 }
654 /* ss-version */
655 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
656 setup.fields |= MNCC_F_SSVERSION;
657 gsm48_decode_ssversion(&setup.ssversion,
658 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
659 }
660 /* CLIR suppression */
661 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
662 setup.clir.sup = 1;
663 /* CLIR invocation */
664 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
665 setup.clir.inv = 1;
666 /* cc cap */
667 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
668 setup.fields |= MNCC_F_CCCAP;
669 gsm48_decode_cccap(&setup.cccap,
670 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
671 }
672
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100673 /* MO call leg starting, gather all codec information so far known: */
Oliver Smitha35abb72023-05-23 17:29:57 +0200674 trans_cc_filter_init(trans);
Oliver Smithc7c40c92023-05-23 17:43:40 +0200675 trans_cc_filter_set_ran(trans, trans->msc_a->c.ran->type);
Oliver Smith1c7f1782023-05-23 17:58:26 +0200676 trans_cc_filter_set_bss(trans, trans->msc_a);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100677 if (setup.fields & MNCC_F_BEARER_CAP)
Oliver Smith5375f782023-05-23 13:38:33 +0200678 trans_cc_filter_set_ms_from_bc(trans, &trans->bearer_cap);
Oliver Smithceca8e62023-05-24 11:15:52 +0200679 trans_cc_filter_run(trans);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100680
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100681 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
682 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200683
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200684 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200685
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100686 new_cc_state(trans, GSM_CSTATE_INITIATED);
687
688 /* To complete the MNCC_SETUP_IND, we need to provide an RTP address and port. First instruct the MGW to create
689 * a CN-side RTP conn, and continue with MNCC_SETUP_IND once that is done. Leave trans.cc in GSM_CSTATE_NULL and
690 * note down the msg_type to indicate that we indeed composed an MNCC_SETUP_IND for later. */
691 setup.msg_type = MNCC_SETUP_IND;
692 trans->cc.msg = setup;
693 return msc_a_try_call_assignment(trans);
694 /* continue in gsm48_cc_rx_setup_cn_local_rtp_port_known() */
695}
696
697/* Callback for MNCC_SETUP_IND waiting for the core network RTP port to be established by the MGW (via msc_a) */
698void gsm48_cc_rx_setup_cn_local_rtp_port_known(struct gsm_trans *trans)
699{
700 struct msc_a *msc_a = trans->msc_a;
701 struct gsm_mncc setup = trans->cc.msg;
702 struct osmo_sockaddr_str *rtp_cn_local;
703 struct sdp_msg *sdp;
704 int rc;
705
706 if (trans->cc.state != GSM_CSTATE_INITIATED
707 || setup.msg_type != MNCC_SETUP_IND) {
708 LOG_TRANS(trans, LOGL_ERROR,
709 "Unexpected CC state. Expected GSM_CSTATE_INITIATED and a buffered MNCC_SETUP_IND message,"
710 " found CC state %d and msg_type %s\n",
711 trans->cc.state, get_mncc_name(setup.msg_type));
712 trans->callref = 0;
713 trans_free(trans);
714 return;
715 }
716
717 if (!msc_a) {
718 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
719 trans->callref = 0;
720 trans_free(trans);
721 return;
722 }
723
724 /* 'setup' above has taken the value of trans->cc.msg, we can now clear that. */
725 trans->cc.msg = (struct gsm_mncc){};
726
727 /* Insert the CN side RTP port now available into SDP and compose SDP string */
728 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
729 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
730 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
731 trans_free(trans);
732 return;
733 }
Oliver Smithc63c3a02023-05-24 10:48:07 +0200734 trans->cc.local.rtp = *rtp_cn_local;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100735
Oliver Smithc63c3a02023-05-24 10:48:07 +0200736 sdp = trans->cc.local.audio_codecs.count ? &trans->cc.local : NULL;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100737 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
738 if (rc >= sizeof(setup.sdp)) {
739 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
740 trans_free(trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100741 return;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100742 }
743
Harald Welte27989d42018-06-21 20:39:20 +0200744 /* indicate setup to MNCC */
745 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte27989d42018-06-21 20:39:20 +0200746}
747
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100748static void rx_mncc_sdp(struct gsm_trans *trans, uint32_t mncc_msg_type, const char *sdp,
749 const struct gsm_mncc_bearer_cap *bcap)
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100750{
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100751 struct call_leg *cl = trans->msc_a ? trans->msc_a->cc.call_leg : NULL;
752 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
753
754 if (sdp[0]) {
755 int rc = sdp_msg_from_sdp_str(&trans->cc.remote, sdp);
756 if (rc)
757 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "rx %s: Failed to parse SDP: %d. Trying anyway.\n",
758 get_mncc_name(mncc_msg_type), rc);
759 }
760
761 /* if there is no SDP information or we failed to parse it, try using the Bearer Cap from MNCC, if any. */
762 if (!trans->cc.remote.audio_codecs.count && bcap) {
763 trans->cc.remote = (struct sdp_msg){};
764 trans_cc_set_remote_from_bc(trans, bcap);
765 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s Bearer Cap: remote=%s\n",
766 get_mncc_name(mncc_msg_type), sdp_msg_to_str(&trans->cc.remote));
767 }
768
769 if (!trans->cc.remote.audio_codecs.count)
770 LOG_TRANS(trans, LOGL_INFO,
771 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
772
773 trans_cc_filter_run(trans);
774 if (rtp_cn) {
775 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.remote);
776 rtp_stream_commit(rtp_cn);
777 }
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100778}
779
Harald Welte27989d42018-06-21 20:39:20 +0200780static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
781{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100782 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200783 struct gsm48_hdr *gh;
784 struct gsm_mncc *setup = arg;
785 int rc, trans_id;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100786 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200787
788 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
789
790 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700791 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100792 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200793 "This is not allowed!\n");
794 /* Temporarily out of order */
795 rc = mncc_release_ind(trans->net, trans, trans->callref,
796 GSM48_CAUSE_LOC_PRN_S_LU,
797 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
798 trans->callref = 0;
799 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200800 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200801 return rc;
802 }
803
804 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100805 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200806 if (trans_id < 0) {
807 /* no free transaction ID */
808 rc = mncc_release_ind(trans->net, trans, trans->callref,
809 GSM48_CAUSE_LOC_PRN_S_LU,
810 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
811 trans->callref = 0;
812 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200813 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200814 return rc;
815 }
816 trans->transaction_id = trans_id;
817
818 gh->msg_type = GSM48_MT_CC_SETUP;
819
820 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
821
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100822 /* MT call leg is starting. Gather all codecs information so far known.
823 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
824 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
Oliver Smithe545b9d2023-06-15 14:17:12 +0200825 * We haven't received the MT MS's Bearer Capabilities yet; the Bearer Capabilities handled here are
826 * actually the remote call leg's Bearer Capabilities. */
Oliver Smitha35abb72023-05-23 17:29:57 +0200827 trans_cc_filter_init(trans);
Oliver Smithc7c40c92023-05-23 17:43:40 +0200828 trans_cc_filter_set_ran(trans, trans->msc_a->c.ran->type);
Oliver Smith1c7f1782023-05-23 17:58:26 +0200829 trans_cc_filter_set_bss(trans, trans->msc_a);
Oliver Smith64f39302023-06-15 14:26:37 +0200830 if (setup->fields & MNCC_F_BEARER_CAP)
831 trans->bearer_cap.transfer = setup->bearer_cap.transfer;
Oliver Smith10632132023-05-12 12:14:22 +0200832
833 switch (trans->bearer_cap.transfer) {
834 case GSM48_BCAP_ITCAP_SPEECH:
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100835 /* if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
836 rx_mncc_sdp(trans, setup->msg_type, setup->sdp,
837 (setup->fields & MNCC_F_BEARER_CAP) ? &setup->bearer_cap : NULL);
838 /* rx_mncc_sdp() has called trans_cc_filter_run(trans); */
Oliver Smith10632132023-05-12 12:14:22 +0200839 break;
Manawyrm1ed12ea2023-10-14 17:23:04 +0200840 case GSM48_BCAP_ITCAP_3k1_AUDIO:
841 case GSM48_BCAP_ITCAP_FAX_G3:
Oliver Smith10632132023-05-12 12:14:22 +0200842 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
Oliver Smith412cf922023-07-05 15:47:04 +0200843 if (setup->fields & MNCC_F_BEARER_CAP) {
844 trans->cc.remote = (struct sdp_msg){};
845 trans_cc_set_remote_from_bc(trans, &setup->bearer_cap);
846 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s Bearer Cap: remote=%s\n",
847 get_mncc_name(setup->msg_type), sdp_msg_to_str(&trans->cc.remote));
848 } else {
849 LOG_TRANS(trans, LOGL_INFO,
850 "Got no information of remote Bearer Capability. Trying anyway.\n");
851 sdp_audio_codecs_set_csd(&trans->cc.codecs.ms);
852 }
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100853 trans_cc_filter_run(trans);
Oliver Smith10632132023-05-12 12:14:22 +0200854 break;
855 default:
856 LOG_TRANS(trans, LOGL_ERROR, "Handling of information transfer capability %d not implemented\n",
857 trans->bearer_cap.transfer);
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100858 break;
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100859 }
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100860
Oliver Smith10632132023-05-12 12:14:22 +0200861 /* Compose Bearer Capability information that reflects only the codecs (Speech Versions) / CSD bearer services
862 * remaining after intersecting MS, BSS and remote call leg restrictions. To store in trans for later use, and
863 * to include in the outgoing CC Setup message. */
864 switch (trans->bearer_cap.transfer) {
865 case GSM48_BCAP_ITCAP_SPEECH:
866 bearer_cap = (struct gsm_mncc_bearer_cap){
867 .speech_ver = { -1 },
868 };
869 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.local.audio_codecs);
870 rc = bearer_cap_set_radio(&bearer_cap);
871 if (rc) {
872 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
873 trans_free(trans);
874 msgb_free(msg);
875 return rc;
876 }
877 /* If no resulting codecs remain, error out. We cannot find a codec that matches both call legs. If the MGW were
878 * able to transcode, we could use non-identical codecs on each conn of the MGW endpoint, but we are aiming for
879 * finding a matching codec. */
880 if (bearer_cap.speech_ver[0] == -1) {
881 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
882 get_mncc_name(setup->msg_type),
883 codec_filter_to_str(&trans->cc.codecs, &trans->cc.local, &trans->cc.remote));
884
885 /* incompatible codecs */
886 rc = mncc_release_ind(trans->net, trans, trans->callref,
887 GSM48_CAUSE_LOC_PRN_S_LU,
888 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
889 trans->callref = 0;
890 trans_free(trans);
891 msgb_free(msg);
892 return rc;
893 }
894 break;
Manawyrm1ed12ea2023-10-14 17:23:04 +0200895 case GSM48_BCAP_ITCAP_3k1_AUDIO:
896 case GSM48_BCAP_ITCAP_FAX_G3:
Oliver Smith10632132023-05-12 12:14:22 +0200897 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
898 if (csd_bs_list_to_bearer_cap(&bearer_cap, &trans->cc.local.bearer_services) == 0) {
899 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
900
901 /* incompatible codecs */
902 rc = mncc_release_ind(trans->net, trans, trans->callref,
903 GSM48_CAUSE_LOC_PRN_S_LU,
904 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
905 trans->callref = 0;
906 trans_free(trans);
907 msgb_free(msg);
908 return rc;
909 }
910 break;
Harald Welte27989d42018-06-21 20:39:20 +0200911 }
Oliver Smith10632132023-05-12 12:14:22 +0200912
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100913 /* Create a copy of the bearer capability in the transaction struct, so we can use this information later */
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100914 trans->bearer_cap = bearer_cap;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100915
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100916 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
917
Harald Welte27989d42018-06-21 20:39:20 +0200918 /* facility */
919 if (setup->fields & MNCC_F_FACILITY)
920 gsm48_encode_facility(msg, 0, &setup->facility);
921 /* progress */
922 if (setup->fields & MNCC_F_PROGRESS)
923 gsm48_encode_progress(msg, 0, &setup->progress);
924 /* calling party BCD number */
925 if (setup->fields & MNCC_F_CALLING)
926 gsm48_encode_calling(msg, &setup->calling);
927 /* called party BCD number */
928 if (setup->fields & MNCC_F_CALLED)
929 gsm48_encode_called(msg, &setup->called);
930 /* user-user */
931 if (setup->fields & MNCC_F_USERUSER)
932 gsm48_encode_useruser(msg, 0, &setup->useruser);
933 /* redirecting party BCD number */
934 if (setup->fields & MNCC_F_REDIRECTING)
935 gsm48_encode_redirecting(msg, &setup->redirecting);
936 /* signal */
937 if (setup->fields & MNCC_F_SIGNAL)
938 gsm48_encode_signal(msg, setup->signal);
939
940 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
941
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200942 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200943
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100944 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200945}
946
947static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
948{
949 struct gsm48_hdr *gh = msgb_l3(msg);
950 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
951 struct tlv_parsed tp;
952 struct gsm_mncc call_conf;
953 int rc;
954
955 gsm48_stop_cc_timer(trans);
956 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
957
958 memset(&call_conf, 0, sizeof(struct gsm_mncc));
959 call_conf.callref = trans->callref;
960
961 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
962#if 0
963 /* repeat */
964 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
965 call_conf.repeat = 1;
966 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
967 call_conf.repeat = 2;
968#endif
969 /* bearer capability */
970 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
971 call_conf.fields |= MNCC_F_BEARER_CAP;
972 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
973 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
974
975 /* Create a copy of the bearer capability
976 * in the transaction struct, so we can use
977 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100978 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200979 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +0100980
981 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
982 * Store in codecs filter. */
Oliver Smith5375f782023-05-23 13:38:33 +0200983 trans_cc_filter_set_ms_from_bc(trans, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200984 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100985
Harald Welte27989d42018-06-21 20:39:20 +0200986 /* cause */
987 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
988 call_conf.fields |= MNCC_F_CAUSE;
989 gsm48_decode_cause(&call_conf.cause,
990 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
991 }
992 /* cc cap */
993 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
994 call_conf.fields |= MNCC_F_CCCAP;
995 gsm48_decode_cccap(&call_conf.cccap,
996 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
997 }
998
999 /* IMSI of called subscriber */
1000 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
1001
Harald Welte27989d42018-06-21 20:39:20 +02001002 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001003 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001004
1005 /* don't continue, if there were problems with
1006 * the call assignment. */
1007 if (rc)
1008 return rc;
1009
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001010 /* Directly ack with MNCC_CALL_CONF_IND, not yet containing SDP or RTP IP:port information. */
1011 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1012 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND, &call_conf);
1013}
1014
1015static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1016 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1017 uint32_t payload_msg_type, const struct sdp_msg *sdp);
1018
1019static int gsm48_cc_mt_rtp_port_and_codec_known(struct gsm_trans *trans)
1020{
1021 struct msc_a *msc_a = trans->msc_a;
1022 struct osmo_sockaddr_str *rtp_cn_local;
1023 struct gsm_mncc_rtp;
1024
1025 if (!msc_a) {
1026 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
1027 trans->callref = 0;
1028 trans_free(trans);
1029 return -EINVAL;
1030 }
1031
1032 /* Insert the CN side RTP port now available into SDP */
1033 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
1034 if (!rtp_cn_local) {
1035 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_RTP_CREATE: no RTP set up for the CN side\n");
1036 trans_free(trans);
1037 return -EINVAL;
1038 }
Oliver Smithc63c3a02023-05-24 10:48:07 +02001039 trans->cc.local.rtp = *rtp_cn_local;
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001040
Oliver Smithceca8e62023-05-24 11:15:52 +02001041 trans_cc_filter_run(trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001042
1043 /* If we haven't completed Assignment yet, don't sent MNCC_RTP_CREATE */
1044 if (!sdp_audio_codec_is_set(&trans->cc.codecs.assignment)) {
1045 LOG_TRANS(trans, LOGL_DEBUG, "no codec confirmed by Assignment yet\n");
1046 return 0;
1047 }
1048
1049 return mncc_recv_rtp(msc_a_net(msc_a), trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, 0, 0,
Oliver Smithc63c3a02023-05-24 10:48:07 +02001050 &trans->cc.local);
Harald Welte27989d42018-06-21 20:39:20 +02001051}
1052
1053static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
1054{
1055 struct gsm_mncc *proceeding = arg;
1056 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
1057 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1058 int rc;
1059
1060 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1061
1062 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1063
1064 /* bearer capability */
1065 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Oliver Smith92caa1c2023-08-23 14:26:23 +02001066 /* MNCC should not switch from e.g. CSD to speech */
1067 if (proceeding->bearer_cap.transfer != trans->bearer_cap.transfer) {
1068 LOG_TRANS(trans, LOGL_ERROR, "Unexpected Information Transfer Capability %d from MNCC,"
1069 " transaction has %d\n",
1070 proceeding->bearer_cap.transfer,
1071 trans->bearer_cap.transfer);
1072 return -EINVAL;
1073 }
Harald Welte27989d42018-06-21 20:39:20 +02001074 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
1075 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1076 }
1077 /* facility */
1078 if (proceeding->fields & MNCC_F_FACILITY)
1079 gsm48_encode_facility(msg, 0, &proceeding->facility);
1080 /* progress */
1081 if (proceeding->fields & MNCC_F_PROGRESS)
1082 gsm48_encode_progress(msg, 0, &proceeding->progress);
1083
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001084 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001085 if (rc)
1086 return rc;
1087
1088 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001089 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001090}
1091
1092static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1093{
1094 struct gsm48_hdr *gh = msgb_l3(msg);
1095 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1096 struct tlv_parsed tp;
1097 struct gsm_mncc alerting;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001098 int rc;
Harald Welte27989d42018-06-21 20:39:20 +02001099
1100 gsm48_stop_cc_timer(trans);
1101 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1102
1103 memset(&alerting, 0, sizeof(struct gsm_mncc));
1104 alerting.callref = trans->callref;
1105 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1106 /* facility */
1107 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1108 alerting.fields |= MNCC_F_FACILITY;
1109 gsm48_decode_facility(&alerting.facility,
1110 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1111 }
1112
1113 /* progress */
1114 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1115 alerting.fields |= MNCC_F_PROGRESS;
1116 gsm48_decode_progress(&alerting.progress,
1117 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1118 }
1119 /* ss-version */
1120 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1121 alerting.fields |= MNCC_F_SSVERSION;
1122 gsm48_decode_ssversion(&alerting.ssversion,
1123 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1124 }
1125
1126 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1127
Oliver Smithceca8e62023-05-24 11:15:52 +02001128 trans_cc_filter_run(trans);
Oliver Smithc63c3a02023-05-24 10:48:07 +02001129 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.local);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001130 if (rc >= sizeof(alerting.sdp)) {
1131 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
1132 rc, sizeof(alerting.sdp));
1133 trans_free(trans);
1134 return -EINVAL;
1135 }
1136
Harald Welte27989d42018-06-21 20:39:20 +02001137 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
1138 &alerting);
1139}
1140
1141static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1142{
1143 struct gsm_mncc *alerting = arg;
1144 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1145 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Oliver Smith8e16e8b2023-06-22 11:27:24 +02001146 int rc;
Harald Welte27989d42018-06-21 20:39:20 +02001147
1148 gh->msg_type = GSM48_MT_CC_ALERTING;
1149
1150 /* facility */
1151 if (alerting->fields & MNCC_F_FACILITY)
1152 gsm48_encode_facility(msg, 0, &alerting->facility);
1153 /* progress */
1154 if (alerting->fields & MNCC_F_PROGRESS)
1155 gsm48_encode_progress(msg, 0, &alerting->progress);
1156 /* user-user */
1157 if (alerting->fields & MNCC_F_USERUSER)
1158 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1159
1160 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1161
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01001162 if (alerting->sdp[0])
1163 rx_mncc_sdp(trans, alerting->msg_type, alerting->sdp,
1164 (alerting->fields & MNCC_F_BEARER_CAP) ? &alerting->bearer_cap : NULL);
Oliver Smith8e16e8b2023-06-22 11:27:24 +02001165
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01001166 /* handle the MNCC event */
1167 rc = trans_tx_gsm48(trans, msg);
1168 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02001169}
1170
1171static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1172{
1173 struct gsm_mncc *progress = arg;
1174 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1175 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1176
1177 gh->msg_type = GSM48_MT_CC_PROGRESS;
1178
1179 /* progress */
1180 gsm48_encode_progress(msg, 1, &progress->progress);
1181 /* user-user */
1182 if (progress->fields & MNCC_F_USERUSER)
1183 gsm48_encode_useruser(msg, 0, &progress->useruser);
1184
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001185 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001186}
1187
1188static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1189{
1190 struct gsm_mncc *connect = arg;
1191 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1192 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1193
1194 gh->msg_type = GSM48_MT_CC_CONNECT;
1195
1196 gsm48_stop_cc_timer(trans);
1197 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1198
1199 /* facility */
1200 if (connect->fields & MNCC_F_FACILITY)
1201 gsm48_encode_facility(msg, 0, &connect->facility);
1202 /* progress */
1203 if (connect->fields & MNCC_F_PROGRESS)
1204 gsm48_encode_progress(msg, 0, &connect->progress);
1205 /* connected number */
1206 if (connect->fields & MNCC_F_CONNECTED)
1207 gsm48_encode_connected(msg, &connect->connected);
1208 /* user-user */
1209 if (connect->fields & MNCC_F_USERUSER)
1210 gsm48_encode_useruser(msg, 0, &connect->useruser);
1211
1212 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1213
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01001214 if (connect->sdp[0])
1215 rx_mncc_sdp(trans, connect->msg_type, connect->sdp,
1216 (connect->fields & MNCC_F_BEARER_CAP) ? &connect->bearer_cap : NULL);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001217
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001218 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001219}
1220
1221static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1222{
1223 struct gsm48_hdr *gh = msgb_l3(msg);
1224 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1225 struct tlv_parsed tp;
1226 struct gsm_mncc connect;
1227
1228 gsm48_stop_cc_timer(trans);
1229
1230 memset(&connect, 0, sizeof(struct gsm_mncc));
1231 connect.callref = trans->callref;
1232 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1233 /* use subscriber as connected party number */
1234 connect.fields |= MNCC_F_CONNECTED;
1235 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1236 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1237
1238 /* facility */
1239 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1240 connect.fields |= MNCC_F_FACILITY;
1241 gsm48_decode_facility(&connect.facility,
1242 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1243 }
1244 /* user-user */
1245 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1246 connect.fields |= MNCC_F_USERUSER;
1247 gsm48_decode_useruser(&connect.useruser,
1248 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1249 }
1250 /* ss-version */
1251 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1252 connect.fields |= MNCC_F_SSVERSION;
1253 gsm48_decode_ssversion(&connect.ssversion,
1254 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1255 }
1256
1257 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001258 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001259
Oliver Smithceca8e62023-05-24 11:15:52 +02001260 trans_cc_filter_run(trans);
Oliver Smithc63c3a02023-05-24 10:48:07 +02001261 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.local);
Harald Welte27989d42018-06-21 20:39:20 +02001262 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1263}
1264
1265
1266static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1267{
1268 struct gsm_mncc connect_ack;
1269
1270 gsm48_stop_cc_timer(trans);
1271
1272 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001273 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 +02001274
1275 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1276 connect_ack.callref = trans->callref;
1277
1278 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1279 &connect_ack);
1280}
1281
1282static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1283{
1284 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1285 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1286
1287 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1288
1289 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1290
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001291 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001292}
1293
1294static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1295{
1296 struct gsm48_hdr *gh = msgb_l3(msg);
1297 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1298 struct tlv_parsed tp;
1299 struct gsm_mncc disc;
1300
1301 gsm48_stop_cc_timer(trans);
1302
1303 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1304
1305 memset(&disc, 0, sizeof(struct gsm_mncc));
1306 disc.callref = trans->callref;
1307 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1308 /* cause */
1309 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1310 disc.fields |= MNCC_F_CAUSE;
1311 gsm48_decode_cause(&disc.cause,
1312 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1313 }
1314 /* facility */
1315 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1316 disc.fields |= MNCC_F_FACILITY;
1317 gsm48_decode_facility(&disc.facility,
1318 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1319 }
1320 /* user-user */
1321 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1322 disc.fields |= MNCC_F_USERUSER;
1323 gsm48_decode_useruser(&disc.useruser,
1324 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1325 }
1326 /* ss-version */
1327 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1328 disc.fields |= MNCC_F_SSVERSION;
1329 gsm48_decode_ssversion(&disc.ssversion,
1330 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1331 }
1332
1333 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001334}
1335
1336static struct gsm_mncc_cause default_cause = {
1337 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1338 .coding = 0,
1339 .rec = 0,
1340 .rec_val = 0,
1341 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1342 .diag_len = 0,
1343 .diag = { 0 },
1344};
1345
1346static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1347{
1348 struct gsm_mncc *disc = arg;
1349 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1350 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1351
1352 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1353
1354 gsm48_stop_cc_timer(trans);
1355 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1356
1357 /* cause */
1358 if (disc->fields & MNCC_F_CAUSE)
1359 gsm48_encode_cause(msg, 1, &disc->cause);
1360 else
1361 gsm48_encode_cause(msg, 1, &default_cause);
1362
1363 /* facility */
1364 if (disc->fields & MNCC_F_FACILITY)
1365 gsm48_encode_facility(msg, 0, &disc->facility);
1366 /* progress */
1367 if (disc->fields & MNCC_F_PROGRESS)
1368 gsm48_encode_progress(msg, 0, &disc->progress);
1369 /* user-user */
1370 if (disc->fields & MNCC_F_USERUSER)
1371 gsm48_encode_useruser(msg, 0, &disc->useruser);
1372
1373 /* store disconnect cause for T306 expiry */
1374 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1375
1376 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1377
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001378 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001379}
1380
1381static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1382{
1383 struct gsm48_hdr *gh = msgb_l3(msg);
1384 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1385 struct tlv_parsed tp;
1386 struct gsm_mncc rel;
1387 int rc;
1388
1389 gsm48_stop_cc_timer(trans);
1390
1391 memset(&rel, 0, sizeof(struct gsm_mncc));
1392 rel.callref = trans->callref;
1393 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1394 /* cause */
1395 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1396 rel.fields |= MNCC_F_CAUSE;
1397 gsm48_decode_cause(&rel.cause,
1398 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1399 }
1400 /* facility */
1401 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1402 rel.fields |= MNCC_F_FACILITY;
1403 gsm48_decode_facility(&rel.facility,
1404 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1405 }
1406 /* user-user */
1407 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1408 rel.fields |= MNCC_F_USERUSER;
1409 gsm48_decode_useruser(&rel.useruser,
1410 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1411 }
1412 /* ss-version */
1413 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1414 rel.fields |= MNCC_F_SSVERSION;
1415 gsm48_decode_ssversion(&rel.ssversion,
1416 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1417 }
1418
1419 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1420 /* release collision 5.4.5 */
1421 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1422 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001423 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001424 GSM48_PDISC_CC | (trans->transaction_id << 4),
1425 GSM48_MT_CC_RELEASE_COMPL);
1426 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1427 }
1428
1429 new_cc_state(trans, GSM_CSTATE_NULL);
1430
1431 trans->callref = 0;
1432 trans_free(trans);
1433
1434 return rc;
1435}
1436
1437static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1438{
1439 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001440 struct msgb *msg;
1441 struct gsm48_hdr *gh;
1442
1443 if (!trans->msc_a) {
1444 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1445 return -EINVAL;
1446 }
1447
1448 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1449 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001450
1451 gh->msg_type = GSM48_MT_CC_RELEASE;
1452
1453 gsm48_stop_cc_timer(trans);
1454 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1455
1456 /* cause */
1457 if (rel->fields & MNCC_F_CAUSE)
1458 gsm48_encode_cause(msg, 0, &rel->cause);
1459 /* facility */
1460 if (rel->fields & MNCC_F_FACILITY)
1461 gsm48_encode_facility(msg, 0, &rel->facility);
1462 /* user-user */
1463 if (rel->fields & MNCC_F_USERUSER)
1464 gsm48_encode_useruser(msg, 0, &rel->useruser);
1465
1466 trans->cc.T308_second = 0;
1467 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1468
1469 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1470 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1471
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001472 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001473}
1474
1475static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1476{
1477 struct gsm48_hdr *gh = msgb_l3(msg);
1478 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1479 struct tlv_parsed tp;
1480 struct gsm_mncc rel;
1481 int rc = 0;
1482
1483 gsm48_stop_cc_timer(trans);
1484
1485 memset(&rel, 0, sizeof(struct gsm_mncc));
1486 rel.callref = trans->callref;
1487 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1488 /* cause */
1489 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1490 rel.fields |= MNCC_F_CAUSE;
1491 gsm48_decode_cause(&rel.cause,
1492 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1493 }
1494 /* facility */
1495 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1496 rel.fields |= MNCC_F_FACILITY;
1497 gsm48_decode_facility(&rel.facility,
1498 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1499 }
1500 /* user-user */
1501 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1502 rel.fields |= MNCC_F_USERUSER;
1503 gsm48_decode_useruser(&rel.useruser,
1504 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1505 }
1506 /* ss-version */
1507 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1508 rel.fields |= MNCC_F_SSVERSION;
1509 gsm48_decode_ssversion(&rel.ssversion,
1510 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1511 }
1512
1513 if (trans->callref) {
1514 switch (trans->cc.state) {
1515 case GSM_CSTATE_CALL_PRESENT:
1516 rc = mncc_recvmsg(trans->net, trans,
1517 MNCC_REJ_IND, &rel);
1518 break;
1519 case GSM_CSTATE_RELEASE_REQ:
1520 rc = mncc_recvmsg(trans->net, trans,
1521 MNCC_REL_CNF, &rel);
1522 break;
1523 default:
1524 rc = mncc_recvmsg(trans->net, trans,
1525 MNCC_REL_IND, &rel);
1526 }
1527 }
1528
1529 trans->callref = 0;
1530 trans_free(trans);
1531
1532 return rc;
1533}
1534
1535static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1536{
1537 struct gsm_mncc *rel = arg;
1538 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1539 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1540 int ret;
1541
1542 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1543
1544 trans->callref = 0;
1545
1546 gsm48_stop_cc_timer(trans);
1547
1548 /* cause */
1549 if (rel->fields & MNCC_F_CAUSE)
1550 gsm48_encode_cause(msg, 0, &rel->cause);
1551 /* facility */
1552 if (rel->fields & MNCC_F_FACILITY)
1553 gsm48_encode_facility(msg, 0, &rel->facility);
1554 /* user-user */
1555 if (rel->fields & MNCC_F_USERUSER)
1556 gsm48_encode_useruser(msg, 0, &rel->useruser);
1557
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001558 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001559
1560 trans_free(trans);
1561
1562 return ret;
1563}
1564
1565static int gsm48_cc_rx_facility(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 fac;
1571
1572 memset(&fac, 0, sizeof(struct gsm_mncc));
1573 fac.callref = trans->callref;
1574 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1575 /* facility */
1576 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1577 fac.fields |= MNCC_F_FACILITY;
1578 gsm48_decode_facility(&fac.facility,
1579 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1580 }
1581 /* ss-version */
1582 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1583 fac.fields |= MNCC_F_SSVERSION;
1584 gsm48_decode_ssversion(&fac.ssversion,
1585 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1586 }
1587
1588 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1589}
1590
1591static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1592{
1593 struct gsm_mncc *fac = arg;
1594 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1595 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1596
1597 gh->msg_type = GSM48_MT_CC_FACILITY;
1598
1599 /* facility */
1600 gsm48_encode_facility(msg, 1, &fac->facility);
1601
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001602 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001603}
1604
1605static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1606{
1607 struct gsm_mncc hold;
1608
1609 memset(&hold, 0, sizeof(struct gsm_mncc));
1610 hold.callref = trans->callref;
1611 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1612}
1613
1614static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1615{
1616 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1618
1619 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1620
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001621 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001622}
1623
1624static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1625{
1626 struct gsm_mncc *hold_rej = arg;
1627 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1628 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1629
1630 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1631
1632 /* cause */
1633 if (hold_rej->fields & MNCC_F_CAUSE)
1634 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1635 else
1636 gsm48_encode_cause(msg, 1, &default_cause);
1637
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001638 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001639}
1640
1641static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1642{
1643 struct gsm_mncc retrieve;
1644
1645 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1646 retrieve.callref = trans->callref;
1647 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1648 &retrieve);
1649}
1650
1651static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1652{
1653 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1655
1656 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1657
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001658 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001659}
1660
1661static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1662{
1663 struct gsm_mncc *retrieve_rej = arg;
1664 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1665 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1666
1667 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1668
1669 /* cause */
1670 if (retrieve_rej->fields & MNCC_F_CAUSE)
1671 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1672 else
1673 gsm48_encode_cause(msg, 1, &default_cause);
1674
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001675 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001676}
1677
1678static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1679{
1680 struct gsm48_hdr *gh = msgb_l3(msg);
1681 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1682 struct tlv_parsed tp;
1683 struct gsm_mncc dtmf;
1684
1685 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1686 dtmf.callref = trans->callref;
1687 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1688 /* keypad facility */
1689 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1690 dtmf.fields |= MNCC_F_KEYPAD;
1691 gsm48_decode_keypad(&dtmf.keypad,
1692 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1693 }
1694
1695 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1696}
1697
1698static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1699{
1700 struct gsm_mncc *dtmf = arg;
1701 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1702 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1703
1704 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1705
1706 /* keypad */
1707 if (dtmf->fields & MNCC_F_KEYPAD)
1708 gsm48_encode_keypad(msg, dtmf->keypad);
1709
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001710 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001711}
1712
1713static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1714{
1715 struct gsm_mncc *dtmf = arg;
1716 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1717 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1718
1719 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1720
1721 /* cause */
1722 if (dtmf->fields & MNCC_F_CAUSE)
1723 gsm48_encode_cause(msg, 1, &dtmf->cause);
1724 else
1725 gsm48_encode_cause(msg, 1, &default_cause);
1726
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001727 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001728}
1729
1730static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1731{
1732 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1733 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1734
1735 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1736
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001737 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001738}
1739
1740static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1741{
1742 struct gsm_mncc dtmf;
1743
1744 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1745 dtmf.callref = trans->callref;
1746
1747 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1748}
1749
1750static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1751{
1752 struct gsm48_hdr *gh = msgb_l3(msg);
1753 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1754 struct tlv_parsed tp;
1755 struct gsm_mncc modify;
1756
1757 memset(&modify, 0, sizeof(struct gsm_mncc));
1758 modify.callref = trans->callref;
1759 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1760 /* bearer capability */
1761 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1762 modify.fields |= MNCC_F_BEARER_CAP;
1763 gsm48_decode_bearer_cap(&modify.bearer_cap,
1764 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1765
1766 /* Create a copy of the bearer capability
1767 * in the transaction struct, so we can use
1768 * this information later */
1769 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1770 sizeof(trans->bearer_cap));
1771 }
1772
1773 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1774
1775 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1776}
1777
1778static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1779{
1780 struct gsm_mncc *modify = arg;
1781 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1782 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1783
1784 gh->msg_type = GSM48_MT_CC_MODIFY;
1785
1786 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1787
1788 /* bearer capability */
1789 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1790 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1791
1792 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1793
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001794 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001795}
1796
1797static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1798{
1799 struct gsm48_hdr *gh = msgb_l3(msg);
1800 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1801 struct tlv_parsed tp;
1802 struct gsm_mncc modify;
1803
1804 gsm48_stop_cc_timer(trans);
1805
1806 memset(&modify, 0, sizeof(struct gsm_mncc));
1807 modify.callref = trans->callref;
1808 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1809 /* bearer capability */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1811 modify.fields |= MNCC_F_BEARER_CAP;
1812 gsm48_decode_bearer_cap(&modify.bearer_cap,
1813 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1814
1815 /* Create a copy of the bearer capability
1816 * in the transaction struct, so we can use
1817 * this information later */
1818 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1819 sizeof(trans->bearer_cap));
1820 }
1821
1822 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1823
1824 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1825}
1826
1827static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1828{
1829 struct gsm_mncc *modify = arg;
1830 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1831 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1832
1833 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1834
1835 /* bearer capability */
1836 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1837 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1838
1839 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1840
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001841 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001842}
1843
1844static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1845{
1846 struct gsm48_hdr *gh = msgb_l3(msg);
1847 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1848 struct tlv_parsed tp;
1849 struct gsm_mncc modify;
1850
1851 gsm48_stop_cc_timer(trans);
1852
1853 memset(&modify, 0, sizeof(struct gsm_mncc));
1854 modify.callref = trans->callref;
1855 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1856 /* bearer capability */
1857 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1858 modify.fields |= GSM48_IE_BEARER_CAP;
1859 gsm48_decode_bearer_cap(&modify.bearer_cap,
1860 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1861
1862 /* Create a copy of the bearer capability
1863 * in the transaction struct, so we can use
1864 * this information later */
1865 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1866 sizeof(trans->bearer_cap));
1867 }
1868 /* cause */
1869 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1870 modify.fields |= MNCC_F_CAUSE;
1871 gsm48_decode_cause(&modify.cause,
1872 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1873 }
1874
1875 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1876
1877 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1878}
1879
1880static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1881{
1882 struct gsm_mncc *modify = arg;
1883 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1884 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1885
1886 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1887
1888 /* bearer capability */
1889 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1890 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1891 /* cause */
1892 gsm48_encode_cause(msg, 1, &modify->cause);
1893
1894 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1895
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001896 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001897}
1898
1899static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1900{
1901 struct gsm_mncc *notify = arg;
1902 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1903 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1904
1905 gh->msg_type = GSM48_MT_CC_NOTIFY;
1906
1907 /* notify */
1908 gsm48_encode_notify(msg, notify->notify);
1909
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001910 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001911}
1912
1913static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1914{
1915 struct gsm48_hdr *gh = msgb_l3(msg);
1916 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1917// struct tlv_parsed tp;
1918 struct gsm_mncc notify;
1919
1920 memset(&notify, 0, sizeof(struct gsm_mncc));
1921 notify.callref = trans->callref;
1922// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1923 if (payload_len >= 1)
1924 gsm48_decode_notify(&notify.notify, gh->data);
1925
1926 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1927}
1928
1929static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1930{
1931 struct gsm_mncc *user = arg;
1932 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1933 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1934
1935 gh->msg_type = GSM48_MT_CC_USER_INFO;
1936
1937 /* user-user */
1938 if (user->fields & MNCC_F_USERUSER)
1939 gsm48_encode_useruser(msg, 1, &user->useruser);
1940 /* more data */
1941 if (user->more)
1942 gsm48_encode_more(msg);
1943
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001944 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001945}
1946
1947static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1948{
1949 struct gsm48_hdr *gh = msgb_l3(msg);
1950 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1951 struct tlv_parsed tp;
1952 struct gsm_mncc user;
1953
1954 memset(&user, 0, sizeof(struct gsm_mncc));
1955 user.callref = trans->callref;
1956 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1957 /* user-user */
1958 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1959 user.fields |= MNCC_F_USERUSER;
1960 gsm48_decode_useruser(&user.useruser,
1961 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1962 }
1963 /* more data */
1964 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1965 user.more = 1;
1966
1967 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1968}
1969
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001970static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1971 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001972 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001973{
1974 uint8_t data[sizeof(struct gsm_mncc)];
1975 struct gsm_mncc_rtp *rtp;
1976
1977 memset(&data, 0, sizeof(data));
1978 rtp = (struct gsm_mncc_rtp *) &data[0];
1979
1980 rtp->callref = callref;
1981 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001982 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001983 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1984 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001985 }
Harald Welte27989d42018-06-21 20:39:20 +02001986 rtp->payload_type = payload_type;
1987 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001988 if (sdp)
1989 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001990 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001991}
1992
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001993static 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 +02001994{
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001995 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001996}
1997
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001998static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001999{
2000 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02002001
2002 /* Find callref */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +02002003 trans = trans_find_by_callref(net, TRANS_CC, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002004 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002005 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002006 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02002007 return -EIO;
2008 }
2009 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002010 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002011 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002012 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02002013 return 0;
2014 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002015 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002016
Harald Welte27989d42018-06-21 20:39:20 +02002017 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002018 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002019}
2020
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002021int cc_on_cn_local_rtp_port_known(struct gsm_trans *cc_trans)
2022{
2023 /* Depending on MO or MT call, dispatch the event differently */
2024 switch (cc_trans->cc.state) {
2025 case GSM_CSTATE_INITIATED:
2026 if (cc_trans->cc.msg.msg_type != MNCC_SETUP_IND) {
2027 LOG_TRANS(cc_trans, LOGL_ERROR, "Assuming MO call, expected MNCC_SETUP_IND to be prepared\n");
2028 return -EINVAL;
2029 }
2030 /* This is the MO call leg, waiting for a CN RTP be able to send initial MNCC_SETUP_IND. */
2031 gsm48_cc_rx_setup_cn_local_rtp_port_known(cc_trans);
2032 return 0;
2033
2034 case GSM_CSTATE_MO_TERM_CALL_CONF:
2035 /* This is the MT call leg, waiting for a CN RTP to be able to send MNCC_CALL_CONF_IND. */
2036 return gsm48_cc_mt_rtp_port_and_codec_known(cc_trans);
2037
2038 default:
2039 LOG_TRANS(cc_trans, LOGL_ERROR, "CN RTP address available, but in unexpected state %d\n",
2040 cc_trans->cc.state);
2041 return -EINVAL;
2042 }
2043}
2044
2045int cc_on_assignment_done(struct gsm_trans *trans)
2046{
2047 struct msc_a *msc_a = trans->msc_a;
2048
2049 switch (trans->cc.state) {
2050 case GSM_CSTATE_INITIATED:
2051 case GSM_CSTATE_MO_CALL_PROC:
2052 /* MO call */
2053 break;
2054
2055 case GSM_CSTATE_CALL_RECEIVED:
2056 case GSM_CSTATE_MO_TERM_CALL_CONF:
2057 /* MT call */
2058 break;
2059
2060 case GSM_CSTATE_ACTIVE:
2061 /* already active. MNCC finished before Abis completed the Assignment. */
2062 break;
2063
2064 default:
2065 LOG_TRANS(trans, LOGL_ERROR, "Assignment done in unexpected CC state: %d\n", trans->cc.state);
2066 return -EINVAL;
2067 }
2068
2069 if (!call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN)) {
2070 LOG_TRANS(trans, LOGL_DEBUG,
2071 "Assignment complete, but still waiting for the CRCX OK on the CN side RTP\n");
2072 return 0;
2073 }
2074 return gsm48_tch_rtp_create(trans);
2075}
2076
Harald Welte27989d42018-06-21 20:39:20 +02002077/* Trigger TCH_RTP_CREATE acknowledgement */
2078int gsm48_tch_rtp_create(struct gsm_trans *trans)
2079{
2080 /* This function is called as soon as the port, on which the
2081 * mgcp-gw expects the incoming RTP stream from the remote
2082 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002083 struct msc_a *msc_a = trans->msc_a;
2084 struct gsm_network *net = msc_a_net(msc_a);
2085 struct call_leg *cl = msc_a->cc.call_leg;
2086 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002087 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002088 int mncc_payload_msg_type;
2089 struct sdp_audio_codec *codec;
Neels Hofmeyra001a702022-10-31 17:57:30 +01002090 const struct codec_mapping *m;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002091 struct sdp_audio_codecs *codecs;
Harald Welte27989d42018-06-21 20:39:20 +02002092
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002093 if (!rtp_cn) {
2094 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
2095 return -EINVAL;
2096 }
2097
Oliver Smithceca8e62023-05-24 11:15:52 +02002098 trans_cc_filter_run(trans);
Oliver Smithc63c3a02023-05-24 10:48:07 +02002099 codecs = &trans->cc.local.audio_codecs;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002100 if (!codecs->count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002101 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002102 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002103 return -EINVAL;
2104 }
2105
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002106 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
2107 codec = &codecs->codec[0];
2108 m = codec_mapping_by_subtype_name(codec->subtype_name);
2109 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02002110
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002111 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
2112 if (!rtp_cn_local) {
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002113 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port to CN set up\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002114 return -EINVAL;
2115 }
2116
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002117 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002118 codec->payload_type, mncc_payload_msg_type, &trans->cc.local);
Harald Welte27989d42018-06-21 20:39:20 +02002119}
2120
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002121static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002122{
2123 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002124 struct call_leg *cl;
2125 struct rtp_stream *rtps;
Philipp Maier8ad3dac2018-08-07 13:00:14 +02002126
Harald Welte27989d42018-06-21 20:39:20 +02002127 /* Find callref */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +02002128 trans = trans_find_by_callref(net, TRANS_CC, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002129 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002130 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002131 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02002132 return -EIO;
2133 }
2134 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002135 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002136 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002137 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002138 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02002139 }
2140
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002141 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002142
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002143 rx_mncc_sdp(trans, rtp->msg_type, rtp->sdp, NULL);
2144
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002145 cl = trans->msc_a->cc.call_leg;
2146 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002147 if (rtps && !osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002148 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
2149 struct osmo_sockaddr_str rtp_addr;
2150 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
2151 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
2152 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2153 return -EINVAL;
2154 }
2155 rtp_stream_set_remote_addr(rtps, &rtp_addr);
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002156 rtp_stream_commit(rtps);
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002157 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002158 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02002159}
2160
2161static struct downstate {
2162 uint32_t states;
2163 int type;
2164 int (*rout) (struct gsm_trans *trans, void *arg);
2165} downstatelist[] = {
2166 /* mobile originating call establishment */
2167 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2168 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2169 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2170 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2171 {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 */
2172 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2173 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2174 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2175 /* mobile terminating call establishment */
2176 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2177 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2178 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2179 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2180 /* signalling during call */
2181 {SBIT(GSM_CSTATE_ACTIVE),
2182 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2183 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2184 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2185 {ALL_STATES,
2186 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2187 {ALL_STATES,
2188 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2189 {ALL_STATES,
2190 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2191 {SBIT(GSM_CSTATE_ACTIVE),
2192 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2193 {SBIT(GSM_CSTATE_ACTIVE),
2194 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2195 {SBIT(GSM_CSTATE_ACTIVE),
2196 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2197 {SBIT(GSM_CSTATE_ACTIVE),
2198 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2199 {SBIT(GSM_CSTATE_ACTIVE),
2200 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2201 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2202 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2203 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2204 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2205 {SBIT(GSM_CSTATE_ACTIVE),
2206 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2207 /* clearing */
2208 {SBIT(GSM_CSTATE_INITIATED),
2209 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2210 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2211 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2212 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2213 MNCC_REL_REQ, gsm48_cc_tx_release},
2214};
2215
2216#define DOWNSLLEN \
2217 (sizeof(downstatelist) / sizeof(struct downstate))
2218
2219
Philipp Maiercd64af72019-08-01 09:46:40 +02002220static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002221{
2222 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002223 struct msc_a *msc_a = NULL;
2224 struct gsm_trans *trans = NULL;
2225 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002226
Harald Welte27989d42018-06-21 20:39:20 +02002227 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002228 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002229 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002230 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002231 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002232 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002233 return rc;
2234 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002235 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002236 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002237 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002238 case MNCC_RTP_FREE:
2239 /* unused right now */
2240 return -EIO;
2241
2242 case MNCC_FRAME_DROP:
2243 case MNCC_FRAME_RECV:
2244 case GSM_TCHF_FRAME:
2245 case GSM_TCHF_FRAME_EFR:
2246 case GSM_TCHH_FRAME:
2247 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002248 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002249 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002250 return -ENOTSUP;
2251 }
2252
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002253 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002254
2255 /* Find callref */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +02002256 trans = trans_find_by_callref(net, TRANS_CC, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002257
2258 /* Callref unknown */
2259 if (!trans) {
2260 struct vlr_subscr *vsub;
2261
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002262 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002263 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002264 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002265 /* Invalid call reference */
2266 return mncc_release_ind(net, NULL, data->callref,
2267 GSM48_CAUSE_LOC_PRN_S_LU,
2268 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2269 }
2270 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002271 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002272 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002273 /* Invalid number */
2274 return mncc_release_ind(net, NULL, data->callref,
2275 GSM48_CAUSE_LOC_PRN_S_LU,
2276 GSM48_CC_CAUSE_INV_NR_FORMAT);
2277 }
2278 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002279 if (data->called.number[0]) {
2280 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2281 if (!vsub)
2282 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002283 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002284 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002285 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002286 if (!vsub)
2287 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002288 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002289 }
2290 if (!vsub)
2291 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002292 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002293 /* update the subscriber we deal with */
2294 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2295
Harald Welte27989d42018-06-21 20:39:20 +02002296 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002297 if (!vsub->lu_complete) {
2298 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002299 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002300 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002301 /* Temporarily out of order */
2302 return mncc_release_ind(net, NULL, data->callref,
2303 GSM48_CAUSE_LOC_PRN_S_LU,
2304 GSM48_CC_CAUSE_DEST_OOO);
2305 }
Keith Whyte991bb422019-08-08 15:43:40 +02002306
2307 /* Find valid conn */
2308 msc_a = msc_a_for_vsub(vsub, true);
2309
2310 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2311 if (!net->call_waiting && msc_a) {
2312 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2313 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2314 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2315 "rx '%s' for subscriber %s with trans state (%s)"
2316 " rejecting with USER_BUSY\n",
2317 get_mncc_name(msg->msg_type), data->called.number,
2318 gsm48_cc_state_name(existing_cc_trans->cc.state));
2319 return mncc_release_ind(net, NULL, data->callref,
2320 GSM48_CAUSE_LOC_PRN_S_LU,
2321 GSM48_CC_CAUSE_USER_BUSY);
2322 }
2323 }
2324
Harald Welte27989d42018-06-21 20:39:20 +02002325 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002326 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002327 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002328 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002329 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002330 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002331 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002332 mncc_release_ind(net, NULL, data->callref,
2333 GSM48_CAUSE_LOC_PRN_S_LU,
2334 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2335 return -ENOMEM;
2336 }
2337
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002338 /* Remember remote SDP, if any */
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002339 rx_mncc_sdp(trans, data->msg_type, data->sdp,
2340 (data->fields & MNCC_F_BEARER_CAP) ? &data->bearer_cap : NULL);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002341
Harald Welte27989d42018-06-21 20:39:20 +02002342 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002343 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002344 /* This condition will return before the common logging of the received MNCC message below, so
2345 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002346 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002347
Harald Welte27989d42018-06-21 20:39:20 +02002348 /* store setup information until paging succeeds */
2349 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2350
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002351 /* Request a channel. If Paging already started, paging_request_start() will append the new
2352 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002353 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2354 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002355 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002356 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002357 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002358 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002359 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002360 return 0;
2361 }
2362
2363 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002364 trans->msc_a = msc_a;
2365 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002366 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002367 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002368 } else {
2369 /* update the subscriber we deal with */
2370 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2371 }
2372
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002373 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002374
Mychaela N. Falconia9b7de742023-06-26 05:30:29 +00002375 /*
2376 * The step of gsm48_start_guard_timer() needs to be done for
2377 * major state-impacting MNCC messages, but not for those
2378 * that are a mere pass-through to CC messages to MS.
2379 */
2380 switch (msg->msg_type) {
2381 case MNCC_PROGRESS_REQ:
2382 case MNCC_NOTIFY_REQ:
2383 case MNCC_FACILITY_REQ:
2384 case MNCC_START_DTMF_RSP:
2385 case MNCC_START_DTMF_REJ:
2386 case MNCC_STOP_DTMF_RSP:
2387 case MNCC_HOLD_CNF:
2388 case MNCC_HOLD_REJ:
2389 case MNCC_RETRIEVE_CNF:
2390 case MNCC_RETRIEVE_REJ:
2391 case MNCC_USERINFO_REQ:
2392 break;
2393 default:
2394 gsm48_start_guard_timer(trans);
2395 }
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002396 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002397
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002398 if (trans->msc_a)
2399 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002400
2401 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002402 if (!msc_a) {
2403 struct gsm_mncc rel = {
2404 .callref = data->callref,
2405 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002406 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002407 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2408 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002409 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002410 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2411 else
2412 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2413 trans->callref = 0;
2414 trans_free(trans);
2415 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002416 }
2417
2418 /* Find function for current state and message */
2419 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002420 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002421 && ((1 << trans->cc.state) & downstatelist[i].states))
2422 break;
2423 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002424 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002425 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002426 return 0;
2427 }
2428
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002429 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002430
2431 return rc;
2432}
2433
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002434struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2435{
2436 uint32_t callref;
2437
2438 switch (msg->msg_type) {
2439 case MNCC_BRIDGE:
2440 callref = msg->bridge.callref[0];
2441 break;
2442 case MNCC_RTP_CREATE:
2443 case MNCC_RTP_CONNECT:
2444 callref = msg->rtp.callref;
2445 break;
2446
2447 case MNCC_RTP_FREE:
2448 case MNCC_FRAME_DROP:
2449 case MNCC_FRAME_RECV:
2450 case GSM_TCHF_FRAME:
2451 case GSM_TCHF_FRAME_EFR:
2452 case GSM_TCHH_FRAME:
2453 case GSM_TCH_FRAME_AMR:
2454 return NULL;
2455
2456 default:
2457 callref = msg->signal.callref;
2458 break;
2459 }
2460
2461 return mncc_call_find_by_callref(callref);
2462}
2463
2464/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002465int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002466{
2467 const union mncc_msg *msg = arg;
2468 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002469
2470 if (msg->msg_type == MNCC_SETUP_REQ) {
2471 /* Incoming call to forward for inter-MSC Handover? */
2472 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2473 if (mncc_call)
2474 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2475 "Incoming call matches pending inter-MSC Handover Number\n");
2476 }
2477 if (!mncc_call) {
2478 /* Find already active MNCC FSM for this callref.
2479 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2480 * MNCC<->GSM-CC call handling. */
2481 mncc_call = mncc_find_by_callref_from_msg(msg);
2482 }
2483 if (mncc_call) {
2484 mncc_call_rx(mncc_call, msg);
2485 return 0;
2486 }
2487
2488 /* None of the above? Then it must be a normal GSM CC call related message. */
2489 return mncc_tx_to_gsm_cc(net, msg);
2490}
Harald Welte27989d42018-06-21 20:39:20 +02002491
2492static struct datastate {
2493 uint32_t states;
2494 int type;
2495 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2496} datastatelist[] = {
2497 /* mobile originating call establishment */
2498 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2499 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2500 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2501 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2502 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2503 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2504 /* mobile terminating call establishment */
2505 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2506 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2507 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2508 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2509 {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 */
2510 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2511 /* signalling during call */
2512 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2513 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2514 {SBIT(GSM_CSTATE_ACTIVE),
2515 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2516 {ALL_STATES,
2517 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2518 {ALL_STATES,
2519 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2520 {ALL_STATES,
2521 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2522 {SBIT(GSM_CSTATE_ACTIVE),
2523 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2524 {SBIT(GSM_CSTATE_ACTIVE),
2525 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2526 {SBIT(GSM_CSTATE_ACTIVE),
2527 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2528 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2529 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2530 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2531 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2532 {SBIT(GSM_CSTATE_ACTIVE),
2533 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2534 /* clearing */
2535 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2536 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2537 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2538 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2539 {ALL_STATES, /* 5.4.3.4 */
2540 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2541};
2542
2543#define DATASLLEN \
2544 (sizeof(datastatelist) / sizeof(struct datastate))
2545
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002546int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002547{
2548 struct gsm48_hdr *gh = msgb_l3(msg);
2549 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2550 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2551 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002552 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2553 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002554 int i, rc = 0;
2555
2556 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002557 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002558 return -EINVAL;
2559 }
2560
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002561 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002562 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002563 return -EINVAL;
2564 }
2565
2566 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002567 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002568
Harald Welte27989d42018-06-21 20:39:20 +02002569 /* Create transaction */
2570 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002571 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002572 trans = trans_alloc(net, vsub,
2573 TRANS_CC,
2574 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002575 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002576 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002577 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002578 GSM48_PDISC_CC | (transaction_id << 4),
2579 GSM48_MT_CC_RELEASE_COMPL);
2580 return -ENOMEM;
2581 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002582 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2583 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2584 trans_free(trans);
2585 return -EINVAL;
2586 }
2587
Harald Welte27989d42018-06-21 20:39:20 +02002588 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002589 msc_a_get(msc_a, MSC_A_USE_CC);
2590 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002591 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002592
2593 /* An earlier CM Service Request for this CC message now has concluded */
2594 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2595 LOG_MSC_A(msc_a, LOGL_ERROR,
2596 "Creating new CC transaction without prior CM Service Request\n");
2597 else
2598 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002599 }
2600
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002601 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2602 gsm48_cc_state_name(trans->cc.state));
2603
Harald Welte27989d42018-06-21 20:39:20 +02002604 /* find function for current state and message */
2605 for (i = 0; i < DATASLLEN; i++)
2606 if ((msg_type == datastatelist[i].type)
2607 && ((1 << trans->cc.state) & datastatelist[i].states))
2608 break;
2609 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002610 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002611
2612 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2613 * transaction right away. */
2614 if (trans->cc.state == GSM_CSTATE_NULL) {
2615 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2616 " -- disarding new CC transaction right away\n");
2617 trans_free(trans);
2618 }
Harald Welte27989d42018-06-21 20:39:20 +02002619 return 0;
2620 }
2621
2622 assert(trans->vsub);
2623
2624 rc = datastatelist[i].rout(trans, msg);
2625
Harald Welte27989d42018-06-21 20:39:20 +02002626 return rc;
2627}