blob: f6ec81b41b0a3cc2865a36e66fbc4e34fc85372d [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
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100273 if (sdp && sdp[0]) {
274 int rc = sdp_msg_from_sdp_str(&sdp_msg, sdp);
275 if (rc != 0) {
276 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_ERROR, file, line, "%s %s: invalid SDP message (trying anyway)\n",
277 rx_tx,
278 get_mncc_name(mncc->msg_type));
279 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "erratic SDP: %s\n",
280 osmo_quote_cstr_c(OTC_SELECT, sdp, -1));
281 return;
282 }
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200283 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
284 rx_tx,
285 get_mncc_name(mncc->msg_type),
286 sdp_msg_to_str(&sdp_msg));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100287 return;
288 }
289
290 if (osmo_sockaddr_is_any(&addr) == 0) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200291 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
292 rx_tx,
293 get_mncc_name(mncc->msg_type),
294 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100295 return;
296 }
297
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200298 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 +0100299}
300
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200301#define mncc_recvmsg(ARGS...) _mncc_recvmsg(__FILE__, __LINE__, ##ARGS)
302static int _mncc_recvmsg(const char *file, int line,
303 struct gsm_network *net, struct gsm_trans *trans, int msg_type, struct gsm_mncc *mncc)
Harald Welte27989d42018-06-21 20:39:20 +0200304{
305 struct msgb *msg;
306 unsigned char *data;
307
Harald Welte27989d42018-06-21 20:39:20 +0200308 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100309 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200310
311 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
312 if (!msg)
313 return -ENOMEM;
314
315 data = msgb_put(msg, sizeof(struct gsm_mncc));
316 memcpy(data, mncc, sizeof(struct gsm_mncc));
317
318 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200319 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
320 if (trans)
321 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200322
323 return 0;
324}
325
326int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
327 uint32_t callref, int location, int value)
328{
329 struct gsm_mncc rel;
330
331 memset(&rel, 0, sizeof(rel));
332 rel.callref = callref;
333 mncc_set_cause(&rel, location, value);
334 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
335 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
336 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
337}
338
339/* Call Control Specific transaction release.
340 * gets called by trans_free, DO NOT CALL YOURSELF! */
341void _gsm48_cc_trans_free(struct gsm_trans *trans)
342{
343 gsm48_stop_cc_timer(trans);
344
Harald Welte27989d42018-06-21 20:39:20 +0200345 /* send release to L4, if callref still exists */
346 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100347 /* Send MNCC REL.ind (cause='Resource unavailable') */
348 if (trans->cc.mncc_initiated) {
349 mncc_release_ind(trans->net, trans, trans->callref,
350 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100351 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
352 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100353 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
354 }
355
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100356 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
357 * CC Release to the MS if it gets freed here. Hack it to do so. */
358 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
359 struct gsm_mncc rel = {};
360 rel.callref = trans->callref;
361 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
362 gsm48_cc_tx_release(trans, &rel);
363 }
Harald Welte27989d42018-06-21 20:39:20 +0200364 /* This is a final freeing of the transaction. The MNCC release may have triggered the
365 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
366 gsm48_stop_cc_timer(trans);
367 }
368 if (trans->cc.state != GSM_CSTATE_NULL)
369 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200370
371 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100372
373 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
374 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200375}
376
Harald Welte27989d42018-06-21 20:39:20 +0200377/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100378static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200379{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100380 if (trans->msc_a) {
381 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
382 "Handle paging error: transaction already associated with subscriber,"
383 " apparently it was already handled. Skip.\n");
384 return;
Harald Welte27989d42018-06-21 20:39:20 +0200385 }
386
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100387 if (msc_a) {
388 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
389 /* Assign conn */
390 msc_a_get(msc_a, MSC_A_USE_CC);
391 trans->msc_a = msc_a;
392 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200393
394 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300395 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200396 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300397 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
398 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
399 &trans->cc.msg.gcr[0],
400 sizeof(trans->cc.msg.gcr));
401 if (rc < 0)
402 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
403 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200404 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200405 }
406
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100407 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
408 /* send SETUP request to called party */
409 gsm48_cc_tx_setup(trans, &trans->cc.msg);
410 } else {
411 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
412 /* Temporarily out of order */
413 mncc_release_ind(trans->net, trans,
414 trans->callref,
415 GSM48_CAUSE_LOC_PRN_S_LU,
416 GSM48_CC_CAUSE_DEST_OOO);
417 trans->callref = 0;
418 trans->paging_request = NULL;
419 trans_free(trans);
420 }
Harald Welte27989d42018-06-21 20:39:20 +0200421}
422
423/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100424static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200425{
Andreas Eversberg7e4b0322023-04-23 11:43:13 +0200426 struct gsm_trans *trans1 = trans_find_by_callref(net, TRANS_CC, bridge->callref[0]);
427 struct gsm_trans *trans2 = trans_find_by_callref(net, TRANS_CC, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100428 struct call_leg *cl1;
429 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200430
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100431 if (!trans1 || !trans2) {
432 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200433 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100434 }
Harald Welte27989d42018-06-21 20:39:20 +0200435
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100436 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100437 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
438 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 +0200439 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100440 }
441
442 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
443 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200444
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100445 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
446 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
447 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
448 * If we can't, then we must give up. */
449 cl1 = trans1->msc_a->cc.call_leg;
450 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200451
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200452 return call_leg_local_bridge(cl1, trans1->call_id, trans1, cl2, trans2->call_id, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200453}
454
455static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
456{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100457 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200458 return gsm48_cc_tx_status(trans, msg);
459}
460
Harald Welte27989d42018-06-21 20:39:20 +0200461static void gsm48_cc_timeout(void *arg)
462{
463 struct gsm_trans *trans = arg;
464 int disconnect = 0, release = 0;
465 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
466 int mo_location = GSM48_CAUSE_LOC_USER;
467 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
468 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
469 struct gsm_mncc mo_rel, l4_rel;
470
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200471 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
472
Harald Welte27989d42018-06-21 20:39:20 +0200473 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
474 mo_rel.callref = trans->callref;
475 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
476 l4_rel.callref = trans->callref;
477
478 switch(trans->cc.Tcurrent) {
479 case 0x303:
480 release = 1;
481 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
482 break;
483 case 0x310:
484 disconnect = 1;
485 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
486 break;
487 case 0x313:
488 disconnect = 1;
489 /* unknown, did not find it in the specs */
490 break;
491 case 0x301:
492 disconnect = 1;
493 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
494 break;
495 case 0x308:
496 if (!trans->cc.T308_second) {
497 /* restart T308 a second time */
498 gsm48_cc_tx_release(trans, &trans->cc.msg);
499 trans->cc.T308_second = 1;
500 break; /* stay in release state */
501 }
502 trans_free(trans);
503 return;
504 case 0x306:
505 release = 1;
506 mo_cause = trans->cc.msg.cause.value;
507 mo_location = trans->cc.msg.cause.location;
508 break;
509 case 0x323:
510 disconnect = 1;
511 break;
512 default:
513 release = 1;
514 }
515
516 if (release && trans->callref) {
517 /* process release towards layer 4 */
518 mncc_release_ind(trans->net, trans, trans->callref,
519 l4_location, l4_cause);
520 trans->callref = 0;
521 }
522
523 if (disconnect && trans->callref) {
524 /* process disconnect towards layer 4 */
525 mncc_set_cause(&l4_rel, l4_location, l4_cause);
526 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
527 }
528
529 /* process disconnect towards mobile station */
530 if (disconnect || release) {
531 mncc_set_cause(&mo_rel, mo_location, mo_cause);
532 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
533 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
534 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
535 mo_rel.cause.diag_len = 3;
536
537 if (disconnect)
538 gsm48_cc_tx_disconnect(trans, &mo_rel);
539 if (release)
540 gsm48_cc_tx_release(trans, &mo_rel);
541 }
542
543}
544
545/* disconnect both calls from the bridge */
546static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100547 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200548{
Andreas Eversberg7e4b0322023-04-23 11:43:13 +0200549 struct gsm_trans *trans0 = trans_find_by_callref(net, TRANS_CC, bridge->callref[0]);
550 struct gsm_trans *trans1 = trans_find_by_callref(net, TRANS_CC, bridge->callref[1]);
Harald Welte27989d42018-06-21 20:39:20 +0200551 struct gsm_mncc mx_rel;
552 if (!trans0 || !trans1)
553 return;
554
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100555 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
556 trans0->callref, trans1->callref, strerror(err));
557 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200558 trans0->callref, trans1->callref, strerror(err));
559
560 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
561 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
562 GSM48_CC_CAUSE_CHAN_UNACCEPT);
563
564 mx_rel.callref = trans0->callref;
565 gsm48_cc_tx_disconnect(trans0, &mx_rel);
566
567 mx_rel.callref = trans1->callref;
568 gsm48_cc_tx_disconnect(trans1, &mx_rel);
569}
570
571static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
572 int sec, int micro)
573{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100574 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200575 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
576 osmo_timer_schedule(&trans->cc.timer, sec, micro);
577 trans->cc.Tcurrent = current;
578}
579
580static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
581{
582 struct gsm48_hdr *gh = msgb_l3(msg);
583 uint8_t msg_type = gsm48_hdr_msg_type(gh);
584 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
585 struct tlv_parsed tp;
586 struct gsm_mncc setup;
587
Philipp Maier9ca7b312018-10-10 17:00:49 +0200588 gsm48_start_guard_timer(trans);
589
Harald Welte27989d42018-06-21 20:39:20 +0200590 memset(&setup, 0, sizeof(struct gsm_mncc));
591 setup.callref = trans->callref;
592
Keith Whytea1a70be2021-05-16 02:59:52 +0200593 /* New Global Call Reference */
594 if (!trans->cc.lcls)
595 trans->cc.lcls = trans_lcls_compose(trans, true);
596
597 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300598 if (trans->cc.lcls) {
599 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
600 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
601 int rc;
602
603 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
604 memcpy(&setup.gcr[0], gcr_msg->data, rc);
605 setup.fields |= MNCC_F_GCR;
606 } else
607 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
608 msgb_free(gcr_msg);
609 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200610
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100611 OSMO_ASSERT(trans->msc_a);
612
Harald Welte27989d42018-06-21 20:39:20 +0200613 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
614 /* emergency setup is identified by msg_type */
615 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
616 setup.fields |= MNCC_F_EMERGENCY;
617 setup.emergency = 1;
618 /* use destination number as configured by user (if any) */
619 if (trans->net->emergency.route_to_msisdn) {
620 setup.fields |= MNCC_F_CALLED;
621 setup.called.type = 0; /* unknown */
622 setup.called.plan = 0; /* unknown */
623 OSMO_STRLCPY_ARRAY(setup.called.number,
624 trans->net->emergency.route_to_msisdn);
625 }
626 }
627
628 /* use subscriber as calling party number */
629 setup.fields |= MNCC_F_CALLING;
630 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
631 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
632
633 /* bearer capability */
634 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
635 setup.fields |= MNCC_F_BEARER_CAP;
636 gsm48_decode_bearer_cap(&setup.bearer_cap,
637 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
638
639 /* Create a copy of the bearer capability
640 * in the transaction struct, so we can use
641 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100642 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200643 sizeof(trans->bearer_cap));
644 }
645 /* facility */
646 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
647 setup.fields |= MNCC_F_FACILITY;
648 gsm48_decode_facility(&setup.facility,
649 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
650 }
651 /* called party bcd number */
652 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
653 setup.fields |= MNCC_F_CALLED;
654 gsm48_decode_called(&setup.called,
655 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
656 }
657 /* user-user */
658 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
659 setup.fields |= MNCC_F_USERUSER;
660 gsm48_decode_useruser(&setup.useruser,
661 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
662 }
663 /* ss-version */
664 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
665 setup.fields |= MNCC_F_SSVERSION;
666 gsm48_decode_ssversion(&setup.ssversion,
667 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
668 }
669 /* CLIR suppression */
670 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
671 setup.clir.sup = 1;
672 /* CLIR invocation */
673 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
674 setup.clir.inv = 1;
675 /* cc cap */
676 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
677 setup.fields |= MNCC_F_CCCAP;
678 gsm48_decode_cccap(&setup.cccap,
679 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
680 }
681
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100682 /* MO call leg starting, gather all codec information so far known: */
Oliver Smitha35abb72023-05-23 17:29:57 +0200683 trans_cc_filter_init(trans);
Oliver Smithc7c40c92023-05-23 17:43:40 +0200684 trans_cc_filter_set_ran(trans, trans->msc_a->c.ran->type);
Oliver Smith1c7f1782023-05-23 17:58:26 +0200685 trans_cc_filter_set_bss(trans, trans->msc_a);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100686 if (setup.fields & MNCC_F_BEARER_CAP)
Oliver Smith5375f782023-05-23 13:38:33 +0200687 trans_cc_filter_set_ms_from_bc(trans, &trans->bearer_cap);
Oliver Smithceca8e62023-05-24 11:15:52 +0200688 trans_cc_filter_run(trans);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100689
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100690 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
691 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200692
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200693 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200694
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100695 new_cc_state(trans, GSM_CSTATE_INITIATED);
696
697 /* To complete the MNCC_SETUP_IND, we need to provide an RTP address and port. First instruct the MGW to create
698 * a CN-side RTP conn, and continue with MNCC_SETUP_IND once that is done. Leave trans.cc in GSM_CSTATE_NULL and
699 * note down the msg_type to indicate that we indeed composed an MNCC_SETUP_IND for later. */
700 setup.msg_type = MNCC_SETUP_IND;
701 trans->cc.msg = setup;
702 return msc_a_try_call_assignment(trans);
703 /* continue in gsm48_cc_rx_setup_cn_local_rtp_port_known() */
704}
705
706/* Callback for MNCC_SETUP_IND waiting for the core network RTP port to be established by the MGW (via msc_a) */
707void gsm48_cc_rx_setup_cn_local_rtp_port_known(struct gsm_trans *trans)
708{
709 struct msc_a *msc_a = trans->msc_a;
710 struct gsm_mncc setup = trans->cc.msg;
711 struct osmo_sockaddr_str *rtp_cn_local;
712 struct sdp_msg *sdp;
713 int rc;
714
715 if (trans->cc.state != GSM_CSTATE_INITIATED
716 || setup.msg_type != MNCC_SETUP_IND) {
717 LOG_TRANS(trans, LOGL_ERROR,
718 "Unexpected CC state. Expected GSM_CSTATE_INITIATED and a buffered MNCC_SETUP_IND message,"
719 " found CC state %d and msg_type %s\n",
720 trans->cc.state, get_mncc_name(setup.msg_type));
721 trans->callref = 0;
722 trans_free(trans);
723 return;
724 }
725
726 if (!msc_a) {
727 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
728 trans->callref = 0;
729 trans_free(trans);
730 return;
731 }
732
733 /* 'setup' above has taken the value of trans->cc.msg, we can now clear that. */
734 trans->cc.msg = (struct gsm_mncc){};
735
736 /* Insert the CN side RTP port now available into SDP and compose SDP string */
737 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
738 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
739 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
740 trans_free(trans);
741 return;
742 }
Oliver Smithc63c3a02023-05-24 10:48:07 +0200743 trans->cc.local.rtp = *rtp_cn_local;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100744
Oliver Smithc63c3a02023-05-24 10:48:07 +0200745 sdp = trans->cc.local.audio_codecs.count ? &trans->cc.local : NULL;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100746 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
747 if (rc >= sizeof(setup.sdp)) {
748 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
749 trans_free(trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100750 return;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100751 }
752
Harald Welte27989d42018-06-21 20:39:20 +0200753 /* indicate setup to MNCC */
754 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte27989d42018-06-21 20:39:20 +0200755}
756
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100757static void rx_mncc_sdp(struct gsm_trans *trans, uint32_t mncc_msg_type, const char *sdp,
758 const struct gsm_mncc_bearer_cap *bcap)
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100759{
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100760 struct codec_filter *codecs = &trans->cc.codecs;
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100761 struct call_leg *cl = trans->msc_a ? trans->msc_a->cc.call_leg : NULL;
762 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
763
764 if (sdp[0]) {
765 int rc = sdp_msg_from_sdp_str(&trans->cc.remote, sdp);
766 if (rc)
767 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "rx %s: Failed to parse SDP: %d. Trying anyway.\n",
768 get_mncc_name(mncc_msg_type), rc);
769 }
770
771 /* if there is no SDP information or we failed to parse it, try using the Bearer Cap from MNCC, if any. */
772 if (!trans->cc.remote.audio_codecs.count && bcap) {
773 trans->cc.remote = (struct sdp_msg){};
774 trans_cc_set_remote_from_bc(trans, bcap);
775 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s Bearer Cap: remote=%s\n",
776 get_mncc_name(mncc_msg_type), sdp_msg_to_str(&trans->cc.remote));
777 }
778
779 if (!trans->cc.remote.audio_codecs.count)
780 LOG_TRANS(trans, LOGL_INFO,
781 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
782
783 trans_cc_filter_run(trans);
784 if (rtp_cn) {
785 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.remote);
786 rtp_stream_commit(rtp_cn);
787 }
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100788
789 /* See if we need to switch codecs to maintain TFO: has the remote side changed the codecs information? If we
790 * have already assigned a specific codec here, but the remote call leg has now chosen a different codec, we
791 * need to re-assign this call leg to match the remote leg. */
792 if (!sdp_audio_codec_is_set(&codecs->assignment)) {
793 /* Voice channel assignment has not completed. Do not interfere. */
794 return;
795 }
796 if (!trans->cc.remote.audio_codecs.count) {
797 /* Don't know remote codecs, nothing to do. */
798 return;
799 }
800 if (sdp_audio_codecs_by_descr(&trans->cc.remote.audio_codecs, &codecs->assignment)) {
801 /* The assigned codec is part of the remote codec set. All is well. */
802 /* TODO: maybe this should require exactly the *first* remote codec to match, because we cannot flexibly
803 * transcode, and assume the actual payload we will receive is listed in the first place? */
804 return;
805 }
806
807 /* We've already completed Assignment of a voice channel (some time ago), and now the remote side has changed
808 * to a mismatching codec (list). Try to re-assign this side to a matching codec. */
809 LOG_TRANS(trans, LOGL_INFO, "Remote call leg mismatches assigned codec: %s\n",
810 codec_filter_to_str(&trans->cc.codecs, &trans->cc.local, &trans->cc.remote));
811 msc_a_tx_assignment_cmd(trans->msc_a);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100812}
813
Harald Welte27989d42018-06-21 20:39:20 +0200814static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
815{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100816 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200817 struct gsm48_hdr *gh;
818 struct gsm_mncc *setup = arg;
819 int rc, trans_id;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100820 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200821
822 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
823
824 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700825 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100826 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200827 "This is not allowed!\n");
828 /* Temporarily out of order */
829 rc = mncc_release_ind(trans->net, trans, trans->callref,
830 GSM48_CAUSE_LOC_PRN_S_LU,
831 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
832 trans->callref = 0;
833 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200834 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200835 return rc;
836 }
837
838 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100839 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200840 if (trans_id < 0) {
841 /* no free transaction ID */
842 rc = mncc_release_ind(trans->net, trans, trans->callref,
843 GSM48_CAUSE_LOC_PRN_S_LU,
844 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
845 trans->callref = 0;
846 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200847 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200848 return rc;
849 }
850 trans->transaction_id = trans_id;
851
852 gh->msg_type = GSM48_MT_CC_SETUP;
853
854 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
855
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100856 /* MT call leg is starting. Gather all codecs information so far known.
857 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
858 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
Oliver Smithe545b9d2023-06-15 14:17:12 +0200859 * We haven't received the MT MS's Bearer Capabilities yet; the Bearer Capabilities handled here are
860 * actually the remote call leg's Bearer Capabilities. */
Oliver Smitha35abb72023-05-23 17:29:57 +0200861 trans_cc_filter_init(trans);
Oliver Smithc7c40c92023-05-23 17:43:40 +0200862 trans_cc_filter_set_ran(trans, trans->msc_a->c.ran->type);
Oliver Smith1c7f1782023-05-23 17:58:26 +0200863 trans_cc_filter_set_bss(trans, trans->msc_a);
Oliver Smith64f39302023-06-15 14:26:37 +0200864 if (setup->fields & MNCC_F_BEARER_CAP)
865 trans->bearer_cap.transfer = setup->bearer_cap.transfer;
Oliver Smith10632132023-05-12 12:14:22 +0200866
867 switch (trans->bearer_cap.transfer) {
868 case GSM48_BCAP_ITCAP_SPEECH:
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100869 /* if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
870 rx_mncc_sdp(trans, setup->msg_type, setup->sdp,
871 (setup->fields & MNCC_F_BEARER_CAP) ? &setup->bearer_cap : NULL);
872 /* rx_mncc_sdp() has called trans_cc_filter_run(trans); */
Oliver Smith10632132023-05-12 12:14:22 +0200873 break;
Manawyrm1ed12ea2023-10-14 17:23:04 +0200874 case GSM48_BCAP_ITCAP_3k1_AUDIO:
875 case GSM48_BCAP_ITCAP_FAX_G3:
Oliver Smith10632132023-05-12 12:14:22 +0200876 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
Oliver Smith412cf922023-07-05 15:47:04 +0200877 if (setup->fields & MNCC_F_BEARER_CAP) {
878 trans->cc.remote = (struct sdp_msg){};
879 trans_cc_set_remote_from_bc(trans, &setup->bearer_cap);
880 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s Bearer Cap: remote=%s\n",
881 get_mncc_name(setup->msg_type), sdp_msg_to_str(&trans->cc.remote));
882 } else {
883 LOG_TRANS(trans, LOGL_INFO,
884 "Got no information of remote Bearer Capability. Trying anyway.\n");
885 sdp_audio_codecs_set_csd(&trans->cc.codecs.ms);
886 }
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100887 trans_cc_filter_run(trans);
Oliver Smith10632132023-05-12 12:14:22 +0200888 break;
889 default:
890 LOG_TRANS(trans, LOGL_ERROR, "Handling of information transfer capability %d not implemented\n",
891 trans->bearer_cap.transfer);
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +0100892 break;
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100893 }
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100894
Oliver Smith10632132023-05-12 12:14:22 +0200895 /* Compose Bearer Capability information that reflects only the codecs (Speech Versions) / CSD bearer services
896 * remaining after intersecting MS, BSS and remote call leg restrictions. To store in trans for later use, and
897 * to include in the outgoing CC Setup message. */
898 switch (trans->bearer_cap.transfer) {
899 case GSM48_BCAP_ITCAP_SPEECH:
900 bearer_cap = (struct gsm_mncc_bearer_cap){
901 .speech_ver = { -1 },
902 };
903 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.local.audio_codecs);
904 rc = bearer_cap_set_radio(&bearer_cap);
905 if (rc) {
906 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
907 trans_free(trans);
908 msgb_free(msg);
909 return rc;
910 }
911 /* If no resulting codecs remain, error out. We cannot find a codec that matches both call legs. If the MGW were
912 * able to transcode, we could use non-identical codecs on each conn of the MGW endpoint, but we are aiming for
913 * finding a matching codec. */
914 if (bearer_cap.speech_ver[0] == -1) {
915 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
916 get_mncc_name(setup->msg_type),
917 codec_filter_to_str(&trans->cc.codecs, &trans->cc.local, &trans->cc.remote));
918
919 /* incompatible codecs */
920 rc = mncc_release_ind(trans->net, trans, trans->callref,
921 GSM48_CAUSE_LOC_PRN_S_LU,
922 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
923 trans->callref = 0;
924 trans_free(trans);
925 msgb_free(msg);
926 return rc;
927 }
928 break;
Manawyrm1ed12ea2023-10-14 17:23:04 +0200929 case GSM48_BCAP_ITCAP_3k1_AUDIO:
930 case GSM48_BCAP_ITCAP_FAX_G3:
Oliver Smith10632132023-05-12 12:14:22 +0200931 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
932 if (csd_bs_list_to_bearer_cap(&bearer_cap, &trans->cc.local.bearer_services) == 0) {
933 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
934
935 /* incompatible codecs */
936 rc = mncc_release_ind(trans->net, trans, trans->callref,
937 GSM48_CAUSE_LOC_PRN_S_LU,
938 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
939 trans->callref = 0;
940 trans_free(trans);
941 msgb_free(msg);
942 return rc;
943 }
944 break;
Harald Welte27989d42018-06-21 20:39:20 +0200945 }
Oliver Smith10632132023-05-12 12:14:22 +0200946
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100947 /* 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 +0100948 trans->bearer_cap = bearer_cap;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100949
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100950 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
951
Harald Welte27989d42018-06-21 20:39:20 +0200952 /* facility */
953 if (setup->fields & MNCC_F_FACILITY)
954 gsm48_encode_facility(msg, 0, &setup->facility);
955 /* progress */
956 if (setup->fields & MNCC_F_PROGRESS)
957 gsm48_encode_progress(msg, 0, &setup->progress);
958 /* calling party BCD number */
959 if (setup->fields & MNCC_F_CALLING)
960 gsm48_encode_calling(msg, &setup->calling);
961 /* called party BCD number */
962 if (setup->fields & MNCC_F_CALLED)
963 gsm48_encode_called(msg, &setup->called);
964 /* user-user */
965 if (setup->fields & MNCC_F_USERUSER)
966 gsm48_encode_useruser(msg, 0, &setup->useruser);
967 /* redirecting party BCD number */
968 if (setup->fields & MNCC_F_REDIRECTING)
969 gsm48_encode_redirecting(msg, &setup->redirecting);
970 /* signal */
971 if (setup->fields & MNCC_F_SIGNAL)
972 gsm48_encode_signal(msg, setup->signal);
973
974 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
975
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200976 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200977
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100978 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200979}
980
981static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
982{
983 struct gsm48_hdr *gh = msgb_l3(msg);
984 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
985 struct tlv_parsed tp;
986 struct gsm_mncc call_conf;
987 int rc;
988
989 gsm48_stop_cc_timer(trans);
990 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
991
992 memset(&call_conf, 0, sizeof(struct gsm_mncc));
993 call_conf.callref = trans->callref;
994
995 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
996#if 0
997 /* repeat */
998 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
999 call_conf.repeat = 1;
1000 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1001 call_conf.repeat = 2;
1002#endif
1003 /* bearer capability */
1004 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1005 call_conf.fields |= MNCC_F_BEARER_CAP;
1006 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
1007 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1008
1009 /* Create a copy of the bearer capability
1010 * in the transaction struct, so we can use
1011 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +01001012 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +02001013 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +01001014
1015 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
1016 * Store in codecs filter. */
Oliver Smith5375f782023-05-23 13:38:33 +02001017 trans_cc_filter_set_ms_from_bc(trans, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +02001018 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +01001019
Harald Welte27989d42018-06-21 20:39:20 +02001020 /* cause */
1021 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1022 call_conf.fields |= MNCC_F_CAUSE;
1023 gsm48_decode_cause(&call_conf.cause,
1024 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1025 }
1026 /* cc cap */
1027 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1028 call_conf.fields |= MNCC_F_CCCAP;
1029 gsm48_decode_cccap(&call_conf.cccap,
1030 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1031 }
1032
1033 /* IMSI of called subscriber */
1034 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
1035
Harald Welte27989d42018-06-21 20:39:20 +02001036 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001037 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001038
1039 /* don't continue, if there were problems with
1040 * the call assignment. */
1041 if (rc)
1042 return rc;
1043
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001044 /* Directly ack with MNCC_CALL_CONF_IND, not yet containing SDP or RTP IP:port information. */
1045 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1046 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND, &call_conf);
1047}
1048
1049static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1050 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1051 uint32_t payload_msg_type, const struct sdp_msg *sdp);
1052
1053static int gsm48_cc_mt_rtp_port_and_codec_known(struct gsm_trans *trans)
1054{
1055 struct msc_a *msc_a = trans->msc_a;
1056 struct osmo_sockaddr_str *rtp_cn_local;
1057 struct gsm_mncc_rtp;
1058
1059 if (!msc_a) {
1060 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
1061 trans->callref = 0;
1062 trans_free(trans);
1063 return -EINVAL;
1064 }
1065
1066 /* Insert the CN side RTP port now available into SDP */
1067 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
1068 if (!rtp_cn_local) {
1069 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_RTP_CREATE: no RTP set up for the CN side\n");
1070 trans_free(trans);
1071 return -EINVAL;
1072 }
Oliver Smithc63c3a02023-05-24 10:48:07 +02001073 trans->cc.local.rtp = *rtp_cn_local;
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001074
Oliver Smithceca8e62023-05-24 11:15:52 +02001075 trans_cc_filter_run(trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001076
1077 /* If we haven't completed Assignment yet, don't sent MNCC_RTP_CREATE */
1078 if (!sdp_audio_codec_is_set(&trans->cc.codecs.assignment)) {
1079 LOG_TRANS(trans, LOGL_DEBUG, "no codec confirmed by Assignment yet\n");
1080 return 0;
1081 }
1082
1083 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 +02001084 &trans->cc.local);
Harald Welte27989d42018-06-21 20:39:20 +02001085}
1086
1087static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
1088{
1089 struct gsm_mncc *proceeding = arg;
1090 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
1091 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1092 int rc;
1093
1094 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1095
1096 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1097
1098 /* bearer capability */
1099 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Oliver Smith92caa1c2023-08-23 14:26:23 +02001100 /* MNCC should not switch from e.g. CSD to speech */
1101 if (proceeding->bearer_cap.transfer != trans->bearer_cap.transfer) {
1102 LOG_TRANS(trans, LOGL_ERROR, "Unexpected Information Transfer Capability %d from MNCC,"
1103 " transaction has %d\n",
1104 proceeding->bearer_cap.transfer,
1105 trans->bearer_cap.transfer);
1106 return -EINVAL;
1107 }
Harald Welte27989d42018-06-21 20:39:20 +02001108 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
1109 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1110 }
1111 /* facility */
1112 if (proceeding->fields & MNCC_F_FACILITY)
1113 gsm48_encode_facility(msg, 0, &proceeding->facility);
1114 /* progress */
1115 if (proceeding->fields & MNCC_F_PROGRESS)
1116 gsm48_encode_progress(msg, 0, &proceeding->progress);
1117
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001118 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001119 if (rc)
1120 return rc;
1121
1122 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001123 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001124}
1125
1126static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1127{
1128 struct gsm48_hdr *gh = msgb_l3(msg);
1129 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1130 struct tlv_parsed tp;
1131 struct gsm_mncc alerting;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001132 int rc;
Harald Welte27989d42018-06-21 20:39:20 +02001133
1134 gsm48_stop_cc_timer(trans);
1135 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1136
1137 memset(&alerting, 0, sizeof(struct gsm_mncc));
1138 alerting.callref = trans->callref;
1139 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1140 /* facility */
1141 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1142 alerting.fields |= MNCC_F_FACILITY;
1143 gsm48_decode_facility(&alerting.facility,
1144 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1145 }
1146
1147 /* progress */
1148 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1149 alerting.fields |= MNCC_F_PROGRESS;
1150 gsm48_decode_progress(&alerting.progress,
1151 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1152 }
1153 /* ss-version */
1154 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1155 alerting.fields |= MNCC_F_SSVERSION;
1156 gsm48_decode_ssversion(&alerting.ssversion,
1157 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1158 }
1159
1160 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1161
Oliver Smithceca8e62023-05-24 11:15:52 +02001162 trans_cc_filter_run(trans);
Oliver Smithc63c3a02023-05-24 10:48:07 +02001163 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.local);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001164 if (rc >= sizeof(alerting.sdp)) {
1165 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
1166 rc, sizeof(alerting.sdp));
1167 trans_free(trans);
1168 return -EINVAL;
1169 }
1170
Harald Welte27989d42018-06-21 20:39:20 +02001171 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
1172 &alerting);
1173}
1174
1175static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1176{
1177 struct gsm_mncc *alerting = arg;
1178 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1179 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Oliver Smith8e16e8b2023-06-22 11:27:24 +02001180 int rc;
Harald Welte27989d42018-06-21 20:39:20 +02001181
1182 gh->msg_type = GSM48_MT_CC_ALERTING;
1183
1184 /* facility */
1185 if (alerting->fields & MNCC_F_FACILITY)
1186 gsm48_encode_facility(msg, 0, &alerting->facility);
1187 /* progress */
1188 if (alerting->fields & MNCC_F_PROGRESS)
1189 gsm48_encode_progress(msg, 0, &alerting->progress);
1190 /* user-user */
1191 if (alerting->fields & MNCC_F_USERUSER)
1192 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1193
1194 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1195
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01001196 if (alerting->sdp[0])
1197 rx_mncc_sdp(trans, alerting->msg_type, alerting->sdp,
1198 (alerting->fields & MNCC_F_BEARER_CAP) ? &alerting->bearer_cap : NULL);
Oliver Smith8e16e8b2023-06-22 11:27:24 +02001199
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01001200 /* handle the MNCC event */
1201 rc = trans_tx_gsm48(trans, msg);
1202 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02001203}
1204
1205static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1206{
1207 struct gsm_mncc *progress = arg;
1208 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1210
1211 gh->msg_type = GSM48_MT_CC_PROGRESS;
1212
1213 /* progress */
1214 gsm48_encode_progress(msg, 1, &progress->progress);
1215 /* user-user */
1216 if (progress->fields & MNCC_F_USERUSER)
1217 gsm48_encode_useruser(msg, 0, &progress->useruser);
1218
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001219 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001220}
1221
1222static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1223{
1224 struct gsm_mncc *connect = arg;
1225 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1226 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1227
1228 gh->msg_type = GSM48_MT_CC_CONNECT;
1229
1230 gsm48_stop_cc_timer(trans);
1231 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1232
1233 /* facility */
1234 if (connect->fields & MNCC_F_FACILITY)
1235 gsm48_encode_facility(msg, 0, &connect->facility);
1236 /* progress */
1237 if (connect->fields & MNCC_F_PROGRESS)
1238 gsm48_encode_progress(msg, 0, &connect->progress);
1239 /* connected number */
1240 if (connect->fields & MNCC_F_CONNECTED)
1241 gsm48_encode_connected(msg, &connect->connected);
1242 /* user-user */
1243 if (connect->fields & MNCC_F_USERUSER)
1244 gsm48_encode_useruser(msg, 0, &connect->useruser);
1245
1246 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1247
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01001248 if (connect->sdp[0])
1249 rx_mncc_sdp(trans, connect->msg_type, connect->sdp,
1250 (connect->fields & MNCC_F_BEARER_CAP) ? &connect->bearer_cap : NULL);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001251
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001252 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001253}
1254
1255static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1256{
1257 struct gsm48_hdr *gh = msgb_l3(msg);
1258 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1259 struct tlv_parsed tp;
1260 struct gsm_mncc connect;
1261
1262 gsm48_stop_cc_timer(trans);
1263
1264 memset(&connect, 0, sizeof(struct gsm_mncc));
1265 connect.callref = trans->callref;
1266 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1267 /* use subscriber as connected party number */
1268 connect.fields |= MNCC_F_CONNECTED;
1269 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1270 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1271
1272 /* facility */
1273 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1274 connect.fields |= MNCC_F_FACILITY;
1275 gsm48_decode_facility(&connect.facility,
1276 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1277 }
1278 /* user-user */
1279 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1280 connect.fields |= MNCC_F_USERUSER;
1281 gsm48_decode_useruser(&connect.useruser,
1282 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1283 }
1284 /* ss-version */
1285 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1286 connect.fields |= MNCC_F_SSVERSION;
1287 gsm48_decode_ssversion(&connect.ssversion,
1288 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1289 }
1290
1291 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001292 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001293
Oliver Smithceca8e62023-05-24 11:15:52 +02001294 trans_cc_filter_run(trans);
Oliver Smithc63c3a02023-05-24 10:48:07 +02001295 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.local);
Harald Welte27989d42018-06-21 20:39:20 +02001296 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1297}
1298
1299
1300static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1301{
1302 struct gsm_mncc connect_ack;
1303
1304 gsm48_stop_cc_timer(trans);
1305
1306 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001307 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 +02001308
1309 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1310 connect_ack.callref = trans->callref;
1311
1312 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1313 &connect_ack);
1314}
1315
1316static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1317{
1318 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1319 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1320
1321 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1322
1323 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1324
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001325 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001326}
1327
1328static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1329{
1330 struct gsm48_hdr *gh = msgb_l3(msg);
1331 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1332 struct tlv_parsed tp;
1333 struct gsm_mncc disc;
1334
1335 gsm48_stop_cc_timer(trans);
1336
1337 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1338
1339 memset(&disc, 0, sizeof(struct gsm_mncc));
1340 disc.callref = trans->callref;
1341 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1342 /* cause */
1343 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1344 disc.fields |= MNCC_F_CAUSE;
1345 gsm48_decode_cause(&disc.cause,
1346 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1347 }
1348 /* facility */
1349 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1350 disc.fields |= MNCC_F_FACILITY;
1351 gsm48_decode_facility(&disc.facility,
1352 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1353 }
1354 /* user-user */
1355 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1356 disc.fields |= MNCC_F_USERUSER;
1357 gsm48_decode_useruser(&disc.useruser,
1358 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1359 }
1360 /* ss-version */
1361 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1362 disc.fields |= MNCC_F_SSVERSION;
1363 gsm48_decode_ssversion(&disc.ssversion,
1364 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1365 }
1366
1367 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001368}
1369
1370static struct gsm_mncc_cause default_cause = {
1371 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1372 .coding = 0,
1373 .rec = 0,
1374 .rec_val = 0,
1375 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1376 .diag_len = 0,
1377 .diag = { 0 },
1378};
1379
1380static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1381{
1382 struct gsm_mncc *disc = arg;
1383 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1384 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1385
1386 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1387
1388 gsm48_stop_cc_timer(trans);
1389 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1390
1391 /* cause */
1392 if (disc->fields & MNCC_F_CAUSE)
1393 gsm48_encode_cause(msg, 1, &disc->cause);
1394 else
1395 gsm48_encode_cause(msg, 1, &default_cause);
1396
1397 /* facility */
1398 if (disc->fields & MNCC_F_FACILITY)
1399 gsm48_encode_facility(msg, 0, &disc->facility);
1400 /* progress */
1401 if (disc->fields & MNCC_F_PROGRESS)
1402 gsm48_encode_progress(msg, 0, &disc->progress);
1403 /* user-user */
1404 if (disc->fields & MNCC_F_USERUSER)
1405 gsm48_encode_useruser(msg, 0, &disc->useruser);
1406
1407 /* store disconnect cause for T306 expiry */
1408 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1409
1410 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1411
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001412 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001413}
1414
1415static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1416{
1417 struct gsm48_hdr *gh = msgb_l3(msg);
1418 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1419 struct tlv_parsed tp;
1420 struct gsm_mncc rel;
1421 int rc;
1422
1423 gsm48_stop_cc_timer(trans);
1424
1425 memset(&rel, 0, sizeof(struct gsm_mncc));
1426 rel.callref = trans->callref;
1427 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1428 /* cause */
1429 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1430 rel.fields |= MNCC_F_CAUSE;
1431 gsm48_decode_cause(&rel.cause,
1432 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1433 }
1434 /* facility */
1435 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1436 rel.fields |= MNCC_F_FACILITY;
1437 gsm48_decode_facility(&rel.facility,
1438 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1439 }
1440 /* user-user */
1441 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1442 rel.fields |= MNCC_F_USERUSER;
1443 gsm48_decode_useruser(&rel.useruser,
1444 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1445 }
1446 /* ss-version */
1447 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1448 rel.fields |= MNCC_F_SSVERSION;
1449 gsm48_decode_ssversion(&rel.ssversion,
1450 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1451 }
1452
1453 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1454 /* release collision 5.4.5 */
1455 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1456 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001457 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001458 GSM48_PDISC_CC | (trans->transaction_id << 4),
1459 GSM48_MT_CC_RELEASE_COMPL);
1460 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1461 }
1462
1463 new_cc_state(trans, GSM_CSTATE_NULL);
1464
1465 trans->callref = 0;
1466 trans_free(trans);
1467
1468 return rc;
1469}
1470
1471static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1472{
1473 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001474 struct msgb *msg;
1475 struct gsm48_hdr *gh;
1476
1477 if (!trans->msc_a) {
1478 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1479 return -EINVAL;
1480 }
1481
1482 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1483 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001484
1485 gh->msg_type = GSM48_MT_CC_RELEASE;
1486
1487 gsm48_stop_cc_timer(trans);
1488 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1489
1490 /* cause */
1491 if (rel->fields & MNCC_F_CAUSE)
1492 gsm48_encode_cause(msg, 0, &rel->cause);
1493 /* facility */
1494 if (rel->fields & MNCC_F_FACILITY)
1495 gsm48_encode_facility(msg, 0, &rel->facility);
1496 /* user-user */
1497 if (rel->fields & MNCC_F_USERUSER)
1498 gsm48_encode_useruser(msg, 0, &rel->useruser);
1499
1500 trans->cc.T308_second = 0;
1501 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1502
1503 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1504 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1505
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001506 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001507}
1508
1509static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1510{
1511 struct gsm48_hdr *gh = msgb_l3(msg);
1512 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1513 struct tlv_parsed tp;
1514 struct gsm_mncc rel;
1515 int rc = 0;
1516
1517 gsm48_stop_cc_timer(trans);
1518
1519 memset(&rel, 0, sizeof(struct gsm_mncc));
1520 rel.callref = trans->callref;
1521 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1522 /* cause */
1523 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1524 rel.fields |= MNCC_F_CAUSE;
1525 gsm48_decode_cause(&rel.cause,
1526 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1527 }
1528 /* facility */
1529 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1530 rel.fields |= MNCC_F_FACILITY;
1531 gsm48_decode_facility(&rel.facility,
1532 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1533 }
1534 /* user-user */
1535 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1536 rel.fields |= MNCC_F_USERUSER;
1537 gsm48_decode_useruser(&rel.useruser,
1538 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1539 }
1540 /* ss-version */
1541 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1542 rel.fields |= MNCC_F_SSVERSION;
1543 gsm48_decode_ssversion(&rel.ssversion,
1544 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1545 }
1546
1547 if (trans->callref) {
1548 switch (trans->cc.state) {
1549 case GSM_CSTATE_CALL_PRESENT:
1550 rc = mncc_recvmsg(trans->net, trans,
1551 MNCC_REJ_IND, &rel);
1552 break;
1553 case GSM_CSTATE_RELEASE_REQ:
1554 rc = mncc_recvmsg(trans->net, trans,
1555 MNCC_REL_CNF, &rel);
1556 break;
1557 default:
1558 rc = mncc_recvmsg(trans->net, trans,
1559 MNCC_REL_IND, &rel);
1560 }
1561 }
1562
1563 trans->callref = 0;
1564 trans_free(trans);
1565
1566 return rc;
1567}
1568
1569static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1570{
1571 struct gsm_mncc *rel = arg;
1572 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1573 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1574 int ret;
1575
1576 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1577
1578 trans->callref = 0;
1579
1580 gsm48_stop_cc_timer(trans);
1581
1582 /* cause */
1583 if (rel->fields & MNCC_F_CAUSE)
1584 gsm48_encode_cause(msg, 0, &rel->cause);
1585 /* facility */
1586 if (rel->fields & MNCC_F_FACILITY)
1587 gsm48_encode_facility(msg, 0, &rel->facility);
1588 /* user-user */
1589 if (rel->fields & MNCC_F_USERUSER)
1590 gsm48_encode_useruser(msg, 0, &rel->useruser);
1591
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001592 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001593
1594 trans_free(trans);
1595
1596 return ret;
1597}
1598
1599static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1600{
1601 struct gsm48_hdr *gh = msgb_l3(msg);
1602 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1603 struct tlv_parsed tp;
1604 struct gsm_mncc fac;
1605
1606 memset(&fac, 0, sizeof(struct gsm_mncc));
1607 fac.callref = trans->callref;
1608 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1609 /* facility */
1610 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1611 fac.fields |= MNCC_F_FACILITY;
1612 gsm48_decode_facility(&fac.facility,
1613 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1614 }
1615 /* ss-version */
1616 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1617 fac.fields |= MNCC_F_SSVERSION;
1618 gsm48_decode_ssversion(&fac.ssversion,
1619 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1620 }
1621
1622 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1623}
1624
1625static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1626{
1627 struct gsm_mncc *fac = arg;
1628 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1629 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1630
1631 gh->msg_type = GSM48_MT_CC_FACILITY;
1632
1633 /* facility */
1634 gsm48_encode_facility(msg, 1, &fac->facility);
1635
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001636 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001637}
1638
1639static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1640{
1641 struct gsm_mncc hold;
1642
1643 memset(&hold, 0, sizeof(struct gsm_mncc));
1644 hold.callref = trans->callref;
1645 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1646}
1647
1648static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1649{
1650 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1651 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1652
1653 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1654
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001655 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001656}
1657
1658static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1659{
1660 struct gsm_mncc *hold_rej = arg;
1661 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1662 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1663
1664 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1665
1666 /* cause */
1667 if (hold_rej->fields & MNCC_F_CAUSE)
1668 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1669 else
1670 gsm48_encode_cause(msg, 1, &default_cause);
1671
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001672 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001673}
1674
1675static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1676{
1677 struct gsm_mncc retrieve;
1678
1679 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1680 retrieve.callref = trans->callref;
1681 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1682 &retrieve);
1683}
1684
1685static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1686{
1687 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1688 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1689
1690 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1691
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001692 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001693}
1694
1695static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1696{
1697 struct gsm_mncc *retrieve_rej = arg;
1698 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1699 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1700
1701 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1702
1703 /* cause */
1704 if (retrieve_rej->fields & MNCC_F_CAUSE)
1705 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1706 else
1707 gsm48_encode_cause(msg, 1, &default_cause);
1708
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001709 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001710}
1711
1712static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1713{
1714 struct gsm48_hdr *gh = msgb_l3(msg);
1715 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1716 struct tlv_parsed tp;
1717 struct gsm_mncc dtmf;
1718
1719 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1720 dtmf.callref = trans->callref;
1721 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1722 /* keypad facility */
1723 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1724 dtmf.fields |= MNCC_F_KEYPAD;
1725 gsm48_decode_keypad(&dtmf.keypad,
1726 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1727 }
1728
1729 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1730}
1731
1732static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1733{
1734 struct gsm_mncc *dtmf = arg;
1735 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1736 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1737
1738 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1739
1740 /* keypad */
1741 if (dtmf->fields & MNCC_F_KEYPAD)
1742 gsm48_encode_keypad(msg, dtmf->keypad);
1743
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001744 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001745}
1746
1747static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1748{
1749 struct gsm_mncc *dtmf = arg;
1750 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1751 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1752
1753 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1754
1755 /* cause */
1756 if (dtmf->fields & MNCC_F_CAUSE)
1757 gsm48_encode_cause(msg, 1, &dtmf->cause);
1758 else
1759 gsm48_encode_cause(msg, 1, &default_cause);
1760
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001761 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001762}
1763
1764static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1765{
1766 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1767 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1768
1769 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1770
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001771 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001772}
1773
1774static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1775{
1776 struct gsm_mncc dtmf;
1777
1778 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1779 dtmf.callref = trans->callref;
1780
1781 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1782}
1783
1784static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1785{
1786 struct gsm48_hdr *gh = msgb_l3(msg);
1787 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1788 struct tlv_parsed tp;
1789 struct gsm_mncc modify;
1790
1791 memset(&modify, 0, sizeof(struct gsm_mncc));
1792 modify.callref = trans->callref;
1793 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1794 /* bearer capability */
1795 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1796 modify.fields |= MNCC_F_BEARER_CAP;
1797 gsm48_decode_bearer_cap(&modify.bearer_cap,
1798 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1799
1800 /* Create a copy of the bearer capability
1801 * in the transaction struct, so we can use
1802 * this information later */
1803 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1804 sizeof(trans->bearer_cap));
1805 }
1806
1807 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1808
1809 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1810}
1811
1812static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1813{
1814 struct gsm_mncc *modify = arg;
1815 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1816 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1817
1818 gh->msg_type = GSM48_MT_CC_MODIFY;
1819
1820 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1821
1822 /* bearer capability */
1823 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1824 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1825
1826 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1827
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001828 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001829}
1830
1831static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1832{
1833 struct gsm48_hdr *gh = msgb_l3(msg);
1834 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1835 struct tlv_parsed tp;
1836 struct gsm_mncc modify;
1837
1838 gsm48_stop_cc_timer(trans);
1839
1840 memset(&modify, 0, sizeof(struct gsm_mncc));
1841 modify.callref = trans->callref;
1842 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1843 /* bearer capability */
1844 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1845 modify.fields |= MNCC_F_BEARER_CAP;
1846 gsm48_decode_bearer_cap(&modify.bearer_cap,
1847 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1848
1849 /* Create a copy of the bearer capability
1850 * in the transaction struct, so we can use
1851 * this information later */
1852 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1853 sizeof(trans->bearer_cap));
1854 }
1855
1856 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1857
1858 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1859}
1860
1861static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1862{
1863 struct gsm_mncc *modify = arg;
1864 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1865 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1866
1867 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1868
1869 /* bearer capability */
1870 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1871 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1872
1873 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1874
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001875 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001876}
1877
1878static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1879{
1880 struct gsm48_hdr *gh = msgb_l3(msg);
1881 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1882 struct tlv_parsed tp;
1883 struct gsm_mncc modify;
1884
1885 gsm48_stop_cc_timer(trans);
1886
1887 memset(&modify, 0, sizeof(struct gsm_mncc));
1888 modify.callref = trans->callref;
1889 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1890 /* bearer capability */
1891 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1892 modify.fields |= GSM48_IE_BEARER_CAP;
1893 gsm48_decode_bearer_cap(&modify.bearer_cap,
1894 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1895
1896 /* Create a copy of the bearer capability
1897 * in the transaction struct, so we can use
1898 * this information later */
1899 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1900 sizeof(trans->bearer_cap));
1901 }
1902 /* cause */
1903 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1904 modify.fields |= MNCC_F_CAUSE;
1905 gsm48_decode_cause(&modify.cause,
1906 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1907 }
1908
1909 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1910
1911 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1912}
1913
1914static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1915{
1916 struct gsm_mncc *modify = arg;
1917 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1918 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1919
1920 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1921
1922 /* bearer capability */
1923 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1924 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1925 /* cause */
1926 gsm48_encode_cause(msg, 1, &modify->cause);
1927
1928 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1929
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001930 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001931}
1932
1933static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1934{
1935 struct gsm_mncc *notify = arg;
1936 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1937 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1938
1939 gh->msg_type = GSM48_MT_CC_NOTIFY;
1940
1941 /* notify */
1942 gsm48_encode_notify(msg, notify->notify);
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_notify(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 notify;
1953
1954 memset(&notify, 0, sizeof(struct gsm_mncc));
1955 notify.callref = trans->callref;
1956// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1957 if (payload_len >= 1)
1958 gsm48_decode_notify(&notify.notify, gh->data);
1959
1960 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1961}
1962
1963static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1964{
1965 struct gsm_mncc *user = arg;
1966 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1967 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1968
1969 gh->msg_type = GSM48_MT_CC_USER_INFO;
1970
1971 /* user-user */
1972 if (user->fields & MNCC_F_USERUSER)
1973 gsm48_encode_useruser(msg, 1, &user->useruser);
1974 /* more data */
1975 if (user->more)
1976 gsm48_encode_more(msg);
1977
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001978 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001979}
1980
1981static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1982{
1983 struct gsm48_hdr *gh = msgb_l3(msg);
1984 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1985 struct tlv_parsed tp;
1986 struct gsm_mncc user;
1987
1988 memset(&user, 0, sizeof(struct gsm_mncc));
1989 user.callref = trans->callref;
1990 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1991 /* user-user */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1993 user.fields |= MNCC_F_USERUSER;
1994 gsm48_decode_useruser(&user.useruser,
1995 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1996 }
1997 /* more data */
1998 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1999 user.more = 1;
2000
2001 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
2002}
2003
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002004static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
2005 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002006 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02002007{
2008 uint8_t data[sizeof(struct gsm_mncc)];
2009 struct gsm_mncc_rtp *rtp;
2010
2011 memset(&data, 0, sizeof(data));
2012 rtp = (struct gsm_mncc_rtp *) &data[0];
2013
2014 rtp->callref = callref;
2015 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002017 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
2018 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002019 }
Harald Welte27989d42018-06-21 20:39:20 +02002020 rtp->payload_type = payload_type;
2021 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002022 if (sdp)
2023 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002024 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02002025}
2026
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002027static 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 +02002028{
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002029 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02002030}
2031
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002032static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002033{
2034 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02002035
2036 /* Find callref */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +02002037 trans = trans_find_by_callref(net, TRANS_CC, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002038 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002039 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002040 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02002041 return -EIO;
2042 }
2043 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002044 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002045 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002046 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02002047 return 0;
2048 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002049 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002050
Harald Welte27989d42018-06-21 20:39:20 +02002051 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002052 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002053}
2054
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002055int cc_on_cn_local_rtp_port_known(struct gsm_trans *cc_trans)
2056{
2057 /* Depending on MO or MT call, dispatch the event differently */
2058 switch (cc_trans->cc.state) {
2059 case GSM_CSTATE_INITIATED:
2060 if (cc_trans->cc.msg.msg_type != MNCC_SETUP_IND) {
2061 LOG_TRANS(cc_trans, LOGL_ERROR, "Assuming MO call, expected MNCC_SETUP_IND to be prepared\n");
2062 return -EINVAL;
2063 }
2064 /* This is the MO call leg, waiting for a CN RTP be able to send initial MNCC_SETUP_IND. */
2065 gsm48_cc_rx_setup_cn_local_rtp_port_known(cc_trans);
2066 return 0;
2067
2068 case GSM_CSTATE_MO_TERM_CALL_CONF:
2069 /* This is the MT call leg, waiting for a CN RTP to be able to send MNCC_CALL_CONF_IND. */
2070 return gsm48_cc_mt_rtp_port_and_codec_known(cc_trans);
2071
2072 default:
2073 LOG_TRANS(cc_trans, LOGL_ERROR, "CN RTP address available, but in unexpected state %d\n",
2074 cc_trans->cc.state);
2075 return -EINVAL;
2076 }
2077}
2078
2079int cc_on_assignment_done(struct gsm_trans *trans)
2080{
2081 struct msc_a *msc_a = trans->msc_a;
2082
2083 switch (trans->cc.state) {
2084 case GSM_CSTATE_INITIATED:
2085 case GSM_CSTATE_MO_CALL_PROC:
Neels Hofmeyrd767c732023-11-17 04:12:29 +01002086 /* MO call, send ACK in form of an MNCC_RTP_CREATE (below) */
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002087 break;
2088
2089 case GSM_CSTATE_CALL_RECEIVED:
2090 case GSM_CSTATE_MO_TERM_CALL_CONF:
Neels Hofmeyrd767c732023-11-17 04:12:29 +01002091 /* MT call, send ACK in form of an MNCC_RTP_CREATE (below) */
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002092 break;
2093
2094 case GSM_CSTATE_ACTIVE:
Neels Hofmeyrd767c732023-11-17 04:12:29 +01002095 /* already active. We decided to re-assign later on during the call - at time of writing this never
2096 * happens. */
2097 case GSM_CSTATE_CALL_DELIVERED:
2098 case GSM_CSTATE_CONNECT_IND:
2099 /* MNCC has progressed past the initial assignment. Usually it means that this happened: after
2100 * MNCC_ALERT_REQ, MO has triggered a re-assignment, to adjust MO's codec to MT's codec. */
2101 LOG_TRANS(trans, LOGL_DEBUG, "Re-Assignment complete\n");
2102 return 0;
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002103
2104 default:
2105 LOG_TRANS(trans, LOGL_ERROR, "Assignment done in unexpected CC state: %d\n", trans->cc.state);
2106 return -EINVAL;
2107 }
2108
2109 if (!call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN)) {
2110 LOG_TRANS(trans, LOGL_DEBUG,
2111 "Assignment complete, but still waiting for the CRCX OK on the CN side RTP\n");
2112 return 0;
2113 }
2114 return gsm48_tch_rtp_create(trans);
2115}
2116
Harald Welte27989d42018-06-21 20:39:20 +02002117/* Trigger TCH_RTP_CREATE acknowledgement */
2118int gsm48_tch_rtp_create(struct gsm_trans *trans)
2119{
2120 /* This function is called as soon as the port, on which the
2121 * mgcp-gw expects the incoming RTP stream from the remote
2122 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002123 struct msc_a *msc_a = trans->msc_a;
2124 struct gsm_network *net = msc_a_net(msc_a);
2125 struct call_leg *cl = msc_a->cc.call_leg;
2126 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002127 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002128 int mncc_payload_msg_type;
2129 struct sdp_audio_codec *codec;
Neels Hofmeyra001a702022-10-31 17:57:30 +01002130 const struct codec_mapping *m;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002131 struct sdp_audio_codecs *codecs;
Harald Welte27989d42018-06-21 20:39:20 +02002132
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002133 if (!rtp_cn) {
2134 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
2135 return -EINVAL;
2136 }
2137
Oliver Smithceca8e62023-05-24 11:15:52 +02002138 trans_cc_filter_run(trans);
Oliver Smithc63c3a02023-05-24 10:48:07 +02002139 codecs = &trans->cc.local.audio_codecs;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002140 if (!codecs->count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002141 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002142 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002143 return -EINVAL;
2144 }
2145
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002146 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
2147 codec = &codecs->codec[0];
2148 m = codec_mapping_by_subtype_name(codec->subtype_name);
2149 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02002150
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002151 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
2152 if (!rtp_cn_local) {
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002153 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 +01002154 return -EINVAL;
2155 }
2156
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002157 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002158 codec->payload_type, mncc_payload_msg_type, &trans->cc.local);
Harald Welte27989d42018-06-21 20:39:20 +02002159}
2160
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002161static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002162{
2163 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002164 struct call_leg *cl;
2165 struct rtp_stream *rtps;
Philipp Maier8ad3dac2018-08-07 13:00:14 +02002166
Harald Welte27989d42018-06-21 20:39:20 +02002167 /* Find callref */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +02002168 trans = trans_find_by_callref(net, TRANS_CC, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002169 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002170 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002171 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02002172 return -EIO;
2173 }
2174 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002175 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002176 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002177 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002178 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02002179 }
2180
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002181 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002182
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002183 rx_mncc_sdp(trans, rtp->msg_type, rtp->sdp, NULL);
2184
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002185 cl = trans->msc_a->cc.call_leg;
2186 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002187 if (rtps && !osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002188 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
2189 struct osmo_sockaddr_str rtp_addr;
2190 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
2191 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
2192 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2193 return -EINVAL;
2194 }
2195 rtp_stream_set_remote_addr(rtps, &rtp_addr);
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002196 rtp_stream_commit(rtps);
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002197 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002198 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02002199}
2200
2201static struct downstate {
2202 uint32_t states;
2203 int type;
2204 int (*rout) (struct gsm_trans *trans, void *arg);
2205} downstatelist[] = {
2206 /* mobile originating call establishment */
2207 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2208 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2209 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2210 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2211 {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 */
2212 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2213 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2214 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2215 /* mobile terminating call establishment */
2216 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2217 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2218 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2219 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2220 /* signalling during call */
2221 {SBIT(GSM_CSTATE_ACTIVE),
2222 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2223 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2224 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2225 {ALL_STATES,
2226 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2227 {ALL_STATES,
2228 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2229 {ALL_STATES,
2230 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2231 {SBIT(GSM_CSTATE_ACTIVE),
2232 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2233 {SBIT(GSM_CSTATE_ACTIVE),
2234 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2235 {SBIT(GSM_CSTATE_ACTIVE),
2236 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2237 {SBIT(GSM_CSTATE_ACTIVE),
2238 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2239 {SBIT(GSM_CSTATE_ACTIVE),
2240 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2241 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2242 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2243 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2244 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2245 {SBIT(GSM_CSTATE_ACTIVE),
2246 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2247 /* clearing */
2248 {SBIT(GSM_CSTATE_INITIATED),
2249 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2250 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2251 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2252 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2253 MNCC_REL_REQ, gsm48_cc_tx_release},
2254};
2255
2256#define DOWNSLLEN \
2257 (sizeof(downstatelist) / sizeof(struct downstate))
2258
2259
Philipp Maiercd64af72019-08-01 09:46:40 +02002260static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002261{
2262 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002263 struct msc_a *msc_a = NULL;
2264 struct gsm_trans *trans = NULL;
2265 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002266
Harald Welte27989d42018-06-21 20:39:20 +02002267 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002268 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002269 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002270 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002271 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002272 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002273 return rc;
2274 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002275 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002276 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002277 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002278 case MNCC_RTP_FREE:
2279 /* unused right now */
2280 return -EIO;
2281
2282 case MNCC_FRAME_DROP:
2283 case MNCC_FRAME_RECV:
2284 case GSM_TCHF_FRAME:
2285 case GSM_TCHF_FRAME_EFR:
2286 case GSM_TCHH_FRAME:
2287 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002288 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002289 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002290 return -ENOTSUP;
2291 }
2292
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002293 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002294
2295 /* Find callref */
Andreas Eversberg7e4b0322023-04-23 11:43:13 +02002296 trans = trans_find_by_callref(net, TRANS_CC, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002297
2298 /* Callref unknown */
2299 if (!trans) {
2300 struct vlr_subscr *vsub;
2301
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002302 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002303 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002304 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002305 /* Invalid call reference */
2306 return mncc_release_ind(net, NULL, data->callref,
2307 GSM48_CAUSE_LOC_PRN_S_LU,
2308 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2309 }
2310 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002311 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002312 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002313 /* Invalid number */
2314 return mncc_release_ind(net, NULL, data->callref,
2315 GSM48_CAUSE_LOC_PRN_S_LU,
2316 GSM48_CC_CAUSE_INV_NR_FORMAT);
2317 }
2318 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002319 if (data->called.number[0]) {
2320 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2321 if (!vsub)
2322 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002323 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002324 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002325 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002326 if (!vsub)
2327 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002328 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002329 }
2330 if (!vsub)
2331 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002332 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002333 /* update the subscriber we deal with */
2334 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2335
Harald Welte27989d42018-06-21 20:39:20 +02002336 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002337 if (!vsub->lu_complete) {
2338 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002339 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002340 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002341 /* Temporarily out of order */
2342 return mncc_release_ind(net, NULL, data->callref,
2343 GSM48_CAUSE_LOC_PRN_S_LU,
2344 GSM48_CC_CAUSE_DEST_OOO);
2345 }
Keith Whyte991bb422019-08-08 15:43:40 +02002346
2347 /* Find valid conn */
2348 msc_a = msc_a_for_vsub(vsub, true);
2349
2350 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2351 if (!net->call_waiting && msc_a) {
2352 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2353 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2354 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2355 "rx '%s' for subscriber %s with trans state (%s)"
2356 " rejecting with USER_BUSY\n",
2357 get_mncc_name(msg->msg_type), data->called.number,
2358 gsm48_cc_state_name(existing_cc_trans->cc.state));
2359 return mncc_release_ind(net, NULL, data->callref,
2360 GSM48_CAUSE_LOC_PRN_S_LU,
2361 GSM48_CC_CAUSE_USER_BUSY);
2362 }
2363 }
2364
Harald Welte27989d42018-06-21 20:39:20 +02002365 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002366 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002367 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002368 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002369 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002370 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002371 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002372 mncc_release_ind(net, NULL, data->callref,
2373 GSM48_CAUSE_LOC_PRN_S_LU,
2374 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2375 return -ENOMEM;
2376 }
2377
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002378 /* Remember remote SDP, if any */
Neels Hofmeyrcbabe1e2023-12-10 05:25:36 +01002379 rx_mncc_sdp(trans, data->msg_type, data->sdp,
2380 (data->fields & MNCC_F_BEARER_CAP) ? &data->bearer_cap : NULL);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002381
Harald Welte27989d42018-06-21 20:39:20 +02002382 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002383 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002384 /* This condition will return before the common logging of the received MNCC message below, so
2385 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002386 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002387
Harald Welte27989d42018-06-21 20:39:20 +02002388 /* store setup information until paging succeeds */
2389 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2390
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002391 /* Request a channel. If Paging already started, paging_request_start() will append the new
2392 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002393 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2394 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002395 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002396 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002397 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002398 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002399 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002400 return 0;
2401 }
2402
2403 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002404 trans->msc_a = msc_a;
2405 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002406 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002407 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002408 } else {
2409 /* update the subscriber we deal with */
2410 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2411 }
2412
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002413 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002414
Mychaela N. Falconia9b7de742023-06-26 05:30:29 +00002415 /*
2416 * The step of gsm48_start_guard_timer() needs to be done for
2417 * major state-impacting MNCC messages, but not for those
2418 * that are a mere pass-through to CC messages to MS.
2419 */
2420 switch (msg->msg_type) {
2421 case MNCC_PROGRESS_REQ:
2422 case MNCC_NOTIFY_REQ:
2423 case MNCC_FACILITY_REQ:
2424 case MNCC_START_DTMF_RSP:
2425 case MNCC_START_DTMF_REJ:
2426 case MNCC_STOP_DTMF_RSP:
2427 case MNCC_HOLD_CNF:
2428 case MNCC_HOLD_REJ:
2429 case MNCC_RETRIEVE_CNF:
2430 case MNCC_RETRIEVE_REJ:
2431 case MNCC_USERINFO_REQ:
2432 break;
2433 default:
2434 gsm48_start_guard_timer(trans);
2435 }
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002436 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002437
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002438 if (trans->msc_a)
2439 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002440
2441 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002442 if (!msc_a) {
2443 struct gsm_mncc rel = {
2444 .callref = data->callref,
2445 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002446 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002447 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2448 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002449 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002450 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2451 else
2452 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2453 trans->callref = 0;
2454 trans_free(trans);
2455 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002456 }
2457
2458 /* Find function for current state and message */
2459 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002460 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002461 && ((1 << trans->cc.state) & downstatelist[i].states))
2462 break;
2463 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002464 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002465 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002466 return 0;
2467 }
2468
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002469 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002470
2471 return rc;
2472}
2473
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002474struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2475{
2476 uint32_t callref;
2477
2478 switch (msg->msg_type) {
2479 case MNCC_BRIDGE:
2480 callref = msg->bridge.callref[0];
2481 break;
2482 case MNCC_RTP_CREATE:
2483 case MNCC_RTP_CONNECT:
2484 callref = msg->rtp.callref;
2485 break;
2486
2487 case MNCC_RTP_FREE:
2488 case MNCC_FRAME_DROP:
2489 case MNCC_FRAME_RECV:
2490 case GSM_TCHF_FRAME:
2491 case GSM_TCHF_FRAME_EFR:
2492 case GSM_TCHH_FRAME:
2493 case GSM_TCH_FRAME_AMR:
2494 return NULL;
2495
2496 default:
2497 callref = msg->signal.callref;
2498 break;
2499 }
2500
2501 return mncc_call_find_by_callref(callref);
2502}
2503
2504/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002505int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002506{
2507 const union mncc_msg *msg = arg;
2508 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002509
2510 if (msg->msg_type == MNCC_SETUP_REQ) {
2511 /* Incoming call to forward for inter-MSC Handover? */
2512 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2513 if (mncc_call)
2514 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2515 "Incoming call matches pending inter-MSC Handover Number\n");
2516 }
2517 if (!mncc_call) {
2518 /* Find already active MNCC FSM for this callref.
2519 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2520 * MNCC<->GSM-CC call handling. */
2521 mncc_call = mncc_find_by_callref_from_msg(msg);
2522 }
2523 if (mncc_call) {
2524 mncc_call_rx(mncc_call, msg);
2525 return 0;
2526 }
2527
2528 /* None of the above? Then it must be a normal GSM CC call related message. */
2529 return mncc_tx_to_gsm_cc(net, msg);
2530}
Harald Welte27989d42018-06-21 20:39:20 +02002531
2532static struct datastate {
2533 uint32_t states;
2534 int type;
2535 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2536} datastatelist[] = {
2537 /* mobile originating call establishment */
2538 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2539 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2540 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2541 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2542 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2543 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2544 /* mobile terminating call establishment */
2545 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2546 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2547 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2548 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2549 {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 */
2550 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2551 /* signalling during call */
2552 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2553 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2554 {SBIT(GSM_CSTATE_ACTIVE),
2555 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2556 {ALL_STATES,
2557 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2558 {ALL_STATES,
2559 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2560 {ALL_STATES,
2561 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2562 {SBIT(GSM_CSTATE_ACTIVE),
2563 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2564 {SBIT(GSM_CSTATE_ACTIVE),
2565 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2566 {SBIT(GSM_CSTATE_ACTIVE),
2567 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2568 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2569 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2570 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2571 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2572 {SBIT(GSM_CSTATE_ACTIVE),
2573 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2574 /* clearing */
2575 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2576 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2577 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2578 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2579 {ALL_STATES, /* 5.4.3.4 */
2580 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2581};
2582
2583#define DATASLLEN \
2584 (sizeof(datastatelist) / sizeof(struct datastate))
2585
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002586int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002587{
2588 struct gsm48_hdr *gh = msgb_l3(msg);
2589 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2590 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2591 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002592 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2593 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002594 int i, rc = 0;
2595
2596 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002597 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002598 return -EINVAL;
2599 }
2600
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002601 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002602 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002603 return -EINVAL;
2604 }
2605
2606 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002607 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002608
Harald Welte27989d42018-06-21 20:39:20 +02002609 /* Create transaction */
2610 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002611 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002612 trans = trans_alloc(net, vsub,
2613 TRANS_CC,
2614 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002615 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002616 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002617 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002618 GSM48_PDISC_CC | (transaction_id << 4),
2619 GSM48_MT_CC_RELEASE_COMPL);
2620 return -ENOMEM;
2621 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002622 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2623 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2624 trans_free(trans);
2625 return -EINVAL;
2626 }
2627
Harald Welte27989d42018-06-21 20:39:20 +02002628 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002629 msc_a_get(msc_a, MSC_A_USE_CC);
2630 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002631 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002632
2633 /* An earlier CM Service Request for this CC message now has concluded */
2634 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2635 LOG_MSC_A(msc_a, LOGL_ERROR,
2636 "Creating new CC transaction without prior CM Service Request\n");
2637 else
2638 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002639 }
2640
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002641 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2642 gsm48_cc_state_name(trans->cc.state));
2643
Harald Welte27989d42018-06-21 20:39:20 +02002644 /* find function for current state and message */
2645 for (i = 0; i < DATASLLEN; i++)
2646 if ((msg_type == datastatelist[i].type)
2647 && ((1 << trans->cc.state) & datastatelist[i].states))
2648 break;
2649 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002650 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002651
2652 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2653 * transaction right away. */
2654 if (trans->cc.state == GSM_CSTATE_NULL) {
2655 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2656 " -- disarding new CC transaction right away\n");
2657 trans_free(trans);
2658 }
Harald Welte27989d42018-06-21 20:39:20 +02002659 return 0;
2660 }
2661
2662 assert(trans->vsub);
2663
2664 rc = datastatelist[i].rout(trans, msg);
2665
Harald Welte27989d42018-06-21 20:39:20 +02002666 return rc;
2667}