blob: 868dd66e21ec0eb1288a300ed15025b26f3903a9 [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
132void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
133{
134 net->mncc_recv(net, msg);
135}
136
137int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
138{
139 struct gsm48_hdr *gh;
140 struct msgb *ss_notify;
141
142 ss_notify = gsm0480_create_notifySS(message);
143 if (!ss_notify)
144 return -1;
145
146 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
147 uint8_t *data = msgb_push(ss_notify, 1);
148 data[0] = ss_notify->len - 1;
149 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
150 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100151 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200152}
153
154/* FIXME: this count_statistics is a state machine behaviour. we should convert
155 * the complete call control into a state machine. Afterwards we can move this
156 * code into state transitions.
157 */
158static void count_statistics(struct gsm_trans *trans, int new_state)
159{
160 int old_state = trans->cc.state;
161 struct rate_ctr_group *msc = trans->net->msc_ctrs;
162
163 if (old_state == new_state)
164 return;
165
166 /* state incoming */
167 switch (new_state) {
168 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200169 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
170 1);
171 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200172 break;
173 }
174
175 /* state outgoing */
176 switch (old_state) {
177 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200178 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
179 1);
Harald Welte27989d42018-06-21 20:39:20 +0200180 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
181 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200182 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200183 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200184 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200185 break;
186 }
187}
188
Harald Welte27989d42018-06-21 20:39:20 +0200189static void new_cc_state(struct gsm_trans *trans, int state)
190{
191 if (state > 31 || state < 0)
192 return;
193
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100194 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
195 gsm48_cc_state_name(trans->cc.state),
196 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200197
198 count_statistics(trans, state);
199 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200200
201 /* Stop the guard timer when a call reaches the active state */
202 if (state == GSM_CSTATE_ACTIVE)
203 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200204}
205
206static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
207{
208 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
210 uint8_t *cause, *call_state;
211
212 gh->msg_type = GSM48_MT_CC_STATUS;
213
214 cause = msgb_put(msg, 3);
215 cause[0] = 2;
216 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
217 cause[2] = 0x80 | 30; /* response to status inquiry */
218
219 call_state = msgb_put(msg, 1);
220 call_state[0] = 0xc0 | 0x00;
221
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100222 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200223}
224
225static void gsm48_stop_cc_timer(struct gsm_trans *trans)
226{
227 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100228 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200229 osmo_timer_del(&trans->cc.timer);
230 trans->cc.Tcurrent = 0;
231 }
232}
233
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100234/* Log the MNCC tx and rx events.
235 * Depending on msg_type, also log whether RTP information is passed on.
236 * (This is particularly interesting for the doc/sequence_charts/msc_log_to_ladder.py)
237 */
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200238#define log_mncc_rx_tx(ARGS...) _log_mncc_rx_tx(__FILE__, __LINE__, ##ARGS)
239static void _log_mncc_rx_tx(const char *file, int line,
240 struct gsm_trans *trans, const char *rx_tx, const union mncc_msg *mncc)
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100241{
242 const char *sdp = NULL;
243 struct sdp_msg sdp_msg = {};
244 struct osmo_sockaddr addr = {};
245
246 if (!log_check_level(DMNCC, LOGL_DEBUG))
247 return;
248
249 switch (mncc->msg_type) {
250 case MNCC_RTP_CREATE:
251 case MNCC_RTP_CONNECT:
252 addr = (struct osmo_sockaddr){ .u.sas = mncc->rtp.addr };
253 sdp = mncc->rtp.sdp;
254 break;
255
256 case MNCC_SETUP_IND:
257 case MNCC_SETUP_REQ:
258 case MNCC_SETUP_COMPL_IND:
259 case MNCC_SETUP_COMPL_REQ:
260 case MNCC_SETUP_RSP:
261 case MNCC_SETUP_CNF:
262 case MNCC_CALL_CONF_IND:
263 case MNCC_CALL_PROC_REQ:
264 case MNCC_ALERT_IND:
265 case MNCC_ALERT_REQ:
266 sdp = mncc->signal.sdp;
267 break;
268
269 default:
270 break;
271 }
272
273 if (sdp && sdp[0] && (sdp_msg_from_sdp_str(&sdp_msg, sdp) == 0)) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200274 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
275 rx_tx,
276 get_mncc_name(mncc->msg_type),
277 sdp_msg_to_str(&sdp_msg));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100278 return;
279 }
280
281 if (osmo_sockaddr_is_any(&addr) == 0) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200282 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
283 rx_tx,
284 get_mncc_name(mncc->msg_type),
285 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100286 return;
287 }
288
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200289 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s\n", rx_tx, get_mncc_name(mncc->msg_type));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100290}
291
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200292#define mncc_recvmsg(ARGS...) _mncc_recvmsg(__FILE__, __LINE__, ##ARGS)
293static int _mncc_recvmsg(const char *file, int line,
294 struct gsm_network *net, struct gsm_trans *trans, int msg_type, struct gsm_mncc *mncc)
Harald Welte27989d42018-06-21 20:39:20 +0200295{
296 struct msgb *msg;
297 unsigned char *data;
298
Harald Welte27989d42018-06-21 20:39:20 +0200299 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100300 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200301
302 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
303 if (!msg)
304 return -ENOMEM;
305
306 data = msgb_put(msg, sizeof(struct gsm_mncc));
307 memcpy(data, mncc, sizeof(struct gsm_mncc));
308
309 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200310 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
311 if (trans)
312 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200313
314 return 0;
315}
316
317int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
318 uint32_t callref, int location, int value)
319{
320 struct gsm_mncc rel;
321
322 memset(&rel, 0, sizeof(rel));
323 rel.callref = callref;
324 mncc_set_cause(&rel, location, value);
325 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
326 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
327 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
328}
329
330/* Call Control Specific transaction release.
331 * gets called by trans_free, DO NOT CALL YOURSELF! */
332void _gsm48_cc_trans_free(struct gsm_trans *trans)
333{
334 gsm48_stop_cc_timer(trans);
335
Harald Welte27989d42018-06-21 20:39:20 +0200336 /* send release to L4, if callref still exists */
337 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100338 /* Send MNCC REL.ind (cause='Resource unavailable') */
339 if (trans->cc.mncc_initiated) {
340 mncc_release_ind(trans->net, trans, trans->callref,
341 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100342 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
343 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100344 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
345 }
346
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100347 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
348 * CC Release to the MS if it gets freed here. Hack it to do so. */
349 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
350 struct gsm_mncc rel = {};
351 rel.callref = trans->callref;
352 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
353 gsm48_cc_tx_release(trans, &rel);
354 }
Harald Welte27989d42018-06-21 20:39:20 +0200355 /* This is a final freeing of the transaction. The MNCC release may have triggered the
356 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
357 gsm48_stop_cc_timer(trans);
358 }
359 if (trans->cc.state != GSM_CSTATE_NULL)
360 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200361
362 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100363
364 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
365 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200366}
367
Harald Welte27989d42018-06-21 20:39:20 +0200368/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100369static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200370{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100371 if (trans->msc_a) {
372 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
373 "Handle paging error: transaction already associated with subscriber,"
374 " apparently it was already handled. Skip.\n");
375 return;
Harald Welte27989d42018-06-21 20:39:20 +0200376 }
377
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100378 if (msc_a) {
379 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
380 /* Assign conn */
381 msc_a_get(msc_a, MSC_A_USE_CC);
382 trans->msc_a = msc_a;
383 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200384
385 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300386 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200387 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300388 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
389 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
390 &trans->cc.msg.gcr[0],
391 sizeof(trans->cc.msg.gcr));
392 if (rc < 0)
393 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
394 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200395 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200396 }
397
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100398 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
399 /* send SETUP request to called party */
400 gsm48_cc_tx_setup(trans, &trans->cc.msg);
401 } else {
402 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
403 /* Temporarily out of order */
404 mncc_release_ind(trans->net, trans,
405 trans->callref,
406 GSM48_CAUSE_LOC_PRN_S_LU,
407 GSM48_CC_CAUSE_DEST_OOO);
408 trans->callref = 0;
409 trans->paging_request = NULL;
410 trans_free(trans);
411 }
Harald Welte27989d42018-06-21 20:39:20 +0200412}
413
414/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100415static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200416{
417 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
418 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100419 struct call_leg *cl1;
420 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200421
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100422 if (!trans1 || !trans2) {
423 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200424 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100425 }
Harald Welte27989d42018-06-21 20:39:20 +0200426
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100427 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100428 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
429 LOG_TRANS(trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
Harald Welte27989d42018-06-21 20:39:20 +0200430 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100431 }
432
433 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
434 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200435
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100436 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
437 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
438 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
439 * If we can't, then we must give up. */
440 cl1 = trans1->msc_a->cc.call_leg;
441 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200442
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100443 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200444}
445
446static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
447{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100448 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200449 return gsm48_cc_tx_status(trans, msg);
450}
451
Harald Welte27989d42018-06-21 20:39:20 +0200452static void gsm48_cc_timeout(void *arg)
453{
454 struct gsm_trans *trans = arg;
455 int disconnect = 0, release = 0;
456 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
457 int mo_location = GSM48_CAUSE_LOC_USER;
458 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
459 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
460 struct gsm_mncc mo_rel, l4_rel;
461
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200462 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
463
Harald Welte27989d42018-06-21 20:39:20 +0200464 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
465 mo_rel.callref = trans->callref;
466 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
467 l4_rel.callref = trans->callref;
468
469 switch(trans->cc.Tcurrent) {
470 case 0x303:
471 release = 1;
472 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
473 break;
474 case 0x310:
475 disconnect = 1;
476 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
477 break;
478 case 0x313:
479 disconnect = 1;
480 /* unknown, did not find it in the specs */
481 break;
482 case 0x301:
483 disconnect = 1;
484 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
485 break;
486 case 0x308:
487 if (!trans->cc.T308_second) {
488 /* restart T308 a second time */
489 gsm48_cc_tx_release(trans, &trans->cc.msg);
490 trans->cc.T308_second = 1;
491 break; /* stay in release state */
492 }
493 trans_free(trans);
494 return;
495 case 0x306:
496 release = 1;
497 mo_cause = trans->cc.msg.cause.value;
498 mo_location = trans->cc.msg.cause.location;
499 break;
500 case 0x323:
501 disconnect = 1;
502 break;
503 default:
504 release = 1;
505 }
506
507 if (release && trans->callref) {
508 /* process release towards layer 4 */
509 mncc_release_ind(trans->net, trans, trans->callref,
510 l4_location, l4_cause);
511 trans->callref = 0;
512 }
513
514 if (disconnect && trans->callref) {
515 /* process disconnect towards layer 4 */
516 mncc_set_cause(&l4_rel, l4_location, l4_cause);
517 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
518 }
519
520 /* process disconnect towards mobile station */
521 if (disconnect || release) {
522 mncc_set_cause(&mo_rel, mo_location, mo_cause);
523 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
524 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
525 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
526 mo_rel.cause.diag_len = 3;
527
528 if (disconnect)
529 gsm48_cc_tx_disconnect(trans, &mo_rel);
530 if (release)
531 gsm48_cc_tx_release(trans, &mo_rel);
532 }
533
534}
535
536/* disconnect both calls from the bridge */
537static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100538 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200539{
540 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
541 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
542 struct gsm_mncc mx_rel;
543 if (!trans0 || !trans1)
544 return;
545
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100546 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
547 trans0->callref, trans1->callref, strerror(err));
548 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200549 trans0->callref, trans1->callref, strerror(err));
550
551 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
552 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
553 GSM48_CC_CAUSE_CHAN_UNACCEPT);
554
555 mx_rel.callref = trans0->callref;
556 gsm48_cc_tx_disconnect(trans0, &mx_rel);
557
558 mx_rel.callref = trans1->callref;
559 gsm48_cc_tx_disconnect(trans1, &mx_rel);
560}
561
562static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
563 int sec, int micro)
564{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100565 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200566 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
567 osmo_timer_schedule(&trans->cc.timer, sec, micro);
568 trans->cc.Tcurrent = current;
569}
570
571static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
572{
573 struct gsm48_hdr *gh = msgb_l3(msg);
574 uint8_t msg_type = gsm48_hdr_msg_type(gh);
575 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
576 struct tlv_parsed tp;
577 struct gsm_mncc setup;
578
Philipp Maier9ca7b312018-10-10 17:00:49 +0200579 gsm48_start_guard_timer(trans);
580
Harald Welte27989d42018-06-21 20:39:20 +0200581 memset(&setup, 0, sizeof(struct gsm_mncc));
582 setup.callref = trans->callref;
583
Keith Whytea1a70be2021-05-16 02:59:52 +0200584 /* New Global Call Reference */
585 if (!trans->cc.lcls)
586 trans->cc.lcls = trans_lcls_compose(trans, true);
587
588 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300589 if (trans->cc.lcls) {
590 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
591 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
592 int rc;
593
594 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
595 memcpy(&setup.gcr[0], gcr_msg->data, rc);
596 setup.fields |= MNCC_F_GCR;
597 } else
598 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
599 msgb_free(gcr_msg);
600 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200601
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100602 OSMO_ASSERT(trans->msc_a);
603
Harald Welte27989d42018-06-21 20:39:20 +0200604 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
605 /* emergency setup is identified by msg_type */
606 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
607 setup.fields |= MNCC_F_EMERGENCY;
608 setup.emergency = 1;
609 /* use destination number as configured by user (if any) */
610 if (trans->net->emergency.route_to_msisdn) {
611 setup.fields |= MNCC_F_CALLED;
612 setup.called.type = 0; /* unknown */
613 setup.called.plan = 0; /* unknown */
614 OSMO_STRLCPY_ARRAY(setup.called.number,
615 trans->net->emergency.route_to_msisdn);
616 }
617 }
618
619 /* use subscriber as calling party number */
620 setup.fields |= MNCC_F_CALLING;
621 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
622 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
623
624 /* bearer capability */
625 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
626 setup.fields |= MNCC_F_BEARER_CAP;
627 gsm48_decode_bearer_cap(&setup.bearer_cap,
628 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
629
630 /* Create a copy of the bearer capability
631 * in the transaction struct, so we can use
632 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100633 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200634 sizeof(trans->bearer_cap));
635 }
636 /* facility */
637 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
638 setup.fields |= MNCC_F_FACILITY;
639 gsm48_decode_facility(&setup.facility,
640 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
641 }
642 /* called party bcd number */
643 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
644 setup.fields |= MNCC_F_CALLED;
645 gsm48_decode_called(&setup.called,
646 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
647 }
648 /* user-user */
649 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
650 setup.fields |= MNCC_F_USERUSER;
651 gsm48_decode_useruser(&setup.useruser,
652 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
653 }
654 /* ss-version */
655 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
656 setup.fields |= MNCC_F_SSVERSION;
657 gsm48_decode_ssversion(&setup.ssversion,
658 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
659 }
660 /* CLIR suppression */
661 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
662 setup.clir.sup = 1;
663 /* CLIR invocation */
664 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
665 setup.clir.inv = 1;
666 /* cc cap */
667 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
668 setup.fields |= MNCC_F_CCCAP;
669 gsm48_decode_cccap(&setup.cccap,
670 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
671 }
672
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100673 /* MO call leg starting, gather all codec information so far known: */
Oliver Smitha35abb72023-05-23 17:29:57 +0200674 trans_cc_filter_init(trans);
Oliver Smithc7c40c92023-05-23 17:43:40 +0200675 trans_cc_filter_set_ran(trans, trans->msc_a->c.ran->type);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100676 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
677 if (setup.fields & MNCC_F_BEARER_CAP)
Oliver Smith5375f782023-05-23 13:38:33 +0200678 trans_cc_filter_set_ms_from_bc(trans, &trans->bearer_cap);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100679 codec_filter_run(&trans->cc.codecs);
680
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100681 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
682 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100683 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200684
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200685 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200686
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100687 new_cc_state(trans, GSM_CSTATE_INITIATED);
688
689 /* To complete the MNCC_SETUP_IND, we need to provide an RTP address and port. First instruct the MGW to create
690 * a CN-side RTP conn, and continue with MNCC_SETUP_IND once that is done. Leave trans.cc in GSM_CSTATE_NULL and
691 * note down the msg_type to indicate that we indeed composed an MNCC_SETUP_IND for later. */
692 setup.msg_type = MNCC_SETUP_IND;
693 trans->cc.msg = setup;
694 return msc_a_try_call_assignment(trans);
695 /* continue in gsm48_cc_rx_setup_cn_local_rtp_port_known() */
696}
697
698/* Callback for MNCC_SETUP_IND waiting for the core network RTP port to be established by the MGW (via msc_a) */
699void gsm48_cc_rx_setup_cn_local_rtp_port_known(struct gsm_trans *trans)
700{
701 struct msc_a *msc_a = trans->msc_a;
702 struct gsm_mncc setup = trans->cc.msg;
703 struct osmo_sockaddr_str *rtp_cn_local;
704 struct sdp_msg *sdp;
705 int rc;
706
707 if (trans->cc.state != GSM_CSTATE_INITIATED
708 || setup.msg_type != MNCC_SETUP_IND) {
709 LOG_TRANS(trans, LOGL_ERROR,
710 "Unexpected CC state. Expected GSM_CSTATE_INITIATED and a buffered MNCC_SETUP_IND message,"
711 " found CC state %d and msg_type %s\n",
712 trans->cc.state, get_mncc_name(setup.msg_type));
713 trans->callref = 0;
714 trans_free(trans);
715 return;
716 }
717
718 if (!msc_a) {
719 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
720 trans->callref = 0;
721 trans_free(trans);
722 return;
723 }
724
725 /* 'setup' above has taken the value of trans->cc.msg, we can now clear that. */
726 trans->cc.msg = (struct gsm_mncc){};
727
728 /* Insert the CN side RTP port now available into SDP and compose SDP string */
729 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
730 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
731 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
732 trans_free(trans);
733 return;
734 }
735 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100736
737 sdp = trans->cc.codecs.result.audio_codecs.count ? &trans->cc.codecs.result : NULL;
738 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
739 if (rc >= sizeof(setup.sdp)) {
740 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
741 trans_free(trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100742 return;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100743 }
744
Harald Welte27989d42018-06-21 20:39:20 +0200745 /* indicate setup to MNCC */
746 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte27989d42018-06-21 20:39:20 +0200747}
748
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100749static void rx_mncc_sdp(struct gsm_trans *trans, uint32_t mncc_msg_type, const char *sdp)
750{
751 int rc;
752 if (!sdp[0])
753 return;
754 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, sdp);
755 if (rc)
756 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "rx %s: Failed to parse SDP: %d\n",
757 get_mncc_name(mncc_msg_type), rc);
758}
759
Harald Welte27989d42018-06-21 20:39:20 +0200760static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
761{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100762 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200763 struct gsm48_hdr *gh;
764 struct gsm_mncc *setup = arg;
765 int rc, trans_id;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100766 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200767
768 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
769
770 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700771 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100772 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200773 "This is not allowed!\n");
774 /* Temporarily out of order */
775 rc = mncc_release_ind(trans->net, trans, trans->callref,
776 GSM48_CAUSE_LOC_PRN_S_LU,
777 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
778 trans->callref = 0;
779 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200780 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200781 return rc;
782 }
783
784 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100785 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200786 if (trans_id < 0) {
787 /* no free transaction ID */
788 rc = mncc_release_ind(trans->net, trans, trans->callref,
789 GSM48_CAUSE_LOC_PRN_S_LU,
790 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
791 trans->callref = 0;
792 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200793 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200794 return rc;
795 }
796 trans->transaction_id = trans_id;
797
798 gh->msg_type = GSM48_MT_CC_SETUP;
799
800 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
801
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100802 /* MT call leg is starting. Gather all codecs information so far known.
803 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
804 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
Oliver Smithe545b9d2023-06-15 14:17:12 +0200805 * We haven't received the MT MS's Bearer Capabilities yet; the Bearer Capabilities handled here are
806 * actually the remote call leg's Bearer Capabilities. */
Oliver Smitha35abb72023-05-23 17:29:57 +0200807 trans_cc_filter_init(trans);
Oliver Smithc7c40c92023-05-23 17:43:40 +0200808 trans_cc_filter_set_ran(trans, trans->msc_a->c.ran->type);
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100809 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100810 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100811 rx_mncc_sdp(trans, setup->msg_type, setup->sdp);
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100812 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
813 * MNCC, if any. */
814 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
815 trans->cc.codecs.remote = (struct sdp_msg){};
816 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
817 &setup->bearer_cap);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100818 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s Bearer Cap: remote=%s\n",
819 get_mncc_name(setup->msg_type), sdp_msg_to_str(&trans->cc.codecs.remote));
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100820 }
821 if (!trans->cc.codecs.remote.audio_codecs.count)
822 LOG_TRANS(trans, LOGL_INFO,
823 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
824
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100825 codec_filter_run(&trans->cc.codecs);
826 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
827
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100828 /* Compose Bearer Capability information that reflects only the codecs (Speech Versions) remaining after
829 * intersecting MS, BSS and remote call leg restrictions. To store in trans for later use, and to include in
830 * the outgoing CC Setup message. */
831 bearer_cap = (struct gsm_mncc_bearer_cap){
832 .speech_ver = { -1 },
833 };
834 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.codecs.result.audio_codecs);
835 rc = bearer_cap_set_radio(&bearer_cap);
836 if (rc) {
837 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
838 trans_free(trans);
839 msgb_free(msg);
840 return rc;
Harald Welte27989d42018-06-21 20:39:20 +0200841 }
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100842 /* 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 +0100843 trans->bearer_cap = bearer_cap;
844 /* If no resulting codecs remain, error out. We cannot find a codec that matches both call legs. If the MGW were
845 * able to transcode, we could use non-identical codecs on each conn of the MGW endpoint, but we are aiming for
846 * finding a matching codec. */
847 if (bearer_cap.speech_ver[0] == -1) {
848 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
849 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
850
851 /* incompatible codecs */
852 rc = mncc_release_ind(trans->net, trans, trans->callref,
853 GSM48_CAUSE_LOC_PRN_S_LU,
854 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
855 trans->callref = 0;
856 trans_free(trans);
857 msgb_free(msg);
858 return rc;
859 }
860 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
861
Harald Welte27989d42018-06-21 20:39:20 +0200862 /* facility */
863 if (setup->fields & MNCC_F_FACILITY)
864 gsm48_encode_facility(msg, 0, &setup->facility);
865 /* progress */
866 if (setup->fields & MNCC_F_PROGRESS)
867 gsm48_encode_progress(msg, 0, &setup->progress);
868 /* calling party BCD number */
869 if (setup->fields & MNCC_F_CALLING)
870 gsm48_encode_calling(msg, &setup->calling);
871 /* called party BCD number */
872 if (setup->fields & MNCC_F_CALLED)
873 gsm48_encode_called(msg, &setup->called);
874 /* user-user */
875 if (setup->fields & MNCC_F_USERUSER)
876 gsm48_encode_useruser(msg, 0, &setup->useruser);
877 /* redirecting party BCD number */
878 if (setup->fields & MNCC_F_REDIRECTING)
879 gsm48_encode_redirecting(msg, &setup->redirecting);
880 /* signal */
881 if (setup->fields & MNCC_F_SIGNAL)
882 gsm48_encode_signal(msg, setup->signal);
883
884 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
885
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200886 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200887
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100888 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200889}
890
891static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
892{
893 struct gsm48_hdr *gh = msgb_l3(msg);
894 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
895 struct tlv_parsed tp;
896 struct gsm_mncc call_conf;
897 int rc;
898
899 gsm48_stop_cc_timer(trans);
900 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
901
902 memset(&call_conf, 0, sizeof(struct gsm_mncc));
903 call_conf.callref = trans->callref;
904
905 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
906#if 0
907 /* repeat */
908 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
909 call_conf.repeat = 1;
910 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
911 call_conf.repeat = 2;
912#endif
913 /* bearer capability */
914 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
915 call_conf.fields |= MNCC_F_BEARER_CAP;
916 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
917 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
918
919 /* Create a copy of the bearer capability
920 * in the transaction struct, so we can use
921 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100922 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200923 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +0100924
925 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
926 * Store in codecs filter. */
Oliver Smith5375f782023-05-23 13:38:33 +0200927 trans_cc_filter_set_ms_from_bc(trans, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200928 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100929
Harald Welte27989d42018-06-21 20:39:20 +0200930 /* cause */
931 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
932 call_conf.fields |= MNCC_F_CAUSE;
933 gsm48_decode_cause(&call_conf.cause,
934 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
935 }
936 /* cc cap */
937 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
938 call_conf.fields |= MNCC_F_CCCAP;
939 gsm48_decode_cccap(&call_conf.cccap,
940 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
941 }
942
943 /* IMSI of called subscriber */
944 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
945
Harald Welte27989d42018-06-21 20:39:20 +0200946 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100947 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200948
949 /* don't continue, if there were problems with
950 * the call assignment. */
951 if (rc)
952 return rc;
953
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100954 /* Directly ack with MNCC_CALL_CONF_IND, not yet containing SDP or RTP IP:port information. */
955 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
956 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND, &call_conf);
957}
958
959static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
960 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
961 uint32_t payload_msg_type, const struct sdp_msg *sdp);
962
963static int gsm48_cc_mt_rtp_port_and_codec_known(struct gsm_trans *trans)
964{
965 struct msc_a *msc_a = trans->msc_a;
966 struct osmo_sockaddr_str *rtp_cn_local;
967 struct gsm_mncc_rtp;
968
969 if (!msc_a) {
970 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
971 trans->callref = 0;
972 trans_free(trans);
973 return -EINVAL;
974 }
975
976 /* Insert the CN side RTP port now available into SDP */
977 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
978 if (!rtp_cn_local) {
979 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_RTP_CREATE: no RTP set up for the CN side\n");
980 trans_free(trans);
981 return -EINVAL;
982 }
983 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
984
985 codec_filter_run(&trans->cc.codecs);
986 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
987
988 /* If we haven't completed Assignment yet, don't sent MNCC_RTP_CREATE */
989 if (!sdp_audio_codec_is_set(&trans->cc.codecs.assignment)) {
990 LOG_TRANS(trans, LOGL_DEBUG, "no codec confirmed by Assignment yet\n");
991 return 0;
992 }
993
994 return mncc_recv_rtp(msc_a_net(msc_a), trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, 0, 0,
995 &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +0200996}
997
998static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
999{
1000 struct gsm_mncc *proceeding = arg;
1001 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
1002 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1003 int rc;
1004
1005 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1006
1007 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1008
1009 /* bearer capability */
1010 if (proceeding->fields & MNCC_F_BEARER_CAP) {
1011 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
1012 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1013 }
1014 /* facility */
1015 if (proceeding->fields & MNCC_F_FACILITY)
1016 gsm48_encode_facility(msg, 0, &proceeding->facility);
1017 /* progress */
1018 if (proceeding->fields & MNCC_F_PROGRESS)
1019 gsm48_encode_progress(msg, 0, &proceeding->progress);
1020
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001021 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001022 if (rc)
1023 return rc;
1024
1025 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001026 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001027}
1028
1029static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1030{
1031 struct gsm48_hdr *gh = msgb_l3(msg);
1032 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1033 struct tlv_parsed tp;
1034 struct gsm_mncc alerting;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001035 int rc;
Harald Welte27989d42018-06-21 20:39:20 +02001036
1037 gsm48_stop_cc_timer(trans);
1038 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1039
1040 memset(&alerting, 0, sizeof(struct gsm_mncc));
1041 alerting.callref = trans->callref;
1042 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1043 /* facility */
1044 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1045 alerting.fields |= MNCC_F_FACILITY;
1046 gsm48_decode_facility(&alerting.facility,
1047 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1048 }
1049
1050 /* progress */
1051 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1052 alerting.fields |= MNCC_F_PROGRESS;
1053 gsm48_decode_progress(&alerting.progress,
1054 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1055 }
1056 /* ss-version */
1057 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1058 alerting.fields |= MNCC_F_SSVERSION;
1059 gsm48_decode_ssversion(&alerting.ssversion,
1060 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1061 }
1062
1063 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1064
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001065 codec_filter_run(&trans->cc.codecs);
1066 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1067 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
1068 if (rc >= sizeof(alerting.sdp)) {
1069 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
1070 rc, sizeof(alerting.sdp));
1071 trans_free(trans);
1072 return -EINVAL;
1073 }
1074
Harald Welte27989d42018-06-21 20:39:20 +02001075 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
1076 &alerting);
1077}
1078
1079static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1080{
1081 struct gsm_mncc *alerting = arg;
1082 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1083 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1084
1085 gh->msg_type = GSM48_MT_CC_ALERTING;
1086
1087 /* facility */
1088 if (alerting->fields & MNCC_F_FACILITY)
1089 gsm48_encode_facility(msg, 0, &alerting->facility);
1090 /* progress */
1091 if (alerting->fields & MNCC_F_PROGRESS)
1092 gsm48_encode_progress(msg, 0, &alerting->progress);
1093 /* user-user */
1094 if (alerting->fields & MNCC_F_USERUSER)
1095 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1096
1097 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1098
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001099 if (alerting->sdp[0]) {
1100 struct call_leg *cl = trans->msc_a->cc.call_leg;
1101 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1102 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
1103 codec_filter_run(&trans->cc.codecs);
1104 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1105 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
1106 if (rtp_cn) {
1107 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1108 rtp_stream_commit(rtp_cn);
1109 }
1110 }
1111
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001112 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001113}
1114
1115static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1116{
1117 struct gsm_mncc *progress = arg;
1118 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1119 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1120
1121 gh->msg_type = GSM48_MT_CC_PROGRESS;
1122
1123 /* progress */
1124 gsm48_encode_progress(msg, 1, &progress->progress);
1125 /* user-user */
1126 if (progress->fields & MNCC_F_USERUSER)
1127 gsm48_encode_useruser(msg, 0, &progress->useruser);
1128
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001129 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001130}
1131
1132static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1133{
1134 struct gsm_mncc *connect = arg;
1135 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1136 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1137
1138 gh->msg_type = GSM48_MT_CC_CONNECT;
1139
1140 gsm48_stop_cc_timer(trans);
1141 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1142
1143 /* facility */
1144 if (connect->fields & MNCC_F_FACILITY)
1145 gsm48_encode_facility(msg, 0, &connect->facility);
1146 /* progress */
1147 if (connect->fields & MNCC_F_PROGRESS)
1148 gsm48_encode_progress(msg, 0, &connect->progress);
1149 /* connected number */
1150 if (connect->fields & MNCC_F_CONNECTED)
1151 gsm48_encode_connected(msg, &connect->connected);
1152 /* user-user */
1153 if (connect->fields & MNCC_F_USERUSER)
1154 gsm48_encode_useruser(msg, 0, &connect->useruser);
1155
1156 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1157
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001158 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
1159 if (connect->sdp[0]) {
1160 struct call_leg *cl = trans->msc_a->cc.call_leg;
1161 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1162 rx_mncc_sdp(trans, connect->msg_type, connect->sdp);
1163 codec_filter_run(&trans->cc.codecs);
1164 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1165 get_mncc_name(connect->msg_type),
1166 codec_filter_to_str(&trans->cc.codecs));
1167 if (rtp_cn) {
1168 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1169 rtp_stream_commit(rtp_cn);
1170 }
1171 }
1172
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001173 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001174}
1175
1176static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1177{
1178 struct gsm48_hdr *gh = msgb_l3(msg);
1179 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1180 struct tlv_parsed tp;
1181 struct gsm_mncc connect;
1182
1183 gsm48_stop_cc_timer(trans);
1184
1185 memset(&connect, 0, sizeof(struct gsm_mncc));
1186 connect.callref = trans->callref;
1187 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1188 /* use subscriber as connected party number */
1189 connect.fields |= MNCC_F_CONNECTED;
1190 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1191 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1192
1193 /* facility */
1194 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1195 connect.fields |= MNCC_F_FACILITY;
1196 gsm48_decode_facility(&connect.facility,
1197 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1198 }
1199 /* user-user */
1200 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1201 connect.fields |= MNCC_F_USERUSER;
1202 gsm48_decode_useruser(&connect.useruser,
1203 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1204 }
1205 /* ss-version */
1206 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1207 connect.fields |= MNCC_F_SSVERSION;
1208 gsm48_decode_ssversion(&connect.ssversion,
1209 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1210 }
1211
1212 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001213 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001214
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001215 codec_filter_run(&trans->cc.codecs);
1216 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001217 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1218}
1219
1220
1221static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1222{
1223 struct gsm_mncc connect_ack;
1224
1225 gsm48_stop_cc_timer(trans);
1226
1227 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001228 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 +02001229
1230 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1231 connect_ack.callref = trans->callref;
1232
1233 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1234 &connect_ack);
1235}
1236
1237static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1238{
1239 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1240 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1241
1242 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1243
1244 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1245
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001246 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001247}
1248
1249static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1250{
1251 struct gsm48_hdr *gh = msgb_l3(msg);
1252 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1253 struct tlv_parsed tp;
1254 struct gsm_mncc disc;
1255
1256 gsm48_stop_cc_timer(trans);
1257
1258 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1259
1260 memset(&disc, 0, sizeof(struct gsm_mncc));
1261 disc.callref = trans->callref;
1262 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1263 /* cause */
1264 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1265 disc.fields |= MNCC_F_CAUSE;
1266 gsm48_decode_cause(&disc.cause,
1267 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1268 }
1269 /* facility */
1270 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1271 disc.fields |= MNCC_F_FACILITY;
1272 gsm48_decode_facility(&disc.facility,
1273 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1274 }
1275 /* user-user */
1276 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1277 disc.fields |= MNCC_F_USERUSER;
1278 gsm48_decode_useruser(&disc.useruser,
1279 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1280 }
1281 /* ss-version */
1282 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1283 disc.fields |= MNCC_F_SSVERSION;
1284 gsm48_decode_ssversion(&disc.ssversion,
1285 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1286 }
1287
1288 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001289}
1290
1291static struct gsm_mncc_cause default_cause = {
1292 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1293 .coding = 0,
1294 .rec = 0,
1295 .rec_val = 0,
1296 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1297 .diag_len = 0,
1298 .diag = { 0 },
1299};
1300
1301static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1302{
1303 struct gsm_mncc *disc = arg;
1304 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1305 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1306
1307 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1308
1309 gsm48_stop_cc_timer(trans);
1310 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1311
1312 /* cause */
1313 if (disc->fields & MNCC_F_CAUSE)
1314 gsm48_encode_cause(msg, 1, &disc->cause);
1315 else
1316 gsm48_encode_cause(msg, 1, &default_cause);
1317
1318 /* facility */
1319 if (disc->fields & MNCC_F_FACILITY)
1320 gsm48_encode_facility(msg, 0, &disc->facility);
1321 /* progress */
1322 if (disc->fields & MNCC_F_PROGRESS)
1323 gsm48_encode_progress(msg, 0, &disc->progress);
1324 /* user-user */
1325 if (disc->fields & MNCC_F_USERUSER)
1326 gsm48_encode_useruser(msg, 0, &disc->useruser);
1327
1328 /* store disconnect cause for T306 expiry */
1329 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1330
1331 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1332
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001334}
1335
1336static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1337{
1338 struct gsm48_hdr *gh = msgb_l3(msg);
1339 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1340 struct tlv_parsed tp;
1341 struct gsm_mncc rel;
1342 int rc;
1343
1344 gsm48_stop_cc_timer(trans);
1345
1346 memset(&rel, 0, sizeof(struct gsm_mncc));
1347 rel.callref = trans->callref;
1348 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1349 /* cause */
1350 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1351 rel.fields |= MNCC_F_CAUSE;
1352 gsm48_decode_cause(&rel.cause,
1353 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1354 }
1355 /* facility */
1356 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1357 rel.fields |= MNCC_F_FACILITY;
1358 gsm48_decode_facility(&rel.facility,
1359 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1360 }
1361 /* user-user */
1362 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1363 rel.fields |= MNCC_F_USERUSER;
1364 gsm48_decode_useruser(&rel.useruser,
1365 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1366 }
1367 /* ss-version */
1368 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1369 rel.fields |= MNCC_F_SSVERSION;
1370 gsm48_decode_ssversion(&rel.ssversion,
1371 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1372 }
1373
1374 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1375 /* release collision 5.4.5 */
1376 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1377 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001378 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001379 GSM48_PDISC_CC | (trans->transaction_id << 4),
1380 GSM48_MT_CC_RELEASE_COMPL);
1381 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1382 }
1383
1384 new_cc_state(trans, GSM_CSTATE_NULL);
1385
1386 trans->callref = 0;
1387 trans_free(trans);
1388
1389 return rc;
1390}
1391
1392static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1393{
1394 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001395 struct msgb *msg;
1396 struct gsm48_hdr *gh;
1397
1398 if (!trans->msc_a) {
1399 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1400 return -EINVAL;
1401 }
1402
1403 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1404 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001405
1406 gh->msg_type = GSM48_MT_CC_RELEASE;
1407
1408 gsm48_stop_cc_timer(trans);
1409 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1410
1411 /* cause */
1412 if (rel->fields & MNCC_F_CAUSE)
1413 gsm48_encode_cause(msg, 0, &rel->cause);
1414 /* facility */
1415 if (rel->fields & MNCC_F_FACILITY)
1416 gsm48_encode_facility(msg, 0, &rel->facility);
1417 /* user-user */
1418 if (rel->fields & MNCC_F_USERUSER)
1419 gsm48_encode_useruser(msg, 0, &rel->useruser);
1420
1421 trans->cc.T308_second = 0;
1422 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1423
1424 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1425 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1426
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001427 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001428}
1429
1430static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1431{
1432 struct gsm48_hdr *gh = msgb_l3(msg);
1433 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1434 struct tlv_parsed tp;
1435 struct gsm_mncc rel;
1436 int rc = 0;
1437
1438 gsm48_stop_cc_timer(trans);
1439
1440 memset(&rel, 0, sizeof(struct gsm_mncc));
1441 rel.callref = trans->callref;
1442 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1443 /* cause */
1444 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1445 rel.fields |= MNCC_F_CAUSE;
1446 gsm48_decode_cause(&rel.cause,
1447 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1448 }
1449 /* facility */
1450 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1451 rel.fields |= MNCC_F_FACILITY;
1452 gsm48_decode_facility(&rel.facility,
1453 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1454 }
1455 /* user-user */
1456 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1457 rel.fields |= MNCC_F_USERUSER;
1458 gsm48_decode_useruser(&rel.useruser,
1459 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1460 }
1461 /* ss-version */
1462 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1463 rel.fields |= MNCC_F_SSVERSION;
1464 gsm48_decode_ssversion(&rel.ssversion,
1465 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1466 }
1467
1468 if (trans->callref) {
1469 switch (trans->cc.state) {
1470 case GSM_CSTATE_CALL_PRESENT:
1471 rc = mncc_recvmsg(trans->net, trans,
1472 MNCC_REJ_IND, &rel);
1473 break;
1474 case GSM_CSTATE_RELEASE_REQ:
1475 rc = mncc_recvmsg(trans->net, trans,
1476 MNCC_REL_CNF, &rel);
1477 break;
1478 default:
1479 rc = mncc_recvmsg(trans->net, trans,
1480 MNCC_REL_IND, &rel);
1481 }
1482 }
1483
1484 trans->callref = 0;
1485 trans_free(trans);
1486
1487 return rc;
1488}
1489
1490static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1491{
1492 struct gsm_mncc *rel = arg;
1493 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1494 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1495 int ret;
1496
1497 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1498
1499 trans->callref = 0;
1500
1501 gsm48_stop_cc_timer(trans);
1502
1503 /* cause */
1504 if (rel->fields & MNCC_F_CAUSE)
1505 gsm48_encode_cause(msg, 0, &rel->cause);
1506 /* facility */
1507 if (rel->fields & MNCC_F_FACILITY)
1508 gsm48_encode_facility(msg, 0, &rel->facility);
1509 /* user-user */
1510 if (rel->fields & MNCC_F_USERUSER)
1511 gsm48_encode_useruser(msg, 0, &rel->useruser);
1512
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001513 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001514
1515 trans_free(trans);
1516
1517 return ret;
1518}
1519
1520static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1521{
1522 struct gsm48_hdr *gh = msgb_l3(msg);
1523 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1524 struct tlv_parsed tp;
1525 struct gsm_mncc fac;
1526
1527 memset(&fac, 0, sizeof(struct gsm_mncc));
1528 fac.callref = trans->callref;
1529 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1530 /* facility */
1531 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1532 fac.fields |= MNCC_F_FACILITY;
1533 gsm48_decode_facility(&fac.facility,
1534 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1535 }
1536 /* ss-version */
1537 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1538 fac.fields |= MNCC_F_SSVERSION;
1539 gsm48_decode_ssversion(&fac.ssversion,
1540 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1541 }
1542
1543 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1544}
1545
1546static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1547{
1548 struct gsm_mncc *fac = arg;
1549 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1550 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1551
1552 gh->msg_type = GSM48_MT_CC_FACILITY;
1553
1554 /* facility */
1555 gsm48_encode_facility(msg, 1, &fac->facility);
1556
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001557 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001558}
1559
1560static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1561{
1562 struct gsm_mncc hold;
1563
1564 memset(&hold, 0, sizeof(struct gsm_mncc));
1565 hold.callref = trans->callref;
1566 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1567}
1568
1569static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1570{
1571 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1572 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1573
1574 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1575
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001576 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001577}
1578
1579static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1580{
1581 struct gsm_mncc *hold_rej = arg;
1582 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1584
1585 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1586
1587 /* cause */
1588 if (hold_rej->fields & MNCC_F_CAUSE)
1589 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1590 else
1591 gsm48_encode_cause(msg, 1, &default_cause);
1592
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001593 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001594}
1595
1596static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1597{
1598 struct gsm_mncc retrieve;
1599
1600 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1601 retrieve.callref = trans->callref;
1602 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1603 &retrieve);
1604}
1605
1606static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1607{
1608 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1609 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1610
1611 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1612
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001613 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001614}
1615
1616static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1617{
1618 struct gsm_mncc *retrieve_rej = arg;
1619 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1620 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1621
1622 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1623
1624 /* cause */
1625 if (retrieve_rej->fields & MNCC_F_CAUSE)
1626 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1627 else
1628 gsm48_encode_cause(msg, 1, &default_cause);
1629
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001630 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001631}
1632
1633static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1634{
1635 struct gsm48_hdr *gh = msgb_l3(msg);
1636 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1637 struct tlv_parsed tp;
1638 struct gsm_mncc dtmf;
1639
1640 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1641 dtmf.callref = trans->callref;
1642 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1643 /* keypad facility */
1644 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1645 dtmf.fields |= MNCC_F_KEYPAD;
1646 gsm48_decode_keypad(&dtmf.keypad,
1647 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1648 }
1649
1650 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1651}
1652
1653static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1654{
1655 struct gsm_mncc *dtmf = arg;
1656 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1657 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1658
1659 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1660
1661 /* keypad */
1662 if (dtmf->fields & MNCC_F_KEYPAD)
1663 gsm48_encode_keypad(msg, dtmf->keypad);
1664
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001665 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001666}
1667
1668static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1669{
1670 struct gsm_mncc *dtmf = arg;
1671 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1672 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1673
1674 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1675
1676 /* cause */
1677 if (dtmf->fields & MNCC_F_CAUSE)
1678 gsm48_encode_cause(msg, 1, &dtmf->cause);
1679 else
1680 gsm48_encode_cause(msg, 1, &default_cause);
1681
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001682 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001683}
1684
1685static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1686{
1687 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1688 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1689
1690 gh->msg_type = GSM48_MT_CC_STOP_DTMF_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_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1696{
1697 struct gsm_mncc dtmf;
1698
1699 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1700 dtmf.callref = trans->callref;
1701
1702 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1703}
1704
1705static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1706{
1707 struct gsm48_hdr *gh = msgb_l3(msg);
1708 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1709 struct tlv_parsed tp;
1710 struct gsm_mncc modify;
1711
1712 memset(&modify, 0, sizeof(struct gsm_mncc));
1713 modify.callref = trans->callref;
1714 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1715 /* bearer capability */
1716 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1717 modify.fields |= MNCC_F_BEARER_CAP;
1718 gsm48_decode_bearer_cap(&modify.bearer_cap,
1719 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1720
1721 /* Create a copy of the bearer capability
1722 * in the transaction struct, so we can use
1723 * this information later */
1724 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1725 sizeof(trans->bearer_cap));
1726 }
1727
1728 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1729
1730 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1731}
1732
1733static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1734{
1735 struct gsm_mncc *modify = arg;
1736 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1737 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1738
1739 gh->msg_type = GSM48_MT_CC_MODIFY;
1740
1741 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1742
1743 /* bearer capability */
1744 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1745 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1746
1747 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1748
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001749 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001750}
1751
1752static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1753{
1754 struct gsm48_hdr *gh = msgb_l3(msg);
1755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1756 struct tlv_parsed tp;
1757 struct gsm_mncc modify;
1758
1759 gsm48_stop_cc_timer(trans);
1760
1761 memset(&modify, 0, sizeof(struct gsm_mncc));
1762 modify.callref = trans->callref;
1763 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1764 /* bearer capability */
1765 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1766 modify.fields |= MNCC_F_BEARER_CAP;
1767 gsm48_decode_bearer_cap(&modify.bearer_cap,
1768 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1769
1770 /* Create a copy of the bearer capability
1771 * in the transaction struct, so we can use
1772 * this information later */
1773 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1774 sizeof(trans->bearer_cap));
1775 }
1776
1777 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1778
1779 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1780}
1781
1782static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1783{
1784 struct gsm_mncc *modify = arg;
1785 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1786 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1787
1788 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1789
1790 /* bearer capability */
1791 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1792 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1793
1794 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1795
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001796 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001797}
1798
1799static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1800{
1801 struct gsm48_hdr *gh = msgb_l3(msg);
1802 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1803 struct tlv_parsed tp;
1804 struct gsm_mncc modify;
1805
1806 gsm48_stop_cc_timer(trans);
1807
1808 memset(&modify, 0, sizeof(struct gsm_mncc));
1809 modify.callref = trans->callref;
1810 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1811 /* bearer capability */
1812 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1813 modify.fields |= GSM48_IE_BEARER_CAP;
1814 gsm48_decode_bearer_cap(&modify.bearer_cap,
1815 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1816
1817 /* Create a copy of the bearer capability
1818 * in the transaction struct, so we can use
1819 * this information later */
1820 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1821 sizeof(trans->bearer_cap));
1822 }
1823 /* cause */
1824 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1825 modify.fields |= MNCC_F_CAUSE;
1826 gsm48_decode_cause(&modify.cause,
1827 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1828 }
1829
1830 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1831
1832 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1833}
1834
1835static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1836{
1837 struct gsm_mncc *modify = arg;
1838 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1839 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1840
1841 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1842
1843 /* bearer capability */
1844 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1845 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1846 /* cause */
1847 gsm48_encode_cause(msg, 1, &modify->cause);
1848
1849 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1850
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001851 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001852}
1853
1854static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1855{
1856 struct gsm_mncc *notify = arg;
1857 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1858 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1859
1860 gh->msg_type = GSM48_MT_CC_NOTIFY;
1861
1862 /* notify */
1863 gsm48_encode_notify(msg, notify->notify);
1864
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001865 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001866}
1867
1868static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1869{
1870 struct gsm48_hdr *gh = msgb_l3(msg);
1871 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1872// struct tlv_parsed tp;
1873 struct gsm_mncc notify;
1874
1875 memset(&notify, 0, sizeof(struct gsm_mncc));
1876 notify.callref = trans->callref;
1877// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1878 if (payload_len >= 1)
1879 gsm48_decode_notify(&notify.notify, gh->data);
1880
1881 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1882}
1883
1884static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1885{
1886 struct gsm_mncc *user = arg;
1887 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1888 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1889
1890 gh->msg_type = GSM48_MT_CC_USER_INFO;
1891
1892 /* user-user */
1893 if (user->fields & MNCC_F_USERUSER)
1894 gsm48_encode_useruser(msg, 1, &user->useruser);
1895 /* more data */
1896 if (user->more)
1897 gsm48_encode_more(msg);
1898
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001899 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001900}
1901
1902static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1903{
1904 struct gsm48_hdr *gh = msgb_l3(msg);
1905 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1906 struct tlv_parsed tp;
1907 struct gsm_mncc user;
1908
1909 memset(&user, 0, sizeof(struct gsm_mncc));
1910 user.callref = trans->callref;
1911 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1912 /* user-user */
1913 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1914 user.fields |= MNCC_F_USERUSER;
1915 gsm48_decode_useruser(&user.useruser,
1916 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1917 }
1918 /* more data */
1919 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1920 user.more = 1;
1921
1922 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1923}
1924
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001925static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1926 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001927 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001928{
1929 uint8_t data[sizeof(struct gsm_mncc)];
1930 struct gsm_mncc_rtp *rtp;
1931
1932 memset(&data, 0, sizeof(data));
1933 rtp = (struct gsm_mncc_rtp *) &data[0];
1934
1935 rtp->callref = callref;
1936 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001937 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001938 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1939 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001940 }
Harald Welte27989d42018-06-21 20:39:20 +02001941 rtp->payload_type = payload_type;
1942 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001943 if (sdp)
1944 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001945 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001946}
1947
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001948static 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 +02001949{
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001950 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001951}
1952
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001953static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001954{
1955 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001956
1957 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001958 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001959 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001960 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001961 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001962 return -EIO;
1963 }
1964 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001965 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001966 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001967 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001968 return 0;
1969 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001970 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001971
Harald Welte27989d42018-06-21 20:39:20 +02001972 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001973 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001974}
1975
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001976int cc_on_cn_local_rtp_port_known(struct gsm_trans *cc_trans)
1977{
1978 /* Depending on MO or MT call, dispatch the event differently */
1979 switch (cc_trans->cc.state) {
1980 case GSM_CSTATE_INITIATED:
1981 if (cc_trans->cc.msg.msg_type != MNCC_SETUP_IND) {
1982 LOG_TRANS(cc_trans, LOGL_ERROR, "Assuming MO call, expected MNCC_SETUP_IND to be prepared\n");
1983 return -EINVAL;
1984 }
1985 /* This is the MO call leg, waiting for a CN RTP be able to send initial MNCC_SETUP_IND. */
1986 gsm48_cc_rx_setup_cn_local_rtp_port_known(cc_trans);
1987 return 0;
1988
1989 case GSM_CSTATE_MO_TERM_CALL_CONF:
1990 /* This is the MT call leg, waiting for a CN RTP to be able to send MNCC_CALL_CONF_IND. */
1991 return gsm48_cc_mt_rtp_port_and_codec_known(cc_trans);
1992
1993 default:
1994 LOG_TRANS(cc_trans, LOGL_ERROR, "CN RTP address available, but in unexpected state %d\n",
1995 cc_trans->cc.state);
1996 return -EINVAL;
1997 }
1998}
1999
2000int cc_on_assignment_done(struct gsm_trans *trans)
2001{
2002 struct msc_a *msc_a = trans->msc_a;
2003
2004 switch (trans->cc.state) {
2005 case GSM_CSTATE_INITIATED:
2006 case GSM_CSTATE_MO_CALL_PROC:
2007 /* MO call */
2008 break;
2009
2010 case GSM_CSTATE_CALL_RECEIVED:
2011 case GSM_CSTATE_MO_TERM_CALL_CONF:
2012 /* MT call */
2013 break;
2014
2015 case GSM_CSTATE_ACTIVE:
2016 /* already active. MNCC finished before Abis completed the Assignment. */
2017 break;
2018
2019 default:
2020 LOG_TRANS(trans, LOGL_ERROR, "Assignment done in unexpected CC state: %d\n", trans->cc.state);
2021 return -EINVAL;
2022 }
2023
2024 if (!call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN)) {
2025 LOG_TRANS(trans, LOGL_DEBUG,
2026 "Assignment complete, but still waiting for the CRCX OK on the CN side RTP\n");
2027 return 0;
2028 }
2029 return gsm48_tch_rtp_create(trans);
2030}
2031
Harald Welte27989d42018-06-21 20:39:20 +02002032/* Trigger TCH_RTP_CREATE acknowledgement */
2033int gsm48_tch_rtp_create(struct gsm_trans *trans)
2034{
2035 /* This function is called as soon as the port, on which the
2036 * mgcp-gw expects the incoming RTP stream from the remote
2037 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002038 struct msc_a *msc_a = trans->msc_a;
2039 struct gsm_network *net = msc_a_net(msc_a);
2040 struct call_leg *cl = msc_a->cc.call_leg;
2041 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002042 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002043 int mncc_payload_msg_type;
2044 struct sdp_audio_codec *codec;
Neels Hofmeyra001a702022-10-31 17:57:30 +01002045 const struct codec_mapping *m;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002046 struct sdp_audio_codecs *codecs;
Harald Welte27989d42018-06-21 20:39:20 +02002047
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002048 if (!rtp_cn) {
2049 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
2050 return -EINVAL;
2051 }
2052
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002053 codec_filter_run(&trans->cc.codecs);
2054 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
2055 codecs = &trans->cc.codecs.result.audio_codecs;
2056 if (!codecs->count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002057 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002058 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002059 return -EINVAL;
2060 }
2061
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002062 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
2063 codec = &codecs->codec[0];
2064 m = codec_mapping_by_subtype_name(codec->subtype_name);
2065 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02002066
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002067 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
2068 if (!rtp_cn_local) {
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002069 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 +01002070 return -EINVAL;
2071 }
2072
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002073 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002074 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02002075}
2076
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002077static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002078{
2079 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002080 struct call_leg *cl;
2081 struct rtp_stream *rtps;
Philipp Maier8ad3dac2018-08-07 13:00:14 +02002082
Harald Welte27989d42018-06-21 20:39:20 +02002083 /* Find callref */
2084 trans = trans_find_by_callref(net, rtp->callref);
2085 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002086 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002087 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02002088 return -EIO;
2089 }
2090 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002091 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002092 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002093 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002094 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02002095 }
2096
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002097 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002098
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002099 cl = trans->msc_a->cc.call_leg;
2100 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
2101
2102 if (!rtps) {
2103 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
2104 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2105 return -EINVAL;
2106 }
2107
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002108 rx_mncc_sdp(trans, rtp->msg_type, rtp->sdp);
2109 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
2110
2111 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
2112 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
2113 struct osmo_sockaddr_str rtp_addr;
2114 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
2115 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
2116 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2117 return -EINVAL;
2118 }
2119 rtp_stream_set_remote_addr(rtps, &rtp_addr);
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002120 }
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002121
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002122 rtp_stream_commit(rtps);
2123 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02002124}
2125
2126static struct downstate {
2127 uint32_t states;
2128 int type;
2129 int (*rout) (struct gsm_trans *trans, void *arg);
2130} downstatelist[] = {
2131 /* mobile originating call establishment */
2132 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2133 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2134 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2135 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2136 {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 */
2137 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2138 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2139 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2140 /* mobile terminating call establishment */
2141 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2142 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2143 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2144 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2145 /* signalling during call */
2146 {SBIT(GSM_CSTATE_ACTIVE),
2147 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2148 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2149 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2150 {ALL_STATES,
2151 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2152 {ALL_STATES,
2153 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2154 {ALL_STATES,
2155 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2156 {SBIT(GSM_CSTATE_ACTIVE),
2157 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2158 {SBIT(GSM_CSTATE_ACTIVE),
2159 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2160 {SBIT(GSM_CSTATE_ACTIVE),
2161 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2162 {SBIT(GSM_CSTATE_ACTIVE),
2163 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2164 {SBIT(GSM_CSTATE_ACTIVE),
2165 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2166 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2167 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2168 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2169 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2170 {SBIT(GSM_CSTATE_ACTIVE),
2171 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2172 /* clearing */
2173 {SBIT(GSM_CSTATE_INITIATED),
2174 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2175 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2176 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2177 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2178 MNCC_REL_REQ, gsm48_cc_tx_release},
2179};
2180
2181#define DOWNSLLEN \
2182 (sizeof(downstatelist) / sizeof(struct downstate))
2183
2184
Philipp Maiercd64af72019-08-01 09:46:40 +02002185static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002186{
2187 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002188 struct msc_a *msc_a = NULL;
2189 struct gsm_trans *trans = NULL;
2190 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002191
Harald Welte27989d42018-06-21 20:39:20 +02002192 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002193 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002194 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002195 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002196 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002197 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002198 return rc;
2199 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002200 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002201 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002202 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002203 case MNCC_RTP_FREE:
2204 /* unused right now */
2205 return -EIO;
2206
2207 case MNCC_FRAME_DROP:
2208 case MNCC_FRAME_RECV:
2209 case GSM_TCHF_FRAME:
2210 case GSM_TCHF_FRAME_EFR:
2211 case GSM_TCHH_FRAME:
2212 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002213 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002214 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002215 return -ENOTSUP;
2216 }
2217
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002218 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002219
2220 /* Find callref */
2221 trans = trans_find_by_callref(net, data->callref);
2222
2223 /* Callref unknown */
2224 if (!trans) {
2225 struct vlr_subscr *vsub;
2226
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002227 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002228 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002229 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002230 /* Invalid call reference */
2231 return mncc_release_ind(net, NULL, data->callref,
2232 GSM48_CAUSE_LOC_PRN_S_LU,
2233 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2234 }
2235 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002236 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002237 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002238 /* Invalid number */
2239 return mncc_release_ind(net, NULL, data->callref,
2240 GSM48_CAUSE_LOC_PRN_S_LU,
2241 GSM48_CC_CAUSE_INV_NR_FORMAT);
2242 }
2243 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002244 if (data->called.number[0]) {
2245 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2246 if (!vsub)
2247 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002248 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002249 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002250 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002251 if (!vsub)
2252 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002253 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002254 }
2255 if (!vsub)
2256 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002257 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002258 /* update the subscriber we deal with */
2259 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2260
Harald Welte27989d42018-06-21 20:39:20 +02002261 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002262 if (!vsub->lu_complete) {
2263 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002264 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002265 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002266 /* Temporarily out of order */
2267 return mncc_release_ind(net, NULL, data->callref,
2268 GSM48_CAUSE_LOC_PRN_S_LU,
2269 GSM48_CC_CAUSE_DEST_OOO);
2270 }
Keith Whyte991bb422019-08-08 15:43:40 +02002271
2272 /* Find valid conn */
2273 msc_a = msc_a_for_vsub(vsub, true);
2274
2275 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2276 if (!net->call_waiting && msc_a) {
2277 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2278 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2279 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2280 "rx '%s' for subscriber %s with trans state (%s)"
2281 " rejecting with USER_BUSY\n",
2282 get_mncc_name(msg->msg_type), data->called.number,
2283 gsm48_cc_state_name(existing_cc_trans->cc.state));
2284 return mncc_release_ind(net, NULL, data->callref,
2285 GSM48_CAUSE_LOC_PRN_S_LU,
2286 GSM48_CC_CAUSE_USER_BUSY);
2287 }
2288 }
2289
Harald Welte27989d42018-06-21 20:39:20 +02002290 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002291 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002292 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002293 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002294 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002295 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002296 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002297 mncc_release_ind(net, NULL, data->callref,
2298 GSM48_CAUSE_LOC_PRN_S_LU,
2299 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2300 return -ENOMEM;
2301 }
2302
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002303 /* Remember remote SDP, if any */
2304 rx_mncc_sdp(trans, data->msg_type, data->sdp);
2305
Harald Welte27989d42018-06-21 20:39:20 +02002306 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002307 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002308 /* This condition will return before the common logging of the received MNCC message below, so
2309 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002310 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002311
Harald Welte27989d42018-06-21 20:39:20 +02002312 /* store setup information until paging succeeds */
2313 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2314
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002315 /* Request a channel. If Paging already started, paging_request_start() will append the new
2316 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002317 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2318 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002319 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002320 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002321 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002322 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002323 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002324 return 0;
2325 }
2326
2327 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002328 trans->msc_a = msc_a;
2329 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002330 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002331 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002332 } else {
2333 /* update the subscriber we deal with */
2334 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2335 }
2336
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002337 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002338
Philipp Maier9ca7b312018-10-10 17:00:49 +02002339 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002340 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002342 if (trans->msc_a)
2343 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002344
2345 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002346 if (!msc_a) {
2347 struct gsm_mncc rel = {
2348 .callref = data->callref,
2349 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002350 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002351 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2352 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002353 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002354 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2355 else
2356 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2357 trans->callref = 0;
2358 trans_free(trans);
2359 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002360 }
2361
2362 /* Find function for current state and message */
2363 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002364 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002365 && ((1 << trans->cc.state) & downstatelist[i].states))
2366 break;
2367 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002368 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002369 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002370 return 0;
2371 }
2372
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002373 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002374
2375 return rc;
2376}
2377
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002378struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2379{
2380 uint32_t callref;
2381
2382 switch (msg->msg_type) {
2383 case MNCC_BRIDGE:
2384 callref = msg->bridge.callref[0];
2385 break;
2386 case MNCC_RTP_CREATE:
2387 case MNCC_RTP_CONNECT:
2388 callref = msg->rtp.callref;
2389 break;
2390
2391 case MNCC_RTP_FREE:
2392 case MNCC_FRAME_DROP:
2393 case MNCC_FRAME_RECV:
2394 case GSM_TCHF_FRAME:
2395 case GSM_TCHF_FRAME_EFR:
2396 case GSM_TCHH_FRAME:
2397 case GSM_TCH_FRAME_AMR:
2398 return NULL;
2399
2400 default:
2401 callref = msg->signal.callref;
2402 break;
2403 }
2404
2405 return mncc_call_find_by_callref(callref);
2406}
2407
2408/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002409int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002410{
2411 const union mncc_msg *msg = arg;
2412 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002413
2414 if (msg->msg_type == MNCC_SETUP_REQ) {
2415 /* Incoming call to forward for inter-MSC Handover? */
2416 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2417 if (mncc_call)
2418 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2419 "Incoming call matches pending inter-MSC Handover Number\n");
2420 }
2421 if (!mncc_call) {
2422 /* Find already active MNCC FSM for this callref.
2423 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2424 * MNCC<->GSM-CC call handling. */
2425 mncc_call = mncc_find_by_callref_from_msg(msg);
2426 }
2427 if (mncc_call) {
2428 mncc_call_rx(mncc_call, msg);
2429 return 0;
2430 }
2431
2432 /* None of the above? Then it must be a normal GSM CC call related message. */
2433 return mncc_tx_to_gsm_cc(net, msg);
2434}
Harald Welte27989d42018-06-21 20:39:20 +02002435
2436static struct datastate {
2437 uint32_t states;
2438 int type;
2439 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2440} datastatelist[] = {
2441 /* mobile originating call establishment */
2442 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2443 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2444 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2445 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2446 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2447 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2448 /* mobile terminating call establishment */
2449 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2450 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2451 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2452 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2453 {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 */
2454 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2455 /* signalling during call */
2456 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2457 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2458 {SBIT(GSM_CSTATE_ACTIVE),
2459 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2460 {ALL_STATES,
2461 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2462 {ALL_STATES,
2463 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2464 {ALL_STATES,
2465 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2466 {SBIT(GSM_CSTATE_ACTIVE),
2467 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2468 {SBIT(GSM_CSTATE_ACTIVE),
2469 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2470 {SBIT(GSM_CSTATE_ACTIVE),
2471 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2472 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2473 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2474 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2475 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2476 {SBIT(GSM_CSTATE_ACTIVE),
2477 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2478 /* clearing */
2479 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2480 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2481 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2482 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2483 {ALL_STATES, /* 5.4.3.4 */
2484 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2485};
2486
2487#define DATASLLEN \
2488 (sizeof(datastatelist) / sizeof(struct datastate))
2489
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002490int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002491{
2492 struct gsm48_hdr *gh = msgb_l3(msg);
2493 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2494 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2495 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002496 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2497 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002498 int i, rc = 0;
2499
2500 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002501 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002502 return -EINVAL;
2503 }
2504
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002505 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002506 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002507 return -EINVAL;
2508 }
2509
2510 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002511 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002512
Harald Welte27989d42018-06-21 20:39:20 +02002513 /* Create transaction */
2514 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002515 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002516 trans = trans_alloc(net, vsub,
2517 TRANS_CC,
2518 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002519 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002520 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002521 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002522 GSM48_PDISC_CC | (transaction_id << 4),
2523 GSM48_MT_CC_RELEASE_COMPL);
2524 return -ENOMEM;
2525 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002526 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2527 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2528 trans_free(trans);
2529 return -EINVAL;
2530 }
2531
Harald Welte27989d42018-06-21 20:39:20 +02002532 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002533 msc_a_get(msc_a, MSC_A_USE_CC);
2534 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002535 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002536
2537 /* An earlier CM Service Request for this CC message now has concluded */
2538 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2539 LOG_MSC_A(msc_a, LOGL_ERROR,
2540 "Creating new CC transaction without prior CM Service Request\n");
2541 else
2542 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002543 }
2544
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002545 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2546 gsm48_cc_state_name(trans->cc.state));
2547
Harald Welte27989d42018-06-21 20:39:20 +02002548 /* find function for current state and message */
2549 for (i = 0; i < DATASLLEN; i++)
2550 if ((msg_type == datastatelist[i].type)
2551 && ((1 << trans->cc.state) & datastatelist[i].states))
2552 break;
2553 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002554 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002555
2556 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2557 * transaction right away. */
2558 if (trans->cc.state == GSM_CSTATE_NULL) {
2559 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2560 " -- disarding new CC transaction right away\n");
2561 trans_free(trans);
2562 }
Harald Welte27989d42018-06-21 20:39:20 +02002563 return 0;
2564 }
2565
2566 assert(trans->vsub);
2567
2568 rc = datastatelist[i].rout(trans, msg);
2569
Harald Welte27989d42018-06-21 20:39:20 +02002570 return rc;
2571}