blob: 269fd5709902d552972421ba7074a1d0d0ceeaa7 [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 */
237static void log_mncc_rx_tx(struct gsm_trans *trans, const char *rx_tx, const union mncc_msg *mncc)
238{
239 const char *sdp = NULL;
240 struct sdp_msg sdp_msg = {};
241 struct osmo_sockaddr addr = {};
242
243 if (!log_check_level(DMNCC, LOGL_DEBUG))
244 return;
245
246 switch (mncc->msg_type) {
247 case MNCC_RTP_CREATE:
248 case MNCC_RTP_CONNECT:
249 addr = (struct osmo_sockaddr){ .u.sas = mncc->rtp.addr };
250 sdp = mncc->rtp.sdp;
251 break;
252
253 case MNCC_SETUP_IND:
254 case MNCC_SETUP_REQ:
255 case MNCC_SETUP_COMPL_IND:
256 case MNCC_SETUP_COMPL_REQ:
257 case MNCC_SETUP_RSP:
258 case MNCC_SETUP_CNF:
259 case MNCC_CALL_CONF_IND:
260 case MNCC_CALL_PROC_REQ:
261 case MNCC_ALERT_IND:
262 case MNCC_ALERT_REQ:
263 sdp = mncc->signal.sdp;
264 break;
265
266 default:
267 break;
268 }
269
270 if (sdp && sdp[0] && (sdp_msg_from_sdp_str(&sdp_msg, sdp) == 0)) {
271 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s (RTP=%s)\n",
272 rx_tx,
273 get_mncc_name(mncc->msg_type),
274 sdp_msg_to_str(&sdp_msg));
275 return;
276 }
277
278 if (osmo_sockaddr_is_any(&addr) == 0) {
279 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s (RTP=%s)\n",
280 rx_tx,
281 get_mncc_name(mncc->msg_type),
282 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
283 return;
284 }
285
286 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s\n", rx_tx, get_mncc_name(mncc->msg_type));
287}
288
Harald Welte27989d42018-06-21 20:39:20 +0200289static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
290 int msg_type, struct gsm_mncc *mncc)
291{
292 struct msgb *msg;
293 unsigned char *data;
294
Harald Welte27989d42018-06-21 20:39:20 +0200295 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100296 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200297
298 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
299 if (!msg)
300 return -ENOMEM;
301
302 data = msgb_put(msg, sizeof(struct gsm_mncc));
303 memcpy(data, mncc, sizeof(struct gsm_mncc));
304
305 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200306 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
307 if (trans)
308 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200309
310 return 0;
311}
312
313int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
314 uint32_t callref, int location, int value)
315{
316 struct gsm_mncc rel;
317
318 memset(&rel, 0, sizeof(rel));
319 rel.callref = callref;
320 mncc_set_cause(&rel, location, value);
321 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
322 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
323 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
324}
325
326/* Call Control Specific transaction release.
327 * gets called by trans_free, DO NOT CALL YOURSELF! */
328void _gsm48_cc_trans_free(struct gsm_trans *trans)
329{
330 gsm48_stop_cc_timer(trans);
331
Harald Welte27989d42018-06-21 20:39:20 +0200332 /* send release to L4, if callref still exists */
333 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100334 /* Send MNCC REL.ind (cause='Resource unavailable') */
335 if (trans->cc.mncc_initiated) {
336 mncc_release_ind(trans->net, trans, trans->callref,
337 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100338 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
339 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100340 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
341 }
342
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100343 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
344 * CC Release to the MS if it gets freed here. Hack it to do so. */
345 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
346 struct gsm_mncc rel = {};
347 rel.callref = trans->callref;
348 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
349 gsm48_cc_tx_release(trans, &rel);
350 }
Harald Welte27989d42018-06-21 20:39:20 +0200351 /* This is a final freeing of the transaction. The MNCC release may have triggered the
352 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
353 gsm48_stop_cc_timer(trans);
354 }
355 if (trans->cc.state != GSM_CSTATE_NULL)
356 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200357
358 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359
360 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
361 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200362}
363
Harald Welte27989d42018-06-21 20:39:20 +0200364/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100365static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200366{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100367 if (trans->msc_a) {
368 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
369 "Handle paging error: transaction already associated with subscriber,"
370 " apparently it was already handled. Skip.\n");
371 return;
Harald Welte27989d42018-06-21 20:39:20 +0200372 }
373
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100374 if (msc_a) {
375 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
376 /* Assign conn */
377 msc_a_get(msc_a, MSC_A_USE_CC);
378 trans->msc_a = msc_a;
379 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200380
381 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300382 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200383 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300384 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
385 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
386 &trans->cc.msg.gcr[0],
387 sizeof(trans->cc.msg.gcr));
388 if (rc < 0)
389 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
390 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200391 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200392 }
393
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100394 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
395 /* send SETUP request to called party */
396 gsm48_cc_tx_setup(trans, &trans->cc.msg);
397 } else {
398 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
399 /* Temporarily out of order */
400 mncc_release_ind(trans->net, trans,
401 trans->callref,
402 GSM48_CAUSE_LOC_PRN_S_LU,
403 GSM48_CC_CAUSE_DEST_OOO);
404 trans->callref = 0;
405 trans->paging_request = NULL;
406 trans_free(trans);
407 }
Harald Welte27989d42018-06-21 20:39:20 +0200408}
409
410/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100411static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200412{
413 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
414 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100415 struct call_leg *cl1;
416 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200417
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100418 if (!trans1 || !trans2) {
419 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200420 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100421 }
Harald Welte27989d42018-06-21 20:39:20 +0200422
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100423 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100424 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
425 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 +0200426 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100427 }
428
429 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
430 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200431
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100432 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
433 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
434 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
435 * If we can't, then we must give up. */
436 cl1 = trans1->msc_a->cc.call_leg;
437 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200438
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100439 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200440}
441
442static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
443{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100444 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200445 return gsm48_cc_tx_status(trans, msg);
446}
447
Harald Welte27989d42018-06-21 20:39:20 +0200448static void gsm48_cc_timeout(void *arg)
449{
450 struct gsm_trans *trans = arg;
451 int disconnect = 0, release = 0;
452 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
453 int mo_location = GSM48_CAUSE_LOC_USER;
454 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
455 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
456 struct gsm_mncc mo_rel, l4_rel;
457
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200458 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
459
Harald Welte27989d42018-06-21 20:39:20 +0200460 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
461 mo_rel.callref = trans->callref;
462 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
463 l4_rel.callref = trans->callref;
464
465 switch(trans->cc.Tcurrent) {
466 case 0x303:
467 release = 1;
468 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
469 break;
470 case 0x310:
471 disconnect = 1;
472 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
473 break;
474 case 0x313:
475 disconnect = 1;
476 /* unknown, did not find it in the specs */
477 break;
478 case 0x301:
479 disconnect = 1;
480 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
481 break;
482 case 0x308:
483 if (!trans->cc.T308_second) {
484 /* restart T308 a second time */
485 gsm48_cc_tx_release(trans, &trans->cc.msg);
486 trans->cc.T308_second = 1;
487 break; /* stay in release state */
488 }
489 trans_free(trans);
490 return;
491 case 0x306:
492 release = 1;
493 mo_cause = trans->cc.msg.cause.value;
494 mo_location = trans->cc.msg.cause.location;
495 break;
496 case 0x323:
497 disconnect = 1;
498 break;
499 default:
500 release = 1;
501 }
502
503 if (release && trans->callref) {
504 /* process release towards layer 4 */
505 mncc_release_ind(trans->net, trans, trans->callref,
506 l4_location, l4_cause);
507 trans->callref = 0;
508 }
509
510 if (disconnect && trans->callref) {
511 /* process disconnect towards layer 4 */
512 mncc_set_cause(&l4_rel, l4_location, l4_cause);
513 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
514 }
515
516 /* process disconnect towards mobile station */
517 if (disconnect || release) {
518 mncc_set_cause(&mo_rel, mo_location, mo_cause);
519 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
520 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
521 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
522 mo_rel.cause.diag_len = 3;
523
524 if (disconnect)
525 gsm48_cc_tx_disconnect(trans, &mo_rel);
526 if (release)
527 gsm48_cc_tx_release(trans, &mo_rel);
528 }
529
530}
531
532/* disconnect both calls from the bridge */
533static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100534 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200535{
536 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
537 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
538 struct gsm_mncc mx_rel;
539 if (!trans0 || !trans1)
540 return;
541
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100542 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
543 trans0->callref, trans1->callref, strerror(err));
544 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200545 trans0->callref, trans1->callref, strerror(err));
546
547 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
548 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
549 GSM48_CC_CAUSE_CHAN_UNACCEPT);
550
551 mx_rel.callref = trans0->callref;
552 gsm48_cc_tx_disconnect(trans0, &mx_rel);
553
554 mx_rel.callref = trans1->callref;
555 gsm48_cc_tx_disconnect(trans1, &mx_rel);
556}
557
558static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
559 int sec, int micro)
560{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100561 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200562 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
563 osmo_timer_schedule(&trans->cc.timer, sec, micro);
564 trans->cc.Tcurrent = current;
565}
566
567static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
568{
569 struct gsm48_hdr *gh = msgb_l3(msg);
570 uint8_t msg_type = gsm48_hdr_msg_type(gh);
571 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
572 struct tlv_parsed tp;
573 struct gsm_mncc setup;
574
Philipp Maier9ca7b312018-10-10 17:00:49 +0200575 gsm48_start_guard_timer(trans);
576
Harald Welte27989d42018-06-21 20:39:20 +0200577 memset(&setup, 0, sizeof(struct gsm_mncc));
578 setup.callref = trans->callref;
579
Keith Whytea1a70be2021-05-16 02:59:52 +0200580 /* New Global Call Reference */
581 if (!trans->cc.lcls)
582 trans->cc.lcls = trans_lcls_compose(trans, true);
583
584 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300585 if (trans->cc.lcls) {
586 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
587 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
588 int rc;
589
590 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
591 memcpy(&setup.gcr[0], gcr_msg->data, rc);
592 setup.fields |= MNCC_F_GCR;
593 } else
594 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
595 msgb_free(gcr_msg);
596 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200597
Harald Welte27989d42018-06-21 20:39:20 +0200598 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
599 /* emergency setup is identified by msg_type */
600 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
601 setup.fields |= MNCC_F_EMERGENCY;
602 setup.emergency = 1;
603 /* use destination number as configured by user (if any) */
604 if (trans->net->emergency.route_to_msisdn) {
605 setup.fields |= MNCC_F_CALLED;
606 setup.called.type = 0; /* unknown */
607 setup.called.plan = 0; /* unknown */
608 OSMO_STRLCPY_ARRAY(setup.called.number,
609 trans->net->emergency.route_to_msisdn);
610 }
611 }
612
613 /* use subscriber as calling party number */
614 setup.fields |= MNCC_F_CALLING;
615 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
616 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
617
618 /* bearer capability */
619 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
620 setup.fields |= MNCC_F_BEARER_CAP;
621 gsm48_decode_bearer_cap(&setup.bearer_cap,
622 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
623
624 /* Create a copy of the bearer capability
625 * in the transaction struct, so we can use
626 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100627 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200628 sizeof(trans->bearer_cap));
629 }
630 /* facility */
631 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
632 setup.fields |= MNCC_F_FACILITY;
633 gsm48_decode_facility(&setup.facility,
634 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
635 }
636 /* called party bcd number */
637 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
638 setup.fields |= MNCC_F_CALLED;
639 gsm48_decode_called(&setup.called,
640 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
641 }
642 /* user-user */
643 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
644 setup.fields |= MNCC_F_USERUSER;
645 gsm48_decode_useruser(&setup.useruser,
646 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
647 }
648 /* ss-version */
649 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
650 setup.fields |= MNCC_F_SSVERSION;
651 gsm48_decode_ssversion(&setup.ssversion,
652 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
653 }
654 /* CLIR suppression */
655 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
656 setup.clir.sup = 1;
657 /* CLIR invocation */
658 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
659 setup.clir.inv = 1;
660 /* cc cap */
661 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
662 setup.fields |= MNCC_F_CCCAP;
663 gsm48_decode_cccap(&setup.cccap,
664 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
665 }
666
667 new_cc_state(trans, GSM_CSTATE_INITIATED);
668
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100669 /* MO call leg starting, gather all codec information so far known: */
670 codec_filter_init(&trans->cc.codecs);
671 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
672 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
673 if (setup.fields & MNCC_F_BEARER_CAP)
674 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
675 codec_filter_run(&trans->cc.codecs);
676
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100677 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
678 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100679 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200680
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200681 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200682
683 /* indicate setup to MNCC */
684 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
685
686 /* MNCC code will modify the channel asynchronously, we should
687 * ipaccess-bind only after the modification has been made to the
688 * lchan->tch_mode */
689 return 0;
690}
691
692static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
693{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100694 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200695 struct gsm48_hdr *gh;
696 struct gsm_mncc *setup = arg;
697 int rc, trans_id;
698
699 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
700
701 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700702 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100703 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200704 "This is not allowed!\n");
705 /* Temporarily out of order */
706 rc = mncc_release_ind(trans->net, trans, trans->callref,
707 GSM48_CAUSE_LOC_PRN_S_LU,
708 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
709 trans->callref = 0;
710 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200711 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200712 return rc;
713 }
714
715 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100716 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200717 if (trans_id < 0) {
718 /* no free transaction ID */
719 rc = mncc_release_ind(trans->net, trans, trans->callref,
720 GSM48_CAUSE_LOC_PRN_S_LU,
721 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
722 trans->callref = 0;
723 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200724 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200725 return rc;
726 }
727 trans->transaction_id = trans_id;
728
729 gh->msg_type = GSM48_MT_CC_SETUP;
730
731 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
732
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100733 /* MT call leg is starting. Gather all codecs information so far known.
734 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
735 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
736 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
737 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
738 * Capabilities. */
739 codec_filter_init(&trans->cc.codecs);
740 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
741 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100742 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
743 if (setup->sdp[0]) {
744 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
745 if (rc)
746 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
747 }
748 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
749 * MNCC, if any. */
750 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
751 trans->cc.codecs.remote = (struct sdp_msg){};
752 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
753 &setup->bearer_cap);
754 }
755 if (!trans->cc.codecs.remote.audio_codecs.count)
756 LOG_TRANS(trans, LOGL_INFO,
757 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
758
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100759 codec_filter_run(&trans->cc.codecs);
760 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
761
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100762 /* NEAR FUTURE: upcoming patch will use the codecs filter to determine the Bearer Cap to send to the MS.
763 * So far just gathering information in the new codecs filter. */
Harald Welte27989d42018-06-21 20:39:20 +0200764 /* bearer capability */
765 if (setup->fields & MNCC_F_BEARER_CAP) {
766 /* Create a copy of the bearer capability in the transaction struct, so we
767 * can use this information later */
768 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
769 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
770 }
771 /* facility */
772 if (setup->fields & MNCC_F_FACILITY)
773 gsm48_encode_facility(msg, 0, &setup->facility);
774 /* progress */
775 if (setup->fields & MNCC_F_PROGRESS)
776 gsm48_encode_progress(msg, 0, &setup->progress);
777 /* calling party BCD number */
778 if (setup->fields & MNCC_F_CALLING)
779 gsm48_encode_calling(msg, &setup->calling);
780 /* called party BCD number */
781 if (setup->fields & MNCC_F_CALLED)
782 gsm48_encode_called(msg, &setup->called);
783 /* user-user */
784 if (setup->fields & MNCC_F_USERUSER)
785 gsm48_encode_useruser(msg, 0, &setup->useruser);
786 /* redirecting party BCD number */
787 if (setup->fields & MNCC_F_REDIRECTING)
788 gsm48_encode_redirecting(msg, &setup->redirecting);
789 /* signal */
790 if (setup->fields & MNCC_F_SIGNAL)
791 gsm48_encode_signal(msg, setup->signal);
792
793 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
794
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200795 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200796
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100797 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200798}
799
800static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
801{
802 struct gsm48_hdr *gh = msgb_l3(msg);
803 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
804 struct tlv_parsed tp;
805 struct gsm_mncc call_conf;
806 int rc;
807
808 gsm48_stop_cc_timer(trans);
809 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
810
811 memset(&call_conf, 0, sizeof(struct gsm_mncc));
812 call_conf.callref = trans->callref;
813
814 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
815#if 0
816 /* repeat */
817 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
818 call_conf.repeat = 1;
819 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
820 call_conf.repeat = 2;
821#endif
822 /* bearer capability */
823 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
824 call_conf.fields |= MNCC_F_BEARER_CAP;
825 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
826 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
827
828 /* Create a copy of the bearer capability
829 * in the transaction struct, so we can use
830 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100831 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200832 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +0100833
834 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
835 * Store in codecs filter. */
836 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200837 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100838
Harald Welte27989d42018-06-21 20:39:20 +0200839 /* cause */
840 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
841 call_conf.fields |= MNCC_F_CAUSE;
842 gsm48_decode_cause(&call_conf.cause,
843 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
844 }
845 /* cc cap */
846 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
847 call_conf.fields |= MNCC_F_CCCAP;
848 gsm48_decode_cccap(&call_conf.cccap,
849 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
850 }
851
852 /* IMSI of called subscriber */
853 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
854
855 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
856
857 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100858 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200859
860 /* don't continue, if there were problems with
861 * the call assignment. */
862 if (rc)
863 return rc;
864
865 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
866 &call_conf);
867}
868
869static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
870{
871 struct gsm_mncc *proceeding = arg;
872 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
873 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
874 int rc;
875
876 gh->msg_type = GSM48_MT_CC_CALL_PROC;
877
878 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
879
880 /* bearer capability */
881 if (proceeding->fields & MNCC_F_BEARER_CAP) {
882 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
883 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
884 }
885 /* facility */
886 if (proceeding->fields & MNCC_F_FACILITY)
887 gsm48_encode_facility(msg, 0, &proceeding->facility);
888 /* progress */
889 if (proceeding->fields & MNCC_F_PROGRESS)
890 gsm48_encode_progress(msg, 0, &proceeding->progress);
891
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100892 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200893 if (rc)
894 return rc;
895
896 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100897 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200898}
899
900static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
901{
902 struct gsm48_hdr *gh = msgb_l3(msg);
903 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
904 struct tlv_parsed tp;
905 struct gsm_mncc alerting;
906
907 gsm48_stop_cc_timer(trans);
908 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
909
910 memset(&alerting, 0, sizeof(struct gsm_mncc));
911 alerting.callref = trans->callref;
912 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
913 /* facility */
914 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
915 alerting.fields |= MNCC_F_FACILITY;
916 gsm48_decode_facility(&alerting.facility,
917 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
918 }
919
920 /* progress */
921 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
922 alerting.fields |= MNCC_F_PROGRESS;
923 gsm48_decode_progress(&alerting.progress,
924 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
925 }
926 /* ss-version */
927 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
928 alerting.fields |= MNCC_F_SSVERSION;
929 gsm48_decode_ssversion(&alerting.ssversion,
930 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
931 }
932
933 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
934
935 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
936 &alerting);
937}
938
939static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
940{
941 struct gsm_mncc *alerting = arg;
942 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
943 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
944
945 gh->msg_type = GSM48_MT_CC_ALERTING;
946
947 /* facility */
948 if (alerting->fields & MNCC_F_FACILITY)
949 gsm48_encode_facility(msg, 0, &alerting->facility);
950 /* progress */
951 if (alerting->fields & MNCC_F_PROGRESS)
952 gsm48_encode_progress(msg, 0, &alerting->progress);
953 /* user-user */
954 if (alerting->fields & MNCC_F_USERUSER)
955 gsm48_encode_useruser(msg, 0, &alerting->useruser);
956
957 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
958
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100959 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200960}
961
962static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
963{
964 struct gsm_mncc *progress = arg;
965 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
966 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
967
968 gh->msg_type = GSM48_MT_CC_PROGRESS;
969
970 /* progress */
971 gsm48_encode_progress(msg, 1, &progress->progress);
972 /* user-user */
973 if (progress->fields & MNCC_F_USERUSER)
974 gsm48_encode_useruser(msg, 0, &progress->useruser);
975
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100976 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200977}
978
979static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
980{
981 struct gsm_mncc *connect = arg;
982 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
983 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
984
985 gh->msg_type = GSM48_MT_CC_CONNECT;
986
987 gsm48_stop_cc_timer(trans);
988 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
989
990 /* facility */
991 if (connect->fields & MNCC_F_FACILITY)
992 gsm48_encode_facility(msg, 0, &connect->facility);
993 /* progress */
994 if (connect->fields & MNCC_F_PROGRESS)
995 gsm48_encode_progress(msg, 0, &connect->progress);
996 /* connected number */
997 if (connect->fields & MNCC_F_CONNECTED)
998 gsm48_encode_connected(msg, &connect->connected);
999 /* user-user */
1000 if (connect->fields & MNCC_F_USERUSER)
1001 gsm48_encode_useruser(msg, 0, &connect->useruser);
1002
1003 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1004
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001005 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001006}
1007
1008static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1009{
1010 struct gsm48_hdr *gh = msgb_l3(msg);
1011 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1012 struct tlv_parsed tp;
1013 struct gsm_mncc connect;
1014
1015 gsm48_stop_cc_timer(trans);
1016
1017 memset(&connect, 0, sizeof(struct gsm_mncc));
1018 connect.callref = trans->callref;
1019 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1020 /* use subscriber as connected party number */
1021 connect.fields |= MNCC_F_CONNECTED;
1022 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1023 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1024
1025 /* facility */
1026 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1027 connect.fields |= MNCC_F_FACILITY;
1028 gsm48_decode_facility(&connect.facility,
1029 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1030 }
1031 /* user-user */
1032 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1033 connect.fields |= MNCC_F_USERUSER;
1034 gsm48_decode_useruser(&connect.useruser,
1035 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1036 }
1037 /* ss-version */
1038 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1039 connect.fields |= MNCC_F_SSVERSION;
1040 gsm48_decode_ssversion(&connect.ssversion,
1041 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1042 }
1043
1044 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001045 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001046
1047 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1048}
1049
1050
1051static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1052{
1053 struct gsm_mncc connect_ack;
1054
1055 gsm48_stop_cc_timer(trans);
1056
1057 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001058 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 +02001059
1060 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1061 connect_ack.callref = trans->callref;
1062
1063 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1064 &connect_ack);
1065}
1066
1067static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1068{
1069 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1070 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1071
1072 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1073
1074 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1075
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001076 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001077}
1078
1079static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1080{
1081 struct gsm48_hdr *gh = msgb_l3(msg);
1082 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1083 struct tlv_parsed tp;
1084 struct gsm_mncc disc;
1085
1086 gsm48_stop_cc_timer(trans);
1087
1088 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1089
1090 memset(&disc, 0, sizeof(struct gsm_mncc));
1091 disc.callref = trans->callref;
1092 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1093 /* cause */
1094 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1095 disc.fields |= MNCC_F_CAUSE;
1096 gsm48_decode_cause(&disc.cause,
1097 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1098 }
1099 /* facility */
1100 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1101 disc.fields |= MNCC_F_FACILITY;
1102 gsm48_decode_facility(&disc.facility,
1103 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1104 }
1105 /* user-user */
1106 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1107 disc.fields |= MNCC_F_USERUSER;
1108 gsm48_decode_useruser(&disc.useruser,
1109 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1110 }
1111 /* ss-version */
1112 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1113 disc.fields |= MNCC_F_SSVERSION;
1114 gsm48_decode_ssversion(&disc.ssversion,
1115 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1116 }
1117
1118 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001119}
1120
1121static struct gsm_mncc_cause default_cause = {
1122 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1123 .coding = 0,
1124 .rec = 0,
1125 .rec_val = 0,
1126 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1127 .diag_len = 0,
1128 .diag = { 0 },
1129};
1130
1131static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1132{
1133 struct gsm_mncc *disc = arg;
1134 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1135 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1136
1137 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1138
1139 gsm48_stop_cc_timer(trans);
1140 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1141
1142 /* cause */
1143 if (disc->fields & MNCC_F_CAUSE)
1144 gsm48_encode_cause(msg, 1, &disc->cause);
1145 else
1146 gsm48_encode_cause(msg, 1, &default_cause);
1147
1148 /* facility */
1149 if (disc->fields & MNCC_F_FACILITY)
1150 gsm48_encode_facility(msg, 0, &disc->facility);
1151 /* progress */
1152 if (disc->fields & MNCC_F_PROGRESS)
1153 gsm48_encode_progress(msg, 0, &disc->progress);
1154 /* user-user */
1155 if (disc->fields & MNCC_F_USERUSER)
1156 gsm48_encode_useruser(msg, 0, &disc->useruser);
1157
1158 /* store disconnect cause for T306 expiry */
1159 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1160
1161 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1162
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001163 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001164}
1165
1166static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1167{
1168 struct gsm48_hdr *gh = msgb_l3(msg);
1169 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1170 struct tlv_parsed tp;
1171 struct gsm_mncc rel;
1172 int rc;
1173
1174 gsm48_stop_cc_timer(trans);
1175
1176 memset(&rel, 0, sizeof(struct gsm_mncc));
1177 rel.callref = trans->callref;
1178 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1179 /* cause */
1180 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1181 rel.fields |= MNCC_F_CAUSE;
1182 gsm48_decode_cause(&rel.cause,
1183 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1184 }
1185 /* facility */
1186 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1187 rel.fields |= MNCC_F_FACILITY;
1188 gsm48_decode_facility(&rel.facility,
1189 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1190 }
1191 /* user-user */
1192 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1193 rel.fields |= MNCC_F_USERUSER;
1194 gsm48_decode_useruser(&rel.useruser,
1195 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1196 }
1197 /* ss-version */
1198 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1199 rel.fields |= MNCC_F_SSVERSION;
1200 gsm48_decode_ssversion(&rel.ssversion,
1201 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1202 }
1203
1204 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1205 /* release collision 5.4.5 */
1206 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1207 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001208 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001209 GSM48_PDISC_CC | (trans->transaction_id << 4),
1210 GSM48_MT_CC_RELEASE_COMPL);
1211 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1212 }
1213
1214 new_cc_state(trans, GSM_CSTATE_NULL);
1215
1216 trans->callref = 0;
1217 trans_free(trans);
1218
1219 return rc;
1220}
1221
1222static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1223{
1224 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001225 struct msgb *msg;
1226 struct gsm48_hdr *gh;
1227
1228 if (!trans->msc_a) {
1229 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1230 return -EINVAL;
1231 }
1232
1233 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1234 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001235
1236 gh->msg_type = GSM48_MT_CC_RELEASE;
1237
1238 gsm48_stop_cc_timer(trans);
1239 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1240
1241 /* cause */
1242 if (rel->fields & MNCC_F_CAUSE)
1243 gsm48_encode_cause(msg, 0, &rel->cause);
1244 /* facility */
1245 if (rel->fields & MNCC_F_FACILITY)
1246 gsm48_encode_facility(msg, 0, &rel->facility);
1247 /* user-user */
1248 if (rel->fields & MNCC_F_USERUSER)
1249 gsm48_encode_useruser(msg, 0, &rel->useruser);
1250
1251 trans->cc.T308_second = 0;
1252 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1253
1254 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1255 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1256
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001257 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001258}
1259
1260static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1261{
1262 struct gsm48_hdr *gh = msgb_l3(msg);
1263 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1264 struct tlv_parsed tp;
1265 struct gsm_mncc rel;
1266 int rc = 0;
1267
1268 gsm48_stop_cc_timer(trans);
1269
1270 memset(&rel, 0, sizeof(struct gsm_mncc));
1271 rel.callref = trans->callref;
1272 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1273 /* cause */
1274 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1275 rel.fields |= MNCC_F_CAUSE;
1276 gsm48_decode_cause(&rel.cause,
1277 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1278 }
1279 /* facility */
1280 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1281 rel.fields |= MNCC_F_FACILITY;
1282 gsm48_decode_facility(&rel.facility,
1283 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1284 }
1285 /* user-user */
1286 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1287 rel.fields |= MNCC_F_USERUSER;
1288 gsm48_decode_useruser(&rel.useruser,
1289 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1290 }
1291 /* ss-version */
1292 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1293 rel.fields |= MNCC_F_SSVERSION;
1294 gsm48_decode_ssversion(&rel.ssversion,
1295 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1296 }
1297
1298 if (trans->callref) {
1299 switch (trans->cc.state) {
1300 case GSM_CSTATE_CALL_PRESENT:
1301 rc = mncc_recvmsg(trans->net, trans,
1302 MNCC_REJ_IND, &rel);
1303 break;
1304 case GSM_CSTATE_RELEASE_REQ:
1305 rc = mncc_recvmsg(trans->net, trans,
1306 MNCC_REL_CNF, &rel);
1307 break;
1308 default:
1309 rc = mncc_recvmsg(trans->net, trans,
1310 MNCC_REL_IND, &rel);
1311 }
1312 }
1313
1314 trans->callref = 0;
1315 trans_free(trans);
1316
1317 return rc;
1318}
1319
1320static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1321{
1322 struct gsm_mncc *rel = arg;
1323 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1324 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1325 int ret;
1326
1327 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1328
1329 trans->callref = 0;
1330
1331 gsm48_stop_cc_timer(trans);
1332
1333 /* cause */
1334 if (rel->fields & MNCC_F_CAUSE)
1335 gsm48_encode_cause(msg, 0, &rel->cause);
1336 /* facility */
1337 if (rel->fields & MNCC_F_FACILITY)
1338 gsm48_encode_facility(msg, 0, &rel->facility);
1339 /* user-user */
1340 if (rel->fields & MNCC_F_USERUSER)
1341 gsm48_encode_useruser(msg, 0, &rel->useruser);
1342
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001343 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001344
1345 trans_free(trans);
1346
1347 return ret;
1348}
1349
1350static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1351{
1352 struct gsm48_hdr *gh = msgb_l3(msg);
1353 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1354 struct tlv_parsed tp;
1355 struct gsm_mncc fac;
1356
1357 memset(&fac, 0, sizeof(struct gsm_mncc));
1358 fac.callref = trans->callref;
1359 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1360 /* facility */
1361 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1362 fac.fields |= MNCC_F_FACILITY;
1363 gsm48_decode_facility(&fac.facility,
1364 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1365 }
1366 /* ss-version */
1367 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1368 fac.fields |= MNCC_F_SSVERSION;
1369 gsm48_decode_ssversion(&fac.ssversion,
1370 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1371 }
1372
1373 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1374}
1375
1376static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1377{
1378 struct gsm_mncc *fac = arg;
1379 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1380 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1381
1382 gh->msg_type = GSM48_MT_CC_FACILITY;
1383
1384 /* facility */
1385 gsm48_encode_facility(msg, 1, &fac->facility);
1386
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001387 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001388}
1389
1390static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1391{
1392 struct gsm_mncc hold;
1393
1394 memset(&hold, 0, sizeof(struct gsm_mncc));
1395 hold.callref = trans->callref;
1396 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1397}
1398
1399static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1400{
1401 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1402 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1403
1404 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1405
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001406 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001407}
1408
1409static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1410{
1411 struct gsm_mncc *hold_rej = arg;
1412 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1413 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1414
1415 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1416
1417 /* cause */
1418 if (hold_rej->fields & MNCC_F_CAUSE)
1419 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1420 else
1421 gsm48_encode_cause(msg, 1, &default_cause);
1422
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001423 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001424}
1425
1426static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1427{
1428 struct gsm_mncc retrieve;
1429
1430 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1431 retrieve.callref = trans->callref;
1432 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1433 &retrieve);
1434}
1435
1436static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1437{
1438 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1439 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1440
1441 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1442
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001443 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001444}
1445
1446static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1447{
1448 struct gsm_mncc *retrieve_rej = arg;
1449 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1450 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1451
1452 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1453
1454 /* cause */
1455 if (retrieve_rej->fields & MNCC_F_CAUSE)
1456 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1457 else
1458 gsm48_encode_cause(msg, 1, &default_cause);
1459
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001460 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001461}
1462
1463static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1464{
1465 struct gsm48_hdr *gh = msgb_l3(msg);
1466 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1467 struct tlv_parsed tp;
1468 struct gsm_mncc dtmf;
1469
1470 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1471 dtmf.callref = trans->callref;
1472 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1473 /* keypad facility */
1474 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1475 dtmf.fields |= MNCC_F_KEYPAD;
1476 gsm48_decode_keypad(&dtmf.keypad,
1477 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1478 }
1479
1480 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1481}
1482
1483static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1484{
1485 struct gsm_mncc *dtmf = arg;
1486 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1487 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1488
1489 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1490
1491 /* keypad */
1492 if (dtmf->fields & MNCC_F_KEYPAD)
1493 gsm48_encode_keypad(msg, dtmf->keypad);
1494
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001495 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001496}
1497
1498static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1499{
1500 struct gsm_mncc *dtmf = arg;
1501 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1502 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1503
1504 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1505
1506 /* cause */
1507 if (dtmf->fields & MNCC_F_CAUSE)
1508 gsm48_encode_cause(msg, 1, &dtmf->cause);
1509 else
1510 gsm48_encode_cause(msg, 1, &default_cause);
1511
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001512 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001513}
1514
1515static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1516{
1517 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1518 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1519
1520 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1521
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001522 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001523}
1524
1525static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1526{
1527 struct gsm_mncc dtmf;
1528
1529 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1530 dtmf.callref = trans->callref;
1531
1532 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1533}
1534
1535static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1536{
1537 struct gsm48_hdr *gh = msgb_l3(msg);
1538 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1539 struct tlv_parsed tp;
1540 struct gsm_mncc modify;
1541
1542 memset(&modify, 0, sizeof(struct gsm_mncc));
1543 modify.callref = trans->callref;
1544 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1545 /* bearer capability */
1546 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1547 modify.fields |= MNCC_F_BEARER_CAP;
1548 gsm48_decode_bearer_cap(&modify.bearer_cap,
1549 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1550
1551 /* Create a copy of the bearer capability
1552 * in the transaction struct, so we can use
1553 * this information later */
1554 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1555 sizeof(trans->bearer_cap));
1556 }
1557
1558 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1559
1560 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1561}
1562
1563static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1564{
1565 struct gsm_mncc *modify = arg;
1566 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1567 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1568
1569 gh->msg_type = GSM48_MT_CC_MODIFY;
1570
1571 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1572
1573 /* bearer capability */
1574 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1575 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1576
1577 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1578
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001579 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001580}
1581
1582static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1583{
1584 struct gsm48_hdr *gh = msgb_l3(msg);
1585 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1586 struct tlv_parsed tp;
1587 struct gsm_mncc modify;
1588
1589 gsm48_stop_cc_timer(trans);
1590
1591 memset(&modify, 0, sizeof(struct gsm_mncc));
1592 modify.callref = trans->callref;
1593 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1594 /* bearer capability */
1595 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1596 modify.fields |= MNCC_F_BEARER_CAP;
1597 gsm48_decode_bearer_cap(&modify.bearer_cap,
1598 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1599
1600 /* Create a copy of the bearer capability
1601 * in the transaction struct, so we can use
1602 * this information later */
1603 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1604 sizeof(trans->bearer_cap));
1605 }
1606
1607 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1608
1609 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1610}
1611
1612static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1613{
1614 struct gsm_mncc *modify = arg;
1615 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1616 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1617
1618 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1619
1620 /* bearer capability */
1621 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1622 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1623
1624 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1625
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001626 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001627}
1628
1629static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1630{
1631 struct gsm48_hdr *gh = msgb_l3(msg);
1632 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1633 struct tlv_parsed tp;
1634 struct gsm_mncc modify;
1635
1636 gsm48_stop_cc_timer(trans);
1637
1638 memset(&modify, 0, sizeof(struct gsm_mncc));
1639 modify.callref = trans->callref;
1640 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1641 /* bearer capability */
1642 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1643 modify.fields |= GSM48_IE_BEARER_CAP;
1644 gsm48_decode_bearer_cap(&modify.bearer_cap,
1645 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1646
1647 /* Create a copy of the bearer capability
1648 * in the transaction struct, so we can use
1649 * this information later */
1650 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1651 sizeof(trans->bearer_cap));
1652 }
1653 /* cause */
1654 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1655 modify.fields |= MNCC_F_CAUSE;
1656 gsm48_decode_cause(&modify.cause,
1657 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1658 }
1659
1660 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1661
1662 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1663}
1664
1665static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1666{
1667 struct gsm_mncc *modify = arg;
1668 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1670
1671 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1672
1673 /* bearer capability */
1674 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1675 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1676 /* cause */
1677 gsm48_encode_cause(msg, 1, &modify->cause);
1678
1679 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1680
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001681 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001682}
1683
1684static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1685{
1686 struct gsm_mncc *notify = arg;
1687 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1688 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1689
1690 gh->msg_type = GSM48_MT_CC_NOTIFY;
1691
1692 /* notify */
1693 gsm48_encode_notify(msg, notify->notify);
1694
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001695 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001696}
1697
1698static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1699{
1700 struct gsm48_hdr *gh = msgb_l3(msg);
1701 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1702// struct tlv_parsed tp;
1703 struct gsm_mncc notify;
1704
1705 memset(&notify, 0, sizeof(struct gsm_mncc));
1706 notify.callref = trans->callref;
1707// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1708 if (payload_len >= 1)
1709 gsm48_decode_notify(&notify.notify, gh->data);
1710
1711 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1712}
1713
1714static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1715{
1716 struct gsm_mncc *user = arg;
1717 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1719
1720 gh->msg_type = GSM48_MT_CC_USER_INFO;
1721
1722 /* user-user */
1723 if (user->fields & MNCC_F_USERUSER)
1724 gsm48_encode_useruser(msg, 1, &user->useruser);
1725 /* more data */
1726 if (user->more)
1727 gsm48_encode_more(msg);
1728
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001729 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001730}
1731
1732static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1733{
1734 struct gsm48_hdr *gh = msgb_l3(msg);
1735 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1736 struct tlv_parsed tp;
1737 struct gsm_mncc user;
1738
1739 memset(&user, 0, sizeof(struct gsm_mncc));
1740 user.callref = trans->callref;
1741 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1742 /* user-user */
1743 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1744 user.fields |= MNCC_F_USERUSER;
1745 gsm48_decode_useruser(&user.useruser,
1746 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1747 }
1748 /* more data */
1749 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1750 user.more = 1;
1751
1752 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1753}
1754
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001755static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1756 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1757 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001758{
1759 uint8_t data[sizeof(struct gsm_mncc)];
1760 struct gsm_mncc_rtp *rtp;
1761
1762 memset(&data, 0, sizeof(data));
1763 rtp = (struct gsm_mncc_rtp *) &data[0];
1764
1765 rtp->callref = callref;
1766 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001767 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001768 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1769 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001770 }
Harald Welte27989d42018-06-21 20:39:20 +02001771 rtp->payload_type = payload_type;
1772 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001773 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001774}
1775
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001776static 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 +02001777{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001778 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001779}
1780
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001781static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001782{
1783 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001784
1785 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001786 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001787 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001788 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001789 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001790 return -EIO;
1791 }
1792 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001793 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001794 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001795 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001796 return 0;
1797 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001798 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001799
Harald Welte27989d42018-06-21 20:39:20 +02001800 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001801 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001802}
1803
1804/* Trigger TCH_RTP_CREATE acknowledgement */
1805int gsm48_tch_rtp_create(struct gsm_trans *trans)
1806{
1807 /* This function is called as soon as the port, on which the
1808 * mgcp-gw expects the incoming RTP stream from the remote
1809 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001810 struct msc_a *msc_a = trans->msc_a;
1811 struct gsm_network *net = msc_a_net(msc_a);
1812 struct call_leg *cl = msc_a->cc.call_leg;
1813 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001814 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1815 uint32_t payload_type;
1816 int payload_msg_type;
1817 const struct mgcp_conn_peer *mgcp_info;
Neels Hofmeyra001a702022-10-31 17:57:30 +01001818 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001819
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001820 if (!rtp_cn) {
1821 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1822 return -EINVAL;
1823 }
1824
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001825 if (!rtp_cn->codecs_known) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001826 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1827 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1828 return -EINVAL;
1829 }
1830
1831 /* Codec */
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001832 m = codec_mapping_by_subtype_name(rtp_cn->codecs.codec[0].subtype_name);
Neels Hofmeyra001a702022-10-31 17:57:30 +01001833 if (!m) {
1834 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1835 "Cannot RTP CREATE to MNCC, cannot resolve codec '%s'\n",
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001836 sdp_audio_codec_to_str(&rtp_cn->codecs.codec[0]));
Neels Hofmeyra001a702022-10-31 17:57:30 +01001837 return -EINVAL;
1838 }
1839 payload_msg_type = m->mncc_payload_msg_type;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001840
1841 /* Payload Type number */
1842 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001843 if (mgcp_info && mgcp_info->ptmap_len)
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001844 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, m->mgcp);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001845 else
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001846 payload_type = m->mgcp;
Harald Welte27989d42018-06-21 20:39:20 +02001847
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001848 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1849 if (!rtp_cn_local) {
1850 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1851 return -EINVAL;
1852 }
1853
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001854 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, payload_type, payload_msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001855}
1856
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001857static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001858{
1859 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001860 struct call_leg *cl;
1861 struct rtp_stream *rtps;
1862 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001863
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001864 /* FIXME: in *rtp we should get the codec information of the remote
1865 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1866 * meaningful value based on this information but unfortunately we
1867 * can't do that yet because the mncc API can not signal dynamic
1868 * payload types yet. This must be fixed first. Also there may be
1869 * additional members necessary in trans->conn->rtp because we
1870 * somehow need to deal with dynamic payload types that do not
1871 * comply to 3gpp's assumptions of payload type numbers on the A
1872 * interface. See also related tickets: OS#3399 and OS1683 */
1873
Harald Welte27989d42018-06-21 20:39:20 +02001874 /* Find callref */
1875 trans = trans_find_by_callref(net, rtp->callref);
1876 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001877 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001878 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001879 return -EIO;
1880 }
1881 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001882 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001883 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001884 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001885 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001886 }
1887
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001888 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001889
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001890 cl = trans->msc_a->cc.call_leg;
1891 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1892
1893 if (!rtps) {
1894 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1895 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1896 return -EINVAL;
1897 }
1898
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001899 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1900 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1901 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1902 return -EINVAL;
1903 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001904 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1905 rtp_stream_commit(rtps);
1906 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001907}
1908
1909static struct downstate {
1910 uint32_t states;
1911 int type;
1912 int (*rout) (struct gsm_trans *trans, void *arg);
1913} downstatelist[] = {
1914 /* mobile originating call establishment */
1915 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1916 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1917 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1918 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1919 {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 */
1920 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1921 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1922 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1923 /* mobile terminating call establishment */
1924 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1925 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1926 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1927 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1928 /* signalling during call */
1929 {SBIT(GSM_CSTATE_ACTIVE),
1930 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1931 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1932 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1933 {ALL_STATES,
1934 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1935 {ALL_STATES,
1936 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1937 {ALL_STATES,
1938 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1939 {SBIT(GSM_CSTATE_ACTIVE),
1940 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1941 {SBIT(GSM_CSTATE_ACTIVE),
1942 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1943 {SBIT(GSM_CSTATE_ACTIVE),
1944 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1945 {SBIT(GSM_CSTATE_ACTIVE),
1946 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1947 {SBIT(GSM_CSTATE_ACTIVE),
1948 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1949 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1950 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1951 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1952 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1953 {SBIT(GSM_CSTATE_ACTIVE),
1954 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1955 /* clearing */
1956 {SBIT(GSM_CSTATE_INITIATED),
1957 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1958 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1959 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1960 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1961 MNCC_REL_REQ, gsm48_cc_tx_release},
1962};
1963
1964#define DOWNSLLEN \
1965 (sizeof(downstatelist) / sizeof(struct downstate))
1966
1967
Philipp Maiercd64af72019-08-01 09:46:40 +02001968static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001969{
1970 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001971 struct msc_a *msc_a = NULL;
1972 struct gsm_trans *trans = NULL;
1973 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001974
Harald Welte27989d42018-06-21 20:39:20 +02001975 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001976 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001977 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001978 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001979 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001980 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001981 return rc;
1982 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001983 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001984 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001985 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001986 case MNCC_RTP_FREE:
1987 /* unused right now */
1988 return -EIO;
1989
1990 case MNCC_FRAME_DROP:
1991 case MNCC_FRAME_RECV:
1992 case GSM_TCHF_FRAME:
1993 case GSM_TCHF_FRAME_EFR:
1994 case GSM_TCHH_FRAME:
1995 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001996 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001998 return -ENOTSUP;
1999 }
2000
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002001 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002002
2003 /* Find callref */
2004 trans = trans_find_by_callref(net, data->callref);
2005
2006 /* Callref unknown */
2007 if (!trans) {
2008 struct vlr_subscr *vsub;
2009
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002010 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002011 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002012 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002013 /* Invalid call reference */
2014 return mncc_release_ind(net, NULL, data->callref,
2015 GSM48_CAUSE_LOC_PRN_S_LU,
2016 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2017 }
2018 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002019 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002020 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002021 /* Invalid number */
2022 return mncc_release_ind(net, NULL, data->callref,
2023 GSM48_CAUSE_LOC_PRN_S_LU,
2024 GSM48_CC_CAUSE_INV_NR_FORMAT);
2025 }
2026 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002027 if (data->called.number[0]) {
2028 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2029 if (!vsub)
2030 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002031 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002032 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002033 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002034 if (!vsub)
2035 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002036 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002037 }
2038 if (!vsub)
2039 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002040 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002041 /* update the subscriber we deal with */
2042 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2043
Harald Welte27989d42018-06-21 20:39:20 +02002044 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002045 if (!vsub->lu_complete) {
2046 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002047 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002048 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002049 /* Temporarily out of order */
2050 return mncc_release_ind(net, NULL, data->callref,
2051 GSM48_CAUSE_LOC_PRN_S_LU,
2052 GSM48_CC_CAUSE_DEST_OOO);
2053 }
Keith Whyte991bb422019-08-08 15:43:40 +02002054
2055 /* Find valid conn */
2056 msc_a = msc_a_for_vsub(vsub, true);
2057
2058 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2059 if (!net->call_waiting && msc_a) {
2060 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2061 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2062 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2063 "rx '%s' for subscriber %s with trans state (%s)"
2064 " rejecting with USER_BUSY\n",
2065 get_mncc_name(msg->msg_type), data->called.number,
2066 gsm48_cc_state_name(existing_cc_trans->cc.state));
2067 return mncc_release_ind(net, NULL, data->callref,
2068 GSM48_CAUSE_LOC_PRN_S_LU,
2069 GSM48_CC_CAUSE_USER_BUSY);
2070 }
2071 }
2072
Harald Welte27989d42018-06-21 20:39:20 +02002073 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002074 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002075 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002076 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002077 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002078 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002079 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002080 mncc_release_ind(net, NULL, data->callref,
2081 GSM48_CAUSE_LOC_PRN_S_LU,
2082 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2083 return -ENOMEM;
2084 }
2085
Harald Welte27989d42018-06-21 20:39:20 +02002086 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002087 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002088 /* This condition will return before the common logging of the received MNCC message below, so
2089 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002090 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002091
Harald Welte27989d42018-06-21 20:39:20 +02002092 /* store setup information until paging succeeds */
2093 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2094
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002095 /* Request a channel. If Paging already started, paging_request_start() will append the new
2096 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002097 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2098 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002099 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002100 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002101 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002102 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002103 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002104 return 0;
2105 }
2106
2107 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002108 trans->msc_a = msc_a;
2109 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002110 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002111 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002112 } else {
2113 /* update the subscriber we deal with */
2114 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2115 }
2116
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002117 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002118
Philipp Maier9ca7b312018-10-10 17:00:49 +02002119 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002120 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002121
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002122 if (trans->msc_a)
2123 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002124
2125 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002126 if (!msc_a) {
2127 struct gsm_mncc rel = {
2128 .callref = data->callref,
2129 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002130 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002131 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2132 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002133 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002134 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2135 else
2136 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2137 trans->callref = 0;
2138 trans_free(trans);
2139 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002140 }
2141
2142 /* Find function for current state and message */
2143 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002144 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002145 && ((1 << trans->cc.state) & downstatelist[i].states))
2146 break;
2147 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002148 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002149 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002150 return 0;
2151 }
2152
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002153 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002154
2155 return rc;
2156}
2157
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002158struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2159{
2160 uint32_t callref;
2161
2162 switch (msg->msg_type) {
2163 case MNCC_BRIDGE:
2164 callref = msg->bridge.callref[0];
2165 break;
2166 case MNCC_RTP_CREATE:
2167 case MNCC_RTP_CONNECT:
2168 callref = msg->rtp.callref;
2169 break;
2170
2171 case MNCC_RTP_FREE:
2172 case MNCC_FRAME_DROP:
2173 case MNCC_FRAME_RECV:
2174 case GSM_TCHF_FRAME:
2175 case GSM_TCHF_FRAME_EFR:
2176 case GSM_TCHH_FRAME:
2177 case GSM_TCH_FRAME_AMR:
2178 return NULL;
2179
2180 default:
2181 callref = msg->signal.callref;
2182 break;
2183 }
2184
2185 return mncc_call_find_by_callref(callref);
2186}
2187
2188/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002189int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002190{
2191 const union mncc_msg *msg = arg;
2192 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002193
2194 if (msg->msg_type == MNCC_SETUP_REQ) {
2195 /* Incoming call to forward for inter-MSC Handover? */
2196 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2197 if (mncc_call)
2198 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2199 "Incoming call matches pending inter-MSC Handover Number\n");
2200 }
2201 if (!mncc_call) {
2202 /* Find already active MNCC FSM for this callref.
2203 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2204 * MNCC<->GSM-CC call handling. */
2205 mncc_call = mncc_find_by_callref_from_msg(msg);
2206 }
2207 if (mncc_call) {
2208 mncc_call_rx(mncc_call, msg);
2209 return 0;
2210 }
2211
2212 /* None of the above? Then it must be a normal GSM CC call related message. */
2213 return mncc_tx_to_gsm_cc(net, msg);
2214}
Harald Welte27989d42018-06-21 20:39:20 +02002215
2216static struct datastate {
2217 uint32_t states;
2218 int type;
2219 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2220} datastatelist[] = {
2221 /* mobile originating call establishment */
2222 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2223 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2224 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2225 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2226 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2227 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2228 /* mobile terminating call establishment */
2229 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2230 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2231 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2232 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2233 {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 */
2234 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2235 /* signalling during call */
2236 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2237 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2238 {SBIT(GSM_CSTATE_ACTIVE),
2239 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2240 {ALL_STATES,
2241 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2242 {ALL_STATES,
2243 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2244 {ALL_STATES,
2245 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2246 {SBIT(GSM_CSTATE_ACTIVE),
2247 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2248 {SBIT(GSM_CSTATE_ACTIVE),
2249 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2250 {SBIT(GSM_CSTATE_ACTIVE),
2251 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2252 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2253 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2254 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2255 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2256 {SBIT(GSM_CSTATE_ACTIVE),
2257 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2258 /* clearing */
2259 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2260 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2261 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2262 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2263 {ALL_STATES, /* 5.4.3.4 */
2264 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2265};
2266
2267#define DATASLLEN \
2268 (sizeof(datastatelist) / sizeof(struct datastate))
2269
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002270int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002271{
2272 struct gsm48_hdr *gh = msgb_l3(msg);
2273 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2274 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2275 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002276 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2277 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002278 int i, rc = 0;
2279
2280 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002281 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002282 return -EINVAL;
2283 }
2284
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002285 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002286 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002287 return -EINVAL;
2288 }
2289
2290 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002291 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002292
Harald Welte27989d42018-06-21 20:39:20 +02002293 /* Create transaction */
2294 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002295 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002296 trans = trans_alloc(net, vsub,
2297 TRANS_CC,
2298 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002299 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002300 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002301 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002302 GSM48_PDISC_CC | (transaction_id << 4),
2303 GSM48_MT_CC_RELEASE_COMPL);
2304 return -ENOMEM;
2305 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002306 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2307 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2308 trans_free(trans);
2309 return -EINVAL;
2310 }
2311
Harald Welte27989d42018-06-21 20:39:20 +02002312 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002313 msc_a_get(msc_a, MSC_A_USE_CC);
2314 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002315 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002316
2317 /* An earlier CM Service Request for this CC message now has concluded */
2318 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2319 LOG_MSC_A(msc_a, LOGL_ERROR,
2320 "Creating new CC transaction without prior CM Service Request\n");
2321 else
2322 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002323 }
2324
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002325 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2326 gsm48_cc_state_name(trans->cc.state));
2327
Harald Welte27989d42018-06-21 20:39:20 +02002328 /* find function for current state and message */
2329 for (i = 0; i < DATASLLEN; i++)
2330 if ((msg_type == datastatelist[i].type)
2331 && ((1 << trans->cc.state) & datastatelist[i].states))
2332 break;
2333 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002334 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002335
2336 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2337 * transaction right away. */
2338 if (trans->cc.state == GSM_CSTATE_NULL) {
2339 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2340 " -- disarding new CC transaction right away\n");
2341 trans_free(trans);
2342 }
Harald Welte27989d42018-06-21 20:39:20 +02002343 return 0;
2344 }
2345
2346 assert(trans->vsub);
2347
2348 rc = datastatelist[i].rout(trans, msg);
2349
Harald Welte27989d42018-06-21 20:39:20 +02002350 return rc;
2351}