blob: 083e2d87a35c1d094325b1d36bd84f05e3fb9ba3 [file] [log] [blame]
Harald Welte27989d42018-06-21 20:39:20 +02001/* GSM Mobile Radio Interface Layer 3 Call Control */
2
3/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdbool.h>
27#include <errno.h>
28#include <time.h>
29#include <netinet/in.h>
30#include <regex.h>
31#include <sys/types.h>
32
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +020033#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
34
Harald Welte27989d42018-06-21 20:39:20 +020035#include <osmocom/msc/db.h>
36#include <osmocom/msc/debug.h>
37#include <osmocom/msc/gsm_data.h>
38#include <osmocom/msc/gsm_subscriber.h>
39#include <osmocom/msc/gsm_04_11.h>
40#include <osmocom/msc/gsm_04_08.h>
41#include <osmocom/msc/gsm_04_80.h>
42#include <osmocom/msc/gsm_04_14.h>
43#include <osmocom/msc/gsm_09_11.h>
44#include <osmocom/msc/signal.h>
45#include <osmocom/msc/transaction.h>
46#include <osmocom/msc/silent_call.h>
Harald Welte27989d42018-06-21 20:39:20 +020047#include <osmocom/msc/mncc_int.h>
48#include <osmocom/abis/e1_input.h>
49#include <osmocom/core/bitvec.h>
50#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010051#include <osmocom/msc/msub.h>
52#include <osmocom/msc/msc_a.h>
53#include <osmocom/msc/paging.h>
54#include <osmocom/msc/call_leg.h>
55#include <osmocom/msc/rtp_stream.h>
56#include <osmocom/msc/mncc_call.h>
57#include <osmocom/msc/msc_t.h>
Neels Hofmeyr58f40882023-03-08 04:04:27 +010058#include <osmocom/msc/sdp_msg.h>
Neels Hofmeyra001a702022-10-31 17:57:30 +010059#include <osmocom/msc/codec_mapping.h>
Harald Welte27989d42018-06-21 20:39:20 +020060
61#include <osmocom/gsm/gsm48.h>
62#include <osmocom/gsm/gsm0480.h>
63#include <osmocom/gsm/gsm_utils.h>
64#include <osmocom/gsm/protocol/gsm_04_08.h>
65#include <osmocom/core/msgb.h>
66#include <osmocom/core/talloc.h>
67#include <osmocom/core/utils.h>
68#include <osmocom/core/byteswap.h>
69#include <osmocom/gsm/tlv.h>
70#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020071
72#include <assert.h>
73
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010074static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
75static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
76static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
77
78static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
79{
80 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
81 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
82 OMSC_LINKID_CB(msg) = trans->dlci;
83
84 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
85}
86
87uint32_t msc_cc_next_outgoing_callref() {
88 static uint32_t last_callref = 0x80000000;
89 last_callref++;
90 if (last_callref < 0x80000001)
91 last_callref = 0x80000001;
92 return last_callref;
93}
Harald Welte27989d42018-06-21 20:39:20 +020094
Philipp Maier9ca7b312018-10-10 17:00:49 +020095static void gsm48_cc_guard_timeout(void *arg)
96{
97 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010098 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020099 trans_free(trans);
100 return;
101}
102
103static void gsm48_stop_guard_timer(struct gsm_trans *trans)
104{
105 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100106 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200107 osmo_timer_del(&trans->cc.timer_guard);
108 }
109}
110
111static void gsm48_start_guard_timer(struct gsm_trans *trans)
112{
113 /* NOTE: The purpose of this timer is to prevent the cc state machine
114 * from hanging in cases where mncc, gsm48 or both become unresponsive
115 * for some reason. The timer is started initially with the setup from
116 * the gsm48 side and then re-started with every incoming mncc message.
117 * Once the mncc state reaches its active state the timer is stopped.
118 * So if the cc state machine does not show any activity for an
119 * extended amount of time during call setup or teardown the guard
120 * timer will time out and hard-clear the connection. */
121 if (osmo_timer_pending(&trans->cc.timer_guard))
122 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100123 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200124 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
125 osmo_timer_schedule(&trans->cc.timer_guard,
126 trans->net->mncc_guard_timeout, 0);
127}
Harald Welte27989d42018-06-21 20:39:20 +0200128
129/* Call Control */
130
131void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
132{
133 net->mncc_recv(net, msg);
134}
135
136int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
137{
138 struct gsm48_hdr *gh;
139 struct msgb *ss_notify;
140
141 ss_notify = gsm0480_create_notifySS(message);
142 if (!ss_notify)
143 return -1;
144
145 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
146 uint8_t *data = msgb_push(ss_notify, 1);
147 data[0] = ss_notify->len - 1;
148 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
149 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100150 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200151}
152
153/* FIXME: this count_statistics is a state machine behaviour. we should convert
154 * the complete call control into a state machine. Afterwards we can move this
155 * code into state transitions.
156 */
157static void count_statistics(struct gsm_trans *trans, int new_state)
158{
159 int old_state = trans->cc.state;
160 struct rate_ctr_group *msc = trans->net->msc_ctrs;
161
162 if (old_state == new_state)
163 return;
164
165 /* state incoming */
166 switch (new_state) {
167 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200168 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
169 1);
170 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200171 break;
172 }
173
174 /* state outgoing */
175 switch (old_state) {
176 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200177 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
178 1);
Harald Welte27989d42018-06-21 20:39:20 +0200179 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
180 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200181 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200182 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200183 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200184 break;
185 }
186}
187
Harald Welte27989d42018-06-21 20:39:20 +0200188static void new_cc_state(struct gsm_trans *trans, int state)
189{
190 if (state > 31 || state < 0)
191 return;
192
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100193 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
194 gsm48_cc_state_name(trans->cc.state),
195 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200196
197 count_statistics(trans, state);
198 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200199
200 /* Stop the guard timer when a call reaches the active state */
201 if (state == GSM_CSTATE_ACTIVE)
202 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200203}
204
205static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
206{
207 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
208 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
209 uint8_t *cause, *call_state;
210
211 gh->msg_type = GSM48_MT_CC_STATUS;
212
213 cause = msgb_put(msg, 3);
214 cause[0] = 2;
215 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
216 cause[2] = 0x80 | 30; /* response to status inquiry */
217
218 call_state = msgb_put(msg, 1);
219 call_state[0] = 0xc0 | 0x00;
220
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100221 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200222}
223
224static void gsm48_stop_cc_timer(struct gsm_trans *trans)
225{
226 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100227 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200228 osmo_timer_del(&trans->cc.timer);
229 trans->cc.Tcurrent = 0;
230 }
231}
232
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100233/* Log the MNCC tx and rx events.
234 * Depending on msg_type, also log whether RTP information is passed on.
235 * (This is particularly interesting for the doc/sequence_charts/msc_log_to_ladder.py)
236 */
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200237#define log_mncc_rx_tx(ARGS...) _log_mncc_rx_tx(__FILE__, __LINE__, ##ARGS)
238static void _log_mncc_rx_tx(const char *file, int line,
239 struct gsm_trans *trans, const char *rx_tx, const union mncc_msg *mncc)
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100240{
241 const char *sdp = NULL;
242 struct sdp_msg sdp_msg = {};
243 struct osmo_sockaddr addr = {};
244
245 if (!log_check_level(DMNCC, LOGL_DEBUG))
246 return;
247
248 switch (mncc->msg_type) {
249 case MNCC_RTP_CREATE:
250 case MNCC_RTP_CONNECT:
251 addr = (struct osmo_sockaddr){ .u.sas = mncc->rtp.addr };
252 sdp = mncc->rtp.sdp;
253 break;
254
255 case MNCC_SETUP_IND:
256 case MNCC_SETUP_REQ:
257 case MNCC_SETUP_COMPL_IND:
258 case MNCC_SETUP_COMPL_REQ:
259 case MNCC_SETUP_RSP:
260 case MNCC_SETUP_CNF:
261 case MNCC_CALL_CONF_IND:
262 case MNCC_CALL_PROC_REQ:
263 case MNCC_ALERT_IND:
264 case MNCC_ALERT_REQ:
265 sdp = mncc->signal.sdp;
266 break;
267
268 default:
269 break;
270 }
271
272 if (sdp && sdp[0] && (sdp_msg_from_sdp_str(&sdp_msg, sdp) == 0)) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200273 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
274 rx_tx,
275 get_mncc_name(mncc->msg_type),
276 sdp_msg_to_str(&sdp_msg));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100277 return;
278 }
279
280 if (osmo_sockaddr_is_any(&addr) == 0) {
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200281 LOG_TRANS_CAT_SRC(trans, DMNCC, LOGL_DEBUG, file, line, "%s %s (RTP=%s)\n",
282 rx_tx,
283 get_mncc_name(mncc->msg_type),
284 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100285 return;
286 }
287
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200288 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 +0100289}
290
Neels Hofmeyr1c065072022-08-07 02:43:15 +0200291#define mncc_recvmsg(ARGS...) _mncc_recvmsg(__FILE__, __LINE__, ##ARGS)
292static int _mncc_recvmsg(const char *file, int line,
293 struct gsm_network *net, struct gsm_trans *trans, int msg_type, struct gsm_mncc *mncc)
Harald Welte27989d42018-06-21 20:39:20 +0200294{
295 struct msgb *msg;
296 unsigned char *data;
297
Harald Welte27989d42018-06-21 20:39:20 +0200298 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100299 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200300
301 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
302 if (!msg)
303 return -ENOMEM;
304
305 data = msgb_put(msg, sizeof(struct gsm_mncc));
306 memcpy(data, mncc, sizeof(struct gsm_mncc));
307
308 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200309 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
310 if (trans)
311 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200312
313 return 0;
314}
315
316int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
317 uint32_t callref, int location, int value)
318{
319 struct gsm_mncc rel;
320
321 memset(&rel, 0, sizeof(rel));
322 rel.callref = callref;
323 mncc_set_cause(&rel, location, value);
324 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
325 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
326 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
327}
328
329/* Call Control Specific transaction release.
330 * gets called by trans_free, DO NOT CALL YOURSELF! */
331void _gsm48_cc_trans_free(struct gsm_trans *trans)
332{
333 gsm48_stop_cc_timer(trans);
334
Harald Welte27989d42018-06-21 20:39:20 +0200335 /* send release to L4, if callref still exists */
336 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100337 /* Send MNCC REL.ind (cause='Resource unavailable') */
338 if (trans->cc.mncc_initiated) {
339 mncc_release_ind(trans->net, trans, trans->callref,
340 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100341 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
342 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100343 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
344 }
345
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100346 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
347 * CC Release to the MS if it gets freed here. Hack it to do so. */
348 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
349 struct gsm_mncc rel = {};
350 rel.callref = trans->callref;
351 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
352 gsm48_cc_tx_release(trans, &rel);
353 }
Harald Welte27989d42018-06-21 20:39:20 +0200354 /* This is a final freeing of the transaction. The MNCC release may have triggered the
355 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
356 gsm48_stop_cc_timer(trans);
357 }
358 if (trans->cc.state != GSM_CSTATE_NULL)
359 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200360
361 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100362
363 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
364 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200365}
366
Harald Welte27989d42018-06-21 20:39:20 +0200367/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100368static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200369{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100370 if (trans->msc_a) {
371 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
372 "Handle paging error: transaction already associated with subscriber,"
373 " apparently it was already handled. Skip.\n");
374 return;
Harald Welte27989d42018-06-21 20:39:20 +0200375 }
376
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100377 if (msc_a) {
378 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
379 /* Assign conn */
380 msc_a_get(msc_a, MSC_A_USE_CC);
381 trans->msc_a = msc_a;
382 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200383
384 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300385 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200386 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300387 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
388 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
389 &trans->cc.msg.gcr[0],
390 sizeof(trans->cc.msg.gcr));
391 if (rc < 0)
392 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
393 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200394 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200395 }
396
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100397 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
398 /* send SETUP request to called party */
399 gsm48_cc_tx_setup(trans, &trans->cc.msg);
400 } else {
401 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
402 /* Temporarily out of order */
403 mncc_release_ind(trans->net, trans,
404 trans->callref,
405 GSM48_CAUSE_LOC_PRN_S_LU,
406 GSM48_CC_CAUSE_DEST_OOO);
407 trans->callref = 0;
408 trans->paging_request = NULL;
409 trans_free(trans);
410 }
Harald Welte27989d42018-06-21 20:39:20 +0200411}
412
413/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100414static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200415{
416 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
417 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100418 struct call_leg *cl1;
419 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200420
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100421 if (!trans1 || !trans2) {
422 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200423 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100424 }
Harald Welte27989d42018-06-21 20:39:20 +0200425
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100426 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100427 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
428 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 +0200429 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100430 }
431
432 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
433 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200434
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100435 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
436 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
437 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
438 * If we can't, then we must give up. */
439 cl1 = trans1->msc_a->cc.call_leg;
440 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200441
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100442 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200443}
444
445static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
446{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100447 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200448 return gsm48_cc_tx_status(trans, msg);
449}
450
Harald Welte27989d42018-06-21 20:39:20 +0200451static void gsm48_cc_timeout(void *arg)
452{
453 struct gsm_trans *trans = arg;
454 int disconnect = 0, release = 0;
455 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
456 int mo_location = GSM48_CAUSE_LOC_USER;
457 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
458 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
459 struct gsm_mncc mo_rel, l4_rel;
460
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200461 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
462
Harald Welte27989d42018-06-21 20:39:20 +0200463 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
464 mo_rel.callref = trans->callref;
465 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
466 l4_rel.callref = trans->callref;
467
468 switch(trans->cc.Tcurrent) {
469 case 0x303:
470 release = 1;
471 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
472 break;
473 case 0x310:
474 disconnect = 1;
475 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
476 break;
477 case 0x313:
478 disconnect = 1;
479 /* unknown, did not find it in the specs */
480 break;
481 case 0x301:
482 disconnect = 1;
483 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
484 break;
485 case 0x308:
486 if (!trans->cc.T308_second) {
487 /* restart T308 a second time */
488 gsm48_cc_tx_release(trans, &trans->cc.msg);
489 trans->cc.T308_second = 1;
490 break; /* stay in release state */
491 }
492 trans_free(trans);
493 return;
494 case 0x306:
495 release = 1;
496 mo_cause = trans->cc.msg.cause.value;
497 mo_location = trans->cc.msg.cause.location;
498 break;
499 case 0x323:
500 disconnect = 1;
501 break;
502 default:
503 release = 1;
504 }
505
506 if (release && trans->callref) {
507 /* process release towards layer 4 */
508 mncc_release_ind(trans->net, trans, trans->callref,
509 l4_location, l4_cause);
510 trans->callref = 0;
511 }
512
513 if (disconnect && trans->callref) {
514 /* process disconnect towards layer 4 */
515 mncc_set_cause(&l4_rel, l4_location, l4_cause);
516 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
517 }
518
519 /* process disconnect towards mobile station */
520 if (disconnect || release) {
521 mncc_set_cause(&mo_rel, mo_location, mo_cause);
522 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
523 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
524 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
525 mo_rel.cause.diag_len = 3;
526
527 if (disconnect)
528 gsm48_cc_tx_disconnect(trans, &mo_rel);
529 if (release)
530 gsm48_cc_tx_release(trans, &mo_rel);
531 }
532
533}
534
535/* disconnect both calls from the bridge */
536static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100537 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200538{
539 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
540 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
541 struct gsm_mncc mx_rel;
542 if (!trans0 || !trans1)
543 return;
544
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100545 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
546 trans0->callref, trans1->callref, strerror(err));
547 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200548 trans0->callref, trans1->callref, strerror(err));
549
550 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
551 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
552 GSM48_CC_CAUSE_CHAN_UNACCEPT);
553
554 mx_rel.callref = trans0->callref;
555 gsm48_cc_tx_disconnect(trans0, &mx_rel);
556
557 mx_rel.callref = trans1->callref;
558 gsm48_cc_tx_disconnect(trans1, &mx_rel);
559}
560
561static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
562 int sec, int micro)
563{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100564 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200565 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
566 osmo_timer_schedule(&trans->cc.timer, sec, micro);
567 trans->cc.Tcurrent = current;
568}
569
570static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
571{
572 struct gsm48_hdr *gh = msgb_l3(msg);
573 uint8_t msg_type = gsm48_hdr_msg_type(gh);
574 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
575 struct tlv_parsed tp;
576 struct gsm_mncc setup;
577
Philipp Maier9ca7b312018-10-10 17:00:49 +0200578 gsm48_start_guard_timer(trans);
579
Harald Welte27989d42018-06-21 20:39:20 +0200580 memset(&setup, 0, sizeof(struct gsm_mncc));
581 setup.callref = trans->callref;
582
Keith Whytea1a70be2021-05-16 02:59:52 +0200583 /* New Global Call Reference */
584 if (!trans->cc.lcls)
585 trans->cc.lcls = trans_lcls_compose(trans, true);
586
587 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300588 if (trans->cc.lcls) {
589 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
590 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
591 int rc;
592
593 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
594 memcpy(&setup.gcr[0], gcr_msg->data, rc);
595 setup.fields |= MNCC_F_GCR;
596 } else
597 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
598 msgb_free(gcr_msg);
599 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200600
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100601 OSMO_ASSERT(trans->msc_a);
602
Harald Welte27989d42018-06-21 20:39:20 +0200603 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
604 /* emergency setup is identified by msg_type */
605 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
606 setup.fields |= MNCC_F_EMERGENCY;
607 setup.emergency = 1;
608 /* use destination number as configured by user (if any) */
609 if (trans->net->emergency.route_to_msisdn) {
610 setup.fields |= MNCC_F_CALLED;
611 setup.called.type = 0; /* unknown */
612 setup.called.plan = 0; /* unknown */
613 OSMO_STRLCPY_ARRAY(setup.called.number,
614 trans->net->emergency.route_to_msisdn);
615 }
616 }
617
618 /* use subscriber as calling party number */
619 setup.fields |= MNCC_F_CALLING;
620 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
621 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
622
623 /* bearer capability */
624 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
625 setup.fields |= MNCC_F_BEARER_CAP;
626 gsm48_decode_bearer_cap(&setup.bearer_cap,
627 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
628
629 /* Create a copy of the bearer capability
630 * in the transaction struct, so we can use
631 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100632 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200633 sizeof(trans->bearer_cap));
634 }
635 /* facility */
636 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
637 setup.fields |= MNCC_F_FACILITY;
638 gsm48_decode_facility(&setup.facility,
639 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
640 }
641 /* called party bcd number */
642 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
643 setup.fields |= MNCC_F_CALLED;
644 gsm48_decode_called(&setup.called,
645 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
646 }
647 /* user-user */
648 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
649 setup.fields |= MNCC_F_USERUSER;
650 gsm48_decode_useruser(&setup.useruser,
651 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
652 }
653 /* ss-version */
654 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
655 setup.fields |= MNCC_F_SSVERSION;
656 gsm48_decode_ssversion(&setup.ssversion,
657 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
658 }
659 /* CLIR suppression */
660 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
661 setup.clir.sup = 1;
662 /* CLIR invocation */
663 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
664 setup.clir.inv = 1;
665 /* cc cap */
666 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
667 setup.fields |= MNCC_F_CCCAP;
668 gsm48_decode_cccap(&setup.cccap,
669 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
670 }
671
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100672 /* MO call leg starting, gather all codec information so far known: */
673 codec_filter_init(&trans->cc.codecs);
674 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
675 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
676 if (setup.fields & MNCC_F_BEARER_CAP)
677 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
678 codec_filter_run(&trans->cc.codecs);
679
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100680 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
681 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100682 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200683
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200684 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200685
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100686 new_cc_state(trans, GSM_CSTATE_INITIATED);
687
688 /* To complete the MNCC_SETUP_IND, we need to provide an RTP address and port. First instruct the MGW to create
689 * a CN-side RTP conn, and continue with MNCC_SETUP_IND once that is done. Leave trans.cc in GSM_CSTATE_NULL and
690 * note down the msg_type to indicate that we indeed composed an MNCC_SETUP_IND for later. */
691 setup.msg_type = MNCC_SETUP_IND;
692 trans->cc.msg = setup;
693 return msc_a_try_call_assignment(trans);
694 /* continue in gsm48_cc_rx_setup_cn_local_rtp_port_known() */
695}
696
697/* Callback for MNCC_SETUP_IND waiting for the core network RTP port to be established by the MGW (via msc_a) */
698void gsm48_cc_rx_setup_cn_local_rtp_port_known(struct gsm_trans *trans)
699{
700 struct msc_a *msc_a = trans->msc_a;
701 struct gsm_mncc setup = trans->cc.msg;
702 struct osmo_sockaddr_str *rtp_cn_local;
703 struct sdp_msg *sdp;
704 int rc;
705
706 if (trans->cc.state != GSM_CSTATE_INITIATED
707 || setup.msg_type != MNCC_SETUP_IND) {
708 LOG_TRANS(trans, LOGL_ERROR,
709 "Unexpected CC state. Expected GSM_CSTATE_INITIATED and a buffered MNCC_SETUP_IND message,"
710 " found CC state %d and msg_type %s\n",
711 trans->cc.state, get_mncc_name(setup.msg_type));
712 trans->callref = 0;
713 trans_free(trans);
714 return;
715 }
716
717 if (!msc_a) {
718 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
719 trans->callref = 0;
720 trans_free(trans);
721 return;
722 }
723
724 /* 'setup' above has taken the value of trans->cc.msg, we can now clear that. */
725 trans->cc.msg = (struct gsm_mncc){};
726
727 /* Insert the CN side RTP port now available into SDP and compose SDP string */
728 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
729 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
730 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
731 trans_free(trans);
732 return;
733 }
734 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100735
736 sdp = trans->cc.codecs.result.audio_codecs.count ? &trans->cc.codecs.result : NULL;
737 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
738 if (rc >= sizeof(setup.sdp)) {
739 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
740 trans_free(trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100741 return;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100742 }
743
Harald Welte27989d42018-06-21 20:39:20 +0200744 /* indicate setup to MNCC */
745 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte27989d42018-06-21 20:39:20 +0200746}
747
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100748static void rx_mncc_sdp(struct gsm_trans *trans, uint32_t mncc_msg_type, const char *sdp)
749{
750 int rc;
751 if (!sdp[0])
752 return;
753 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, sdp);
754 if (rc)
755 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "rx %s: Failed to parse SDP: %d\n",
756 get_mncc_name(mncc_msg_type), rc);
757}
758
Harald Welte27989d42018-06-21 20:39:20 +0200759static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
760{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100761 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200762 struct gsm48_hdr *gh;
763 struct gsm_mncc *setup = arg;
764 int rc, trans_id;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100765 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200766
767 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
768
769 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700770 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100771 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200772 "This is not allowed!\n");
773 /* Temporarily out of order */
774 rc = mncc_release_ind(trans->net, trans, trans->callref,
775 GSM48_CAUSE_LOC_PRN_S_LU,
776 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
777 trans->callref = 0;
778 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200779 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200780 return rc;
781 }
782
783 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100784 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200785 if (trans_id < 0) {
786 /* no free transaction ID */
787 rc = mncc_release_ind(trans->net, trans, trans->callref,
788 GSM48_CAUSE_LOC_PRN_S_LU,
789 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
790 trans->callref = 0;
791 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200792 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200793 return rc;
794 }
795 trans->transaction_id = trans_id;
796
797 gh->msg_type = GSM48_MT_CC_SETUP;
798
799 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
800
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100801 /* MT call leg is starting. Gather all codecs information so far known.
802 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
803 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
804 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
805 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
806 * Capabilities. */
807 codec_filter_init(&trans->cc.codecs);
808 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
809 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 */
843 /* TODO: we should be able to drop trans->bearer_cap, replaced by the codecs filter. Verify this.
844 * So far let's just store it there like previous code did. */
845 trans->bearer_cap = bearer_cap;
846 /* If no resulting codecs remain, error out. We cannot find a codec that matches both call legs. If the MGW were
847 * able to transcode, we could use non-identical codecs on each conn of the MGW endpoint, but we are aiming for
848 * finding a matching codec. */
849 if (bearer_cap.speech_ver[0] == -1) {
850 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
851 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
852
853 /* incompatible codecs */
854 rc = mncc_release_ind(trans->net, trans, trans->callref,
855 GSM48_CAUSE_LOC_PRN_S_LU,
856 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
857 trans->callref = 0;
858 trans_free(trans);
859 msgb_free(msg);
860 return rc;
861 }
862 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
863
Harald Welte27989d42018-06-21 20:39:20 +0200864 /* facility */
865 if (setup->fields & MNCC_F_FACILITY)
866 gsm48_encode_facility(msg, 0, &setup->facility);
867 /* progress */
868 if (setup->fields & MNCC_F_PROGRESS)
869 gsm48_encode_progress(msg, 0, &setup->progress);
870 /* calling party BCD number */
871 if (setup->fields & MNCC_F_CALLING)
872 gsm48_encode_calling(msg, &setup->calling);
873 /* called party BCD number */
874 if (setup->fields & MNCC_F_CALLED)
875 gsm48_encode_called(msg, &setup->called);
876 /* user-user */
877 if (setup->fields & MNCC_F_USERUSER)
878 gsm48_encode_useruser(msg, 0, &setup->useruser);
879 /* redirecting party BCD number */
880 if (setup->fields & MNCC_F_REDIRECTING)
881 gsm48_encode_redirecting(msg, &setup->redirecting);
882 /* signal */
883 if (setup->fields & MNCC_F_SIGNAL)
884 gsm48_encode_signal(msg, setup->signal);
885
886 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
887
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200888 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200889
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100890 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200891}
892
893static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
894{
895 struct gsm48_hdr *gh = msgb_l3(msg);
896 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
897 struct tlv_parsed tp;
898 struct gsm_mncc call_conf;
899 int rc;
900
901 gsm48_stop_cc_timer(trans);
902 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
903
904 memset(&call_conf, 0, sizeof(struct gsm_mncc));
905 call_conf.callref = trans->callref;
906
907 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
908#if 0
909 /* repeat */
910 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
911 call_conf.repeat = 1;
912 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
913 call_conf.repeat = 2;
914#endif
915 /* bearer capability */
916 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
917 call_conf.fields |= MNCC_F_BEARER_CAP;
918 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
919 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
920
921 /* Create a copy of the bearer capability
922 * in the transaction struct, so we can use
923 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100924 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200925 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +0100926
927 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
928 * Store in codecs filter. */
929 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200930 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100931
Harald Welte27989d42018-06-21 20:39:20 +0200932 /* cause */
933 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
934 call_conf.fields |= MNCC_F_CAUSE;
935 gsm48_decode_cause(&call_conf.cause,
936 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
937 }
938 /* cc cap */
939 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
940 call_conf.fields |= MNCC_F_CCCAP;
941 gsm48_decode_cccap(&call_conf.cccap,
942 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
943 }
944
945 /* IMSI of called subscriber */
946 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
947
Harald Welte27989d42018-06-21 20:39:20 +0200948 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100949 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200950
951 /* don't continue, if there were problems with
952 * the call assignment. */
953 if (rc)
954 return rc;
955
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100956 /* Directly ack with MNCC_CALL_CONF_IND, not yet containing SDP or RTP IP:port information. */
957 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
958 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND, &call_conf);
959}
960
961static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
962 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
963 uint32_t payload_msg_type, const struct sdp_msg *sdp);
964
965static int gsm48_cc_mt_rtp_port_and_codec_known(struct gsm_trans *trans)
966{
967 struct msc_a *msc_a = trans->msc_a;
968 struct osmo_sockaddr_str *rtp_cn_local;
969 struct gsm_mncc_rtp;
970
971 if (!msc_a) {
972 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
973 trans->callref = 0;
974 trans_free(trans);
975 return -EINVAL;
976 }
977
978 /* Insert the CN side RTP port now available into SDP */
979 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
980 if (!rtp_cn_local) {
981 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_RTP_CREATE: no RTP set up for the CN side\n");
982 trans_free(trans);
983 return -EINVAL;
984 }
985 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
986
987 codec_filter_run(&trans->cc.codecs);
988 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
989
990 /* If we haven't completed Assignment yet, don't sent MNCC_RTP_CREATE */
991 if (!sdp_audio_codec_is_set(&trans->cc.codecs.assignment)) {
992 LOG_TRANS(trans, LOGL_DEBUG, "no codec confirmed by Assignment yet\n");
993 return 0;
994 }
995
996 return mncc_recv_rtp(msc_a_net(msc_a), trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, 0, 0,
997 &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +0200998}
999
1000static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
1001{
1002 struct gsm_mncc *proceeding = arg;
1003 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
1004 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1005 int rc;
1006
1007 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1008
1009 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1010
1011 /* bearer capability */
1012 if (proceeding->fields & MNCC_F_BEARER_CAP) {
1013 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
1014 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1015 }
1016 /* facility */
1017 if (proceeding->fields & MNCC_F_FACILITY)
1018 gsm48_encode_facility(msg, 0, &proceeding->facility);
1019 /* progress */
1020 if (proceeding->fields & MNCC_F_PROGRESS)
1021 gsm48_encode_progress(msg, 0, &proceeding->progress);
1022
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001023 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001024 if (rc)
1025 return rc;
1026
1027 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001028 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001029}
1030
1031static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1032{
1033 struct gsm48_hdr *gh = msgb_l3(msg);
1034 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1035 struct tlv_parsed tp;
1036 struct gsm_mncc alerting;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001037 int rc;
Harald Welte27989d42018-06-21 20:39:20 +02001038
1039 gsm48_stop_cc_timer(trans);
1040 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1041
1042 memset(&alerting, 0, sizeof(struct gsm_mncc));
1043 alerting.callref = trans->callref;
1044 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1045 /* facility */
1046 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1047 alerting.fields |= MNCC_F_FACILITY;
1048 gsm48_decode_facility(&alerting.facility,
1049 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1050 }
1051
1052 /* progress */
1053 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1054 alerting.fields |= MNCC_F_PROGRESS;
1055 gsm48_decode_progress(&alerting.progress,
1056 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1057 }
1058 /* ss-version */
1059 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1060 alerting.fields |= MNCC_F_SSVERSION;
1061 gsm48_decode_ssversion(&alerting.ssversion,
1062 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1063 }
1064
1065 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1066
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001067 codec_filter_run(&trans->cc.codecs);
1068 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1069 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
1070 if (rc >= sizeof(alerting.sdp)) {
1071 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
1072 rc, sizeof(alerting.sdp));
1073 trans_free(trans);
1074 return -EINVAL;
1075 }
1076
Harald Welte27989d42018-06-21 20:39:20 +02001077 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
1078 &alerting);
1079}
1080
1081static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1082{
1083 struct gsm_mncc *alerting = arg;
1084 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1085 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1086
1087 gh->msg_type = GSM48_MT_CC_ALERTING;
1088
1089 /* facility */
1090 if (alerting->fields & MNCC_F_FACILITY)
1091 gsm48_encode_facility(msg, 0, &alerting->facility);
1092 /* progress */
1093 if (alerting->fields & MNCC_F_PROGRESS)
1094 gsm48_encode_progress(msg, 0, &alerting->progress);
1095 /* user-user */
1096 if (alerting->fields & MNCC_F_USERUSER)
1097 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1098
1099 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1100
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001101 if (alerting->sdp[0]) {
1102 struct call_leg *cl = trans->msc_a->cc.call_leg;
1103 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1104 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
1105 codec_filter_run(&trans->cc.codecs);
1106 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1107 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
1108 if (rtp_cn) {
1109 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1110 rtp_stream_commit(rtp_cn);
1111 }
1112 }
1113
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001114 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001115}
1116
1117static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1118{
1119 struct gsm_mncc *progress = arg;
1120 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1121 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1122
1123 gh->msg_type = GSM48_MT_CC_PROGRESS;
1124
1125 /* progress */
1126 gsm48_encode_progress(msg, 1, &progress->progress);
1127 /* user-user */
1128 if (progress->fields & MNCC_F_USERUSER)
1129 gsm48_encode_useruser(msg, 0, &progress->useruser);
1130
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001131 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001132}
1133
1134static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1135{
1136 struct gsm_mncc *connect = arg;
1137 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1138 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1139
1140 gh->msg_type = GSM48_MT_CC_CONNECT;
1141
1142 gsm48_stop_cc_timer(trans);
1143 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1144
1145 /* facility */
1146 if (connect->fields & MNCC_F_FACILITY)
1147 gsm48_encode_facility(msg, 0, &connect->facility);
1148 /* progress */
1149 if (connect->fields & MNCC_F_PROGRESS)
1150 gsm48_encode_progress(msg, 0, &connect->progress);
1151 /* connected number */
1152 if (connect->fields & MNCC_F_CONNECTED)
1153 gsm48_encode_connected(msg, &connect->connected);
1154 /* user-user */
1155 if (connect->fields & MNCC_F_USERUSER)
1156 gsm48_encode_useruser(msg, 0, &connect->useruser);
1157
1158 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1159
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001160 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
1161 if (connect->sdp[0]) {
1162 struct call_leg *cl = trans->msc_a->cc.call_leg;
1163 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1164 rx_mncc_sdp(trans, connect->msg_type, connect->sdp);
1165 codec_filter_run(&trans->cc.codecs);
1166 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1167 get_mncc_name(connect->msg_type),
1168 codec_filter_to_str(&trans->cc.codecs));
1169 if (rtp_cn) {
1170 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1171 rtp_stream_commit(rtp_cn);
1172 }
1173 }
1174
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001175 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001176}
1177
1178static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1179{
1180 struct gsm48_hdr *gh = msgb_l3(msg);
1181 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1182 struct tlv_parsed tp;
1183 struct gsm_mncc connect;
1184
1185 gsm48_stop_cc_timer(trans);
1186
1187 memset(&connect, 0, sizeof(struct gsm_mncc));
1188 connect.callref = trans->callref;
1189 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1190 /* use subscriber as connected party number */
1191 connect.fields |= MNCC_F_CONNECTED;
1192 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1193 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1194
1195 /* facility */
1196 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1197 connect.fields |= MNCC_F_FACILITY;
1198 gsm48_decode_facility(&connect.facility,
1199 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1200 }
1201 /* user-user */
1202 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1203 connect.fields |= MNCC_F_USERUSER;
1204 gsm48_decode_useruser(&connect.useruser,
1205 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1206 }
1207 /* ss-version */
1208 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1209 connect.fields |= MNCC_F_SSVERSION;
1210 gsm48_decode_ssversion(&connect.ssversion,
1211 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1212 }
1213
1214 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001215 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001216
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001217 codec_filter_run(&trans->cc.codecs);
1218 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001219 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1220}
1221
1222
1223static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1224{
1225 struct gsm_mncc connect_ack;
1226
1227 gsm48_stop_cc_timer(trans);
1228
1229 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001230 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 +02001231
1232 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1233 connect_ack.callref = trans->callref;
1234
1235 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1236 &connect_ack);
1237}
1238
1239static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1240{
1241 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1242 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1243
1244 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1245
1246 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1247
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001248 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001249}
1250
1251static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1252{
1253 struct gsm48_hdr *gh = msgb_l3(msg);
1254 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1255 struct tlv_parsed tp;
1256 struct gsm_mncc disc;
1257
1258 gsm48_stop_cc_timer(trans);
1259
1260 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1261
1262 memset(&disc, 0, sizeof(struct gsm_mncc));
1263 disc.callref = trans->callref;
1264 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1265 /* cause */
1266 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1267 disc.fields |= MNCC_F_CAUSE;
1268 gsm48_decode_cause(&disc.cause,
1269 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1270 }
1271 /* facility */
1272 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1273 disc.fields |= MNCC_F_FACILITY;
1274 gsm48_decode_facility(&disc.facility,
1275 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1276 }
1277 /* user-user */
1278 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1279 disc.fields |= MNCC_F_USERUSER;
1280 gsm48_decode_useruser(&disc.useruser,
1281 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1282 }
1283 /* ss-version */
1284 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1285 disc.fields |= MNCC_F_SSVERSION;
1286 gsm48_decode_ssversion(&disc.ssversion,
1287 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1288 }
1289
1290 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001291}
1292
1293static struct gsm_mncc_cause default_cause = {
1294 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1295 .coding = 0,
1296 .rec = 0,
1297 .rec_val = 0,
1298 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1299 .diag_len = 0,
1300 .diag = { 0 },
1301};
1302
1303static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1304{
1305 struct gsm_mncc *disc = arg;
1306 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1307 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1308
1309 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1310
1311 gsm48_stop_cc_timer(trans);
1312 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1313
1314 /* cause */
1315 if (disc->fields & MNCC_F_CAUSE)
1316 gsm48_encode_cause(msg, 1, &disc->cause);
1317 else
1318 gsm48_encode_cause(msg, 1, &default_cause);
1319
1320 /* facility */
1321 if (disc->fields & MNCC_F_FACILITY)
1322 gsm48_encode_facility(msg, 0, &disc->facility);
1323 /* progress */
1324 if (disc->fields & MNCC_F_PROGRESS)
1325 gsm48_encode_progress(msg, 0, &disc->progress);
1326 /* user-user */
1327 if (disc->fields & MNCC_F_USERUSER)
1328 gsm48_encode_useruser(msg, 0, &disc->useruser);
1329
1330 /* store disconnect cause for T306 expiry */
1331 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1332
1333 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1334
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001335 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001336}
1337
1338static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1339{
1340 struct gsm48_hdr *gh = msgb_l3(msg);
1341 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1342 struct tlv_parsed tp;
1343 struct gsm_mncc rel;
1344 int rc;
1345
1346 gsm48_stop_cc_timer(trans);
1347
1348 memset(&rel, 0, sizeof(struct gsm_mncc));
1349 rel.callref = trans->callref;
1350 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1351 /* cause */
1352 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1353 rel.fields |= MNCC_F_CAUSE;
1354 gsm48_decode_cause(&rel.cause,
1355 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1356 }
1357 /* facility */
1358 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1359 rel.fields |= MNCC_F_FACILITY;
1360 gsm48_decode_facility(&rel.facility,
1361 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1362 }
1363 /* user-user */
1364 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1365 rel.fields |= MNCC_F_USERUSER;
1366 gsm48_decode_useruser(&rel.useruser,
1367 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1368 }
1369 /* ss-version */
1370 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1371 rel.fields |= MNCC_F_SSVERSION;
1372 gsm48_decode_ssversion(&rel.ssversion,
1373 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1374 }
1375
1376 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1377 /* release collision 5.4.5 */
1378 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1379 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001380 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001381 GSM48_PDISC_CC | (trans->transaction_id << 4),
1382 GSM48_MT_CC_RELEASE_COMPL);
1383 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1384 }
1385
1386 new_cc_state(trans, GSM_CSTATE_NULL);
1387
1388 trans->callref = 0;
1389 trans_free(trans);
1390
1391 return rc;
1392}
1393
1394static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1395{
1396 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001397 struct msgb *msg;
1398 struct gsm48_hdr *gh;
1399
1400 if (!trans->msc_a) {
1401 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1402 return -EINVAL;
1403 }
1404
1405 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1406 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001407
1408 gh->msg_type = GSM48_MT_CC_RELEASE;
1409
1410 gsm48_stop_cc_timer(trans);
1411 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1412
1413 /* cause */
1414 if (rel->fields & MNCC_F_CAUSE)
1415 gsm48_encode_cause(msg, 0, &rel->cause);
1416 /* facility */
1417 if (rel->fields & MNCC_F_FACILITY)
1418 gsm48_encode_facility(msg, 0, &rel->facility);
1419 /* user-user */
1420 if (rel->fields & MNCC_F_USERUSER)
1421 gsm48_encode_useruser(msg, 0, &rel->useruser);
1422
1423 trans->cc.T308_second = 0;
1424 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1425
1426 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1427 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1428
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001429 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001430}
1431
1432static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1433{
1434 struct gsm48_hdr *gh = msgb_l3(msg);
1435 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1436 struct tlv_parsed tp;
1437 struct gsm_mncc rel;
1438 int rc = 0;
1439
1440 gsm48_stop_cc_timer(trans);
1441
1442 memset(&rel, 0, sizeof(struct gsm_mncc));
1443 rel.callref = trans->callref;
1444 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1445 /* cause */
1446 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1447 rel.fields |= MNCC_F_CAUSE;
1448 gsm48_decode_cause(&rel.cause,
1449 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1450 }
1451 /* facility */
1452 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1453 rel.fields |= MNCC_F_FACILITY;
1454 gsm48_decode_facility(&rel.facility,
1455 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1456 }
1457 /* user-user */
1458 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1459 rel.fields |= MNCC_F_USERUSER;
1460 gsm48_decode_useruser(&rel.useruser,
1461 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1462 }
1463 /* ss-version */
1464 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1465 rel.fields |= MNCC_F_SSVERSION;
1466 gsm48_decode_ssversion(&rel.ssversion,
1467 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1468 }
1469
1470 if (trans->callref) {
1471 switch (trans->cc.state) {
1472 case GSM_CSTATE_CALL_PRESENT:
1473 rc = mncc_recvmsg(trans->net, trans,
1474 MNCC_REJ_IND, &rel);
1475 break;
1476 case GSM_CSTATE_RELEASE_REQ:
1477 rc = mncc_recvmsg(trans->net, trans,
1478 MNCC_REL_CNF, &rel);
1479 break;
1480 default:
1481 rc = mncc_recvmsg(trans->net, trans,
1482 MNCC_REL_IND, &rel);
1483 }
1484 }
1485
1486 trans->callref = 0;
1487 trans_free(trans);
1488
1489 return rc;
1490}
1491
1492static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1493{
1494 struct gsm_mncc *rel = arg;
1495 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1496 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1497 int ret;
1498
1499 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1500
1501 trans->callref = 0;
1502
1503 gsm48_stop_cc_timer(trans);
1504
1505 /* cause */
1506 if (rel->fields & MNCC_F_CAUSE)
1507 gsm48_encode_cause(msg, 0, &rel->cause);
1508 /* facility */
1509 if (rel->fields & MNCC_F_FACILITY)
1510 gsm48_encode_facility(msg, 0, &rel->facility);
1511 /* user-user */
1512 if (rel->fields & MNCC_F_USERUSER)
1513 gsm48_encode_useruser(msg, 0, &rel->useruser);
1514
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001515 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001516
1517 trans_free(trans);
1518
1519 return ret;
1520}
1521
1522static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1523{
1524 struct gsm48_hdr *gh = msgb_l3(msg);
1525 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1526 struct tlv_parsed tp;
1527 struct gsm_mncc fac;
1528
1529 memset(&fac, 0, sizeof(struct gsm_mncc));
1530 fac.callref = trans->callref;
1531 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1532 /* facility */
1533 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1534 fac.fields |= MNCC_F_FACILITY;
1535 gsm48_decode_facility(&fac.facility,
1536 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1537 }
1538 /* ss-version */
1539 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1540 fac.fields |= MNCC_F_SSVERSION;
1541 gsm48_decode_ssversion(&fac.ssversion,
1542 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1543 }
1544
1545 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1546}
1547
1548static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1549{
1550 struct gsm_mncc *fac = arg;
1551 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1553
1554 gh->msg_type = GSM48_MT_CC_FACILITY;
1555
1556 /* facility */
1557 gsm48_encode_facility(msg, 1, &fac->facility);
1558
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001559 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001560}
1561
1562static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1563{
1564 struct gsm_mncc hold;
1565
1566 memset(&hold, 0, sizeof(struct gsm_mncc));
1567 hold.callref = trans->callref;
1568 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1569}
1570
1571static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1572{
1573 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1574 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1575
1576 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1577
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001578 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001579}
1580
1581static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1582{
1583 struct gsm_mncc *hold_rej = arg;
1584 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1585 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1586
1587 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1588
1589 /* cause */
1590 if (hold_rej->fields & MNCC_F_CAUSE)
1591 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1592 else
1593 gsm48_encode_cause(msg, 1, &default_cause);
1594
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001595 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001596}
1597
1598static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1599{
1600 struct gsm_mncc retrieve;
1601
1602 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1603 retrieve.callref = trans->callref;
1604 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1605 &retrieve);
1606}
1607
1608static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1609{
1610 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1611 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1612
1613 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1614
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001615 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001616}
1617
1618static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1619{
1620 struct gsm_mncc *retrieve_rej = arg;
1621 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1622 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1623
1624 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1625
1626 /* cause */
1627 if (retrieve_rej->fields & MNCC_F_CAUSE)
1628 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1629 else
1630 gsm48_encode_cause(msg, 1, &default_cause);
1631
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001632 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001633}
1634
1635static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1636{
1637 struct gsm48_hdr *gh = msgb_l3(msg);
1638 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1639 struct tlv_parsed tp;
1640 struct gsm_mncc dtmf;
1641
1642 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1643 dtmf.callref = trans->callref;
1644 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1645 /* keypad facility */
1646 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1647 dtmf.fields |= MNCC_F_KEYPAD;
1648 gsm48_decode_keypad(&dtmf.keypad,
1649 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1650 }
1651
1652 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1653}
1654
1655static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1656{
1657 struct gsm_mncc *dtmf = arg;
1658 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1659 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1660
1661 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1662
1663 /* keypad */
1664 if (dtmf->fields & MNCC_F_KEYPAD)
1665 gsm48_encode_keypad(msg, dtmf->keypad);
1666
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001667 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001668}
1669
1670static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1671{
1672 struct gsm_mncc *dtmf = arg;
1673 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1674 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1675
1676 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1677
1678 /* cause */
1679 if (dtmf->fields & MNCC_F_CAUSE)
1680 gsm48_encode_cause(msg, 1, &dtmf->cause);
1681 else
1682 gsm48_encode_cause(msg, 1, &default_cause);
1683
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001684 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001685}
1686
1687static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1688{
1689 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1690 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1691
1692 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1693
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001694 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001695}
1696
1697static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1698{
1699 struct gsm_mncc dtmf;
1700
1701 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1702 dtmf.callref = trans->callref;
1703
1704 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1705}
1706
1707static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1708{
1709 struct gsm48_hdr *gh = msgb_l3(msg);
1710 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1711 struct tlv_parsed tp;
1712 struct gsm_mncc modify;
1713
1714 memset(&modify, 0, sizeof(struct gsm_mncc));
1715 modify.callref = trans->callref;
1716 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1717 /* bearer capability */
1718 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1719 modify.fields |= MNCC_F_BEARER_CAP;
1720 gsm48_decode_bearer_cap(&modify.bearer_cap,
1721 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1722
1723 /* Create a copy of the bearer capability
1724 * in the transaction struct, so we can use
1725 * this information later */
1726 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1727 sizeof(trans->bearer_cap));
1728 }
1729
1730 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1731
1732 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1733}
1734
1735static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1736{
1737 struct gsm_mncc *modify = arg;
1738 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1739 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1740
1741 gh->msg_type = GSM48_MT_CC_MODIFY;
1742
1743 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1744
1745 /* bearer capability */
1746 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1747 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1748
1749 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1750
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001751 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001752}
1753
1754static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1755{
1756 struct gsm48_hdr *gh = msgb_l3(msg);
1757 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1758 struct tlv_parsed tp;
1759 struct gsm_mncc modify;
1760
1761 gsm48_stop_cc_timer(trans);
1762
1763 memset(&modify, 0, sizeof(struct gsm_mncc));
1764 modify.callref = trans->callref;
1765 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1766 /* bearer capability */
1767 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1768 modify.fields |= MNCC_F_BEARER_CAP;
1769 gsm48_decode_bearer_cap(&modify.bearer_cap,
1770 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1771
1772 /* Create a copy of the bearer capability
1773 * in the transaction struct, so we can use
1774 * this information later */
1775 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1776 sizeof(trans->bearer_cap));
1777 }
1778
1779 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1780
1781 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1782}
1783
1784static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1785{
1786 struct gsm_mncc *modify = arg;
1787 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1788 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1789
1790 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1791
1792 /* bearer capability */
1793 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1794 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1795
1796 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1797
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001798 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001799}
1800
1801static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1802{
1803 struct gsm48_hdr *gh = msgb_l3(msg);
1804 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1805 struct tlv_parsed tp;
1806 struct gsm_mncc modify;
1807
1808 gsm48_stop_cc_timer(trans);
1809
1810 memset(&modify, 0, sizeof(struct gsm_mncc));
1811 modify.callref = trans->callref;
1812 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1813 /* bearer capability */
1814 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1815 modify.fields |= GSM48_IE_BEARER_CAP;
1816 gsm48_decode_bearer_cap(&modify.bearer_cap,
1817 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1818
1819 /* Create a copy of the bearer capability
1820 * in the transaction struct, so we can use
1821 * this information later */
1822 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1823 sizeof(trans->bearer_cap));
1824 }
1825 /* cause */
1826 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1827 modify.fields |= MNCC_F_CAUSE;
1828 gsm48_decode_cause(&modify.cause,
1829 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1830 }
1831
1832 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1833
1834 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1835}
1836
1837static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1838{
1839 struct gsm_mncc *modify = arg;
1840 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1842
1843 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1844
1845 /* bearer capability */
1846 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1847 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1848 /* cause */
1849 gsm48_encode_cause(msg, 1, &modify->cause);
1850
1851 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1852
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001853 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001854}
1855
1856static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1857{
1858 struct gsm_mncc *notify = arg;
1859 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1860 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1861
1862 gh->msg_type = GSM48_MT_CC_NOTIFY;
1863
1864 /* notify */
1865 gsm48_encode_notify(msg, notify->notify);
1866
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001867 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001868}
1869
1870static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1871{
1872 struct gsm48_hdr *gh = msgb_l3(msg);
1873 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1874// struct tlv_parsed tp;
1875 struct gsm_mncc notify;
1876
1877 memset(&notify, 0, sizeof(struct gsm_mncc));
1878 notify.callref = trans->callref;
1879// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1880 if (payload_len >= 1)
1881 gsm48_decode_notify(&notify.notify, gh->data);
1882
1883 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1884}
1885
1886static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1887{
1888 struct gsm_mncc *user = arg;
1889 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1890 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1891
1892 gh->msg_type = GSM48_MT_CC_USER_INFO;
1893
1894 /* user-user */
1895 if (user->fields & MNCC_F_USERUSER)
1896 gsm48_encode_useruser(msg, 1, &user->useruser);
1897 /* more data */
1898 if (user->more)
1899 gsm48_encode_more(msg);
1900
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001901 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001902}
1903
1904static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1905{
1906 struct gsm48_hdr *gh = msgb_l3(msg);
1907 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1908 struct tlv_parsed tp;
1909 struct gsm_mncc user;
1910
1911 memset(&user, 0, sizeof(struct gsm_mncc));
1912 user.callref = trans->callref;
1913 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1914 /* user-user */
1915 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1916 user.fields |= MNCC_F_USERUSER;
1917 gsm48_decode_useruser(&user.useruser,
1918 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1919 }
1920 /* more data */
1921 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1922 user.more = 1;
1923
1924 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1925}
1926
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001927static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1928 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001929 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001930{
1931 uint8_t data[sizeof(struct gsm_mncc)];
1932 struct gsm_mncc_rtp *rtp;
1933
1934 memset(&data, 0, sizeof(data));
1935 rtp = (struct gsm_mncc_rtp *) &data[0];
1936
1937 rtp->callref = callref;
1938 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001939 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001940 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1941 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001942 }
Harald Welte27989d42018-06-21 20:39:20 +02001943 rtp->payload_type = payload_type;
1944 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001945 if (sdp)
1946 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001947 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001948}
1949
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001950static 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 +02001951{
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001952 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001953}
1954
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001955static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001956{
1957 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001958
1959 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001960 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001961 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001962 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001963 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001964 return -EIO;
1965 }
1966 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001967 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001968 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001969 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001970 return 0;
1971 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001972 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001973
Harald Welte27989d42018-06-21 20:39:20 +02001974 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001975 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001976}
1977
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001978int cc_on_cn_local_rtp_port_known(struct gsm_trans *cc_trans)
1979{
1980 /* Depending on MO or MT call, dispatch the event differently */
1981 switch (cc_trans->cc.state) {
1982 case GSM_CSTATE_INITIATED:
1983 if (cc_trans->cc.msg.msg_type != MNCC_SETUP_IND) {
1984 LOG_TRANS(cc_trans, LOGL_ERROR, "Assuming MO call, expected MNCC_SETUP_IND to be prepared\n");
1985 return -EINVAL;
1986 }
1987 /* This is the MO call leg, waiting for a CN RTP be able to send initial MNCC_SETUP_IND. */
1988 gsm48_cc_rx_setup_cn_local_rtp_port_known(cc_trans);
1989 return 0;
1990
1991 case GSM_CSTATE_MO_TERM_CALL_CONF:
1992 /* This is the MT call leg, waiting for a CN RTP to be able to send MNCC_CALL_CONF_IND. */
1993 return gsm48_cc_mt_rtp_port_and_codec_known(cc_trans);
1994
1995 default:
1996 LOG_TRANS(cc_trans, LOGL_ERROR, "CN RTP address available, but in unexpected state %d\n",
1997 cc_trans->cc.state);
1998 return -EINVAL;
1999 }
2000}
2001
2002int cc_on_assignment_done(struct gsm_trans *trans)
2003{
2004 struct msc_a *msc_a = trans->msc_a;
2005
2006 switch (trans->cc.state) {
2007 case GSM_CSTATE_INITIATED:
2008 case GSM_CSTATE_MO_CALL_PROC:
2009 /* MO call */
2010 break;
2011
2012 case GSM_CSTATE_CALL_RECEIVED:
2013 case GSM_CSTATE_MO_TERM_CALL_CONF:
2014 /* MT call */
2015 break;
2016
2017 case GSM_CSTATE_ACTIVE:
2018 /* already active. MNCC finished before Abis completed the Assignment. */
2019 break;
2020
2021 default:
2022 LOG_TRANS(trans, LOGL_ERROR, "Assignment done in unexpected CC state: %d\n", trans->cc.state);
2023 return -EINVAL;
2024 }
2025
2026 if (!call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN)) {
2027 LOG_TRANS(trans, LOGL_DEBUG,
2028 "Assignment complete, but still waiting for the CRCX OK on the CN side RTP\n");
2029 return 0;
2030 }
2031 return gsm48_tch_rtp_create(trans);
2032}
2033
Harald Welte27989d42018-06-21 20:39:20 +02002034/* Trigger TCH_RTP_CREATE acknowledgement */
2035int gsm48_tch_rtp_create(struct gsm_trans *trans)
2036{
2037 /* This function is called as soon as the port, on which the
2038 * mgcp-gw expects the incoming RTP stream from the remote
2039 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002040 struct msc_a *msc_a = trans->msc_a;
2041 struct gsm_network *net = msc_a_net(msc_a);
2042 struct call_leg *cl = msc_a->cc.call_leg;
2043 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002044 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002045 int mncc_payload_msg_type;
2046 struct sdp_audio_codec *codec;
Neels Hofmeyra001a702022-10-31 17:57:30 +01002047 const struct codec_mapping *m;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002048 struct sdp_audio_codecs *codecs;
Harald Welte27989d42018-06-21 20:39:20 +02002049
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002050 if (!rtp_cn) {
2051 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
2052 return -EINVAL;
2053 }
2054
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002055 codec_filter_run(&trans->cc.codecs);
2056 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
2057 codecs = &trans->cc.codecs.result.audio_codecs;
2058 if (!codecs->count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002059 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002060 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002061 return -EINVAL;
2062 }
2063
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002064 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
2065 codec = &codecs->codec[0];
2066 m = codec_mapping_by_subtype_name(codec->subtype_name);
2067 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02002068
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002069 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
2070 if (!rtp_cn_local) {
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002071 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 +01002072 return -EINVAL;
2073 }
2074
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01002075 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002076 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02002077}
2078
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002079static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002080{
2081 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002082 struct call_leg *cl;
2083 struct rtp_stream *rtps;
Philipp Maier8ad3dac2018-08-07 13:00:14 +02002084
Harald Welte27989d42018-06-21 20:39:20 +02002085 /* Find callref */
2086 trans = trans_find_by_callref(net, rtp->callref);
2087 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002088 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002089 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02002090 return -EIO;
2091 }
2092 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002093 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002094 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002095 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002096 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02002097 }
2098
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002099 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002100
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002101 cl = trans->msc_a->cc.call_leg;
2102 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
2103
2104 if (!rtps) {
2105 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
2106 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2107 return -EINVAL;
2108 }
2109
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002110 rx_mncc_sdp(trans, rtp->msg_type, rtp->sdp);
2111 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
2112
2113 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
2114 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
2115 struct osmo_sockaddr_str rtp_addr;
2116 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
2117 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
2118 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2119 return -EINVAL;
2120 }
2121 rtp_stream_set_remote_addr(rtps, &rtp_addr);
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002122 }
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002123
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002124 rtp_stream_commit(rtps);
2125 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02002126}
2127
2128static struct downstate {
2129 uint32_t states;
2130 int type;
2131 int (*rout) (struct gsm_trans *trans, void *arg);
2132} downstatelist[] = {
2133 /* mobile originating call establishment */
2134 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2135 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2136 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2137 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2138 {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 */
2139 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2140 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2141 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2142 /* mobile terminating call establishment */
2143 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2144 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2145 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2146 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2147 /* signalling during call */
2148 {SBIT(GSM_CSTATE_ACTIVE),
2149 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2150 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2151 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2152 {ALL_STATES,
2153 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2154 {ALL_STATES,
2155 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2156 {ALL_STATES,
2157 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2158 {SBIT(GSM_CSTATE_ACTIVE),
2159 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2160 {SBIT(GSM_CSTATE_ACTIVE),
2161 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2162 {SBIT(GSM_CSTATE_ACTIVE),
2163 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2164 {SBIT(GSM_CSTATE_ACTIVE),
2165 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2166 {SBIT(GSM_CSTATE_ACTIVE),
2167 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2168 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2169 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2170 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2171 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2172 {SBIT(GSM_CSTATE_ACTIVE),
2173 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2174 /* clearing */
2175 {SBIT(GSM_CSTATE_INITIATED),
2176 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2177 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2178 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2179 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2180 MNCC_REL_REQ, gsm48_cc_tx_release},
2181};
2182
2183#define DOWNSLLEN \
2184 (sizeof(downstatelist) / sizeof(struct downstate))
2185
2186
Philipp Maiercd64af72019-08-01 09:46:40 +02002187static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002188{
2189 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002190 struct msc_a *msc_a = NULL;
2191 struct gsm_trans *trans = NULL;
2192 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002193
Harald Welte27989d42018-06-21 20:39:20 +02002194 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002195 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002196 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002197 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002198 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002199 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002200 return rc;
2201 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002202 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002203 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002204 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002205 case MNCC_RTP_FREE:
2206 /* unused right now */
2207 return -EIO;
2208
2209 case MNCC_FRAME_DROP:
2210 case MNCC_FRAME_RECV:
2211 case GSM_TCHF_FRAME:
2212 case GSM_TCHF_FRAME_EFR:
2213 case GSM_TCHH_FRAME:
2214 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002215 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002216 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002217 return -ENOTSUP;
2218 }
2219
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002220 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002221
2222 /* Find callref */
2223 trans = trans_find_by_callref(net, data->callref);
2224
2225 /* Callref unknown */
2226 if (!trans) {
2227 struct vlr_subscr *vsub;
2228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002229 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002230 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002231 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002232 /* Invalid call reference */
2233 return mncc_release_ind(net, NULL, data->callref,
2234 GSM48_CAUSE_LOC_PRN_S_LU,
2235 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2236 }
2237 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002238 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002239 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002240 /* Invalid number */
2241 return mncc_release_ind(net, NULL, data->callref,
2242 GSM48_CAUSE_LOC_PRN_S_LU,
2243 GSM48_CC_CAUSE_INV_NR_FORMAT);
2244 }
2245 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002246 if (data->called.number[0]) {
2247 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2248 if (!vsub)
2249 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002250 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002251 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002252 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002253 if (!vsub)
2254 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002255 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002256 }
2257 if (!vsub)
2258 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002259 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002260 /* update the subscriber we deal with */
2261 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2262
Harald Welte27989d42018-06-21 20:39:20 +02002263 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002264 if (!vsub->lu_complete) {
2265 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002266 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002267 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002268 /* Temporarily out of order */
2269 return mncc_release_ind(net, NULL, data->callref,
2270 GSM48_CAUSE_LOC_PRN_S_LU,
2271 GSM48_CC_CAUSE_DEST_OOO);
2272 }
Keith Whyte991bb422019-08-08 15:43:40 +02002273
2274 /* Find valid conn */
2275 msc_a = msc_a_for_vsub(vsub, true);
2276
2277 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2278 if (!net->call_waiting && msc_a) {
2279 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2280 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2281 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2282 "rx '%s' for subscriber %s with trans state (%s)"
2283 " rejecting with USER_BUSY\n",
2284 get_mncc_name(msg->msg_type), data->called.number,
2285 gsm48_cc_state_name(existing_cc_trans->cc.state));
2286 return mncc_release_ind(net, NULL, data->callref,
2287 GSM48_CAUSE_LOC_PRN_S_LU,
2288 GSM48_CC_CAUSE_USER_BUSY);
2289 }
2290 }
2291
Harald Welte27989d42018-06-21 20:39:20 +02002292 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002293 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002294 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002295 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002296 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002297 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002298 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002299 mncc_release_ind(net, NULL, data->callref,
2300 GSM48_CAUSE_LOC_PRN_S_LU,
2301 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2302 return -ENOMEM;
2303 }
2304
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002305 /* Remember remote SDP, if any */
2306 rx_mncc_sdp(trans, data->msg_type, data->sdp);
2307
Harald Welte27989d42018-06-21 20:39:20 +02002308 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002309 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002310 /* This condition will return before the common logging of the received MNCC message below, so
2311 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002312 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002313
Harald Welte27989d42018-06-21 20:39:20 +02002314 /* store setup information until paging succeeds */
2315 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2316
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002317 /* Request a channel. If Paging already started, paging_request_start() will append the new
2318 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002319 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2320 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002321 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002322 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002323 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002324 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002325 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002326 return 0;
2327 }
2328
2329 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002330 trans->msc_a = msc_a;
2331 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002332 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002333 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002334 } else {
2335 /* update the subscriber we deal with */
2336 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2337 }
2338
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002339 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002340
Philipp Maier9ca7b312018-10-10 17:00:49 +02002341 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002342 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002343
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002344 if (trans->msc_a)
2345 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002346
2347 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002348 if (!msc_a) {
2349 struct gsm_mncc rel = {
2350 .callref = data->callref,
2351 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002352 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002353 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2354 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002355 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002356 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2357 else
2358 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2359 trans->callref = 0;
2360 trans_free(trans);
2361 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002362 }
2363
2364 /* Find function for current state and message */
2365 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002366 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002367 && ((1 << trans->cc.state) & downstatelist[i].states))
2368 break;
2369 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002370 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002371 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002372 return 0;
2373 }
2374
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002375 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002376
2377 return rc;
2378}
2379
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002380struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2381{
2382 uint32_t callref;
2383
2384 switch (msg->msg_type) {
2385 case MNCC_BRIDGE:
2386 callref = msg->bridge.callref[0];
2387 break;
2388 case MNCC_RTP_CREATE:
2389 case MNCC_RTP_CONNECT:
2390 callref = msg->rtp.callref;
2391 break;
2392
2393 case MNCC_RTP_FREE:
2394 case MNCC_FRAME_DROP:
2395 case MNCC_FRAME_RECV:
2396 case GSM_TCHF_FRAME:
2397 case GSM_TCHF_FRAME_EFR:
2398 case GSM_TCHH_FRAME:
2399 case GSM_TCH_FRAME_AMR:
2400 return NULL;
2401
2402 default:
2403 callref = msg->signal.callref;
2404 break;
2405 }
2406
2407 return mncc_call_find_by_callref(callref);
2408}
2409
2410/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002411int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002412{
2413 const union mncc_msg *msg = arg;
2414 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002415
2416 if (msg->msg_type == MNCC_SETUP_REQ) {
2417 /* Incoming call to forward for inter-MSC Handover? */
2418 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2419 if (mncc_call)
2420 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2421 "Incoming call matches pending inter-MSC Handover Number\n");
2422 }
2423 if (!mncc_call) {
2424 /* Find already active MNCC FSM for this callref.
2425 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2426 * MNCC<->GSM-CC call handling. */
2427 mncc_call = mncc_find_by_callref_from_msg(msg);
2428 }
2429 if (mncc_call) {
2430 mncc_call_rx(mncc_call, msg);
2431 return 0;
2432 }
2433
2434 /* None of the above? Then it must be a normal GSM CC call related message. */
2435 return mncc_tx_to_gsm_cc(net, msg);
2436}
Harald Welte27989d42018-06-21 20:39:20 +02002437
2438static struct datastate {
2439 uint32_t states;
2440 int type;
2441 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2442} datastatelist[] = {
2443 /* mobile originating call establishment */
2444 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2445 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2446 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2447 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2448 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2449 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2450 /* mobile terminating call establishment */
2451 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2452 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2453 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2454 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2455 {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 */
2456 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2457 /* signalling during call */
2458 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2459 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2460 {SBIT(GSM_CSTATE_ACTIVE),
2461 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2462 {ALL_STATES,
2463 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2464 {ALL_STATES,
2465 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2466 {ALL_STATES,
2467 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2468 {SBIT(GSM_CSTATE_ACTIVE),
2469 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2470 {SBIT(GSM_CSTATE_ACTIVE),
2471 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2472 {SBIT(GSM_CSTATE_ACTIVE),
2473 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2474 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2475 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2476 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2477 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2478 {SBIT(GSM_CSTATE_ACTIVE),
2479 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2480 /* clearing */
2481 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2482 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2483 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2484 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2485 {ALL_STATES, /* 5.4.3.4 */
2486 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2487};
2488
2489#define DATASLLEN \
2490 (sizeof(datastatelist) / sizeof(struct datastate))
2491
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002492int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002493{
2494 struct gsm48_hdr *gh = msgb_l3(msg);
2495 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2496 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2497 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002498 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2499 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002500 int i, rc = 0;
2501
2502 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002503 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002504 return -EINVAL;
2505 }
2506
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002507 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002508 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002509 return -EINVAL;
2510 }
2511
2512 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002513 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002514
Harald Welte27989d42018-06-21 20:39:20 +02002515 /* Create transaction */
2516 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002517 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002518 trans = trans_alloc(net, vsub,
2519 TRANS_CC,
2520 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002521 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002522 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002523 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002524 GSM48_PDISC_CC | (transaction_id << 4),
2525 GSM48_MT_CC_RELEASE_COMPL);
2526 return -ENOMEM;
2527 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002528 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2529 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2530 trans_free(trans);
2531 return -EINVAL;
2532 }
2533
Harald Welte27989d42018-06-21 20:39:20 +02002534 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002535 msc_a_get(msc_a, MSC_A_USE_CC);
2536 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002537 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002538
2539 /* An earlier CM Service Request for this CC message now has concluded */
2540 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2541 LOG_MSC_A(msc_a, LOGL_ERROR,
2542 "Creating new CC transaction without prior CM Service Request\n");
2543 else
2544 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002545 }
2546
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002547 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2548 gsm48_cc_state_name(trans->cc.state));
2549
Harald Welte27989d42018-06-21 20:39:20 +02002550 /* find function for current state and message */
2551 for (i = 0; i < DATASLLEN; i++)
2552 if ((msg_type == datastatelist[i].type)
2553 && ((1 << trans->cc.state) & datastatelist[i].states))
2554 break;
2555 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002556 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002557
2558 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2559 * transaction right away. */
2560 if (trans->cc.state == GSM_CSTATE_NULL) {
2561 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2562 " -- disarding new CC transaction right away\n");
2563 trans_free(trans);
2564 }
Harald Welte27989d42018-06-21 20:39:20 +02002565 return 0;
2566 }
2567
2568 assert(trans->vsub);
2569
2570 rc = datastatelist[i].rout(trans, msg);
2571
Harald Welte27989d42018-06-21 20:39:20 +02002572 return rc;
2573}