blob: 12f2c8925b03c50e343f8869178305cb7724e955 [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));
833 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100834
Harald Welte27989d42018-06-21 20:39:20 +0200835 /* cause */
836 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
837 call_conf.fields |= MNCC_F_CAUSE;
838 gsm48_decode_cause(&call_conf.cause,
839 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
840 }
841 /* cc cap */
842 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
843 call_conf.fields |= MNCC_F_CCCAP;
844 gsm48_decode_cccap(&call_conf.cccap,
845 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
846 }
847
848 /* IMSI of called subscriber */
849 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
850
851 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
852
853 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100854 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200855
856 /* don't continue, if there were problems with
857 * the call assignment. */
858 if (rc)
859 return rc;
860
861 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
862 &call_conf);
863}
864
865static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
866{
867 struct gsm_mncc *proceeding = arg;
868 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
869 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
870 int rc;
871
872 gh->msg_type = GSM48_MT_CC_CALL_PROC;
873
874 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
875
876 /* bearer capability */
877 if (proceeding->fields & MNCC_F_BEARER_CAP) {
878 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
879 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
880 }
881 /* facility */
882 if (proceeding->fields & MNCC_F_FACILITY)
883 gsm48_encode_facility(msg, 0, &proceeding->facility);
884 /* progress */
885 if (proceeding->fields & MNCC_F_PROGRESS)
886 gsm48_encode_progress(msg, 0, &proceeding->progress);
887
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100888 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200889 if (rc)
890 return rc;
891
892 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100893 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200894}
895
896static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
897{
898 struct gsm48_hdr *gh = msgb_l3(msg);
899 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
900 struct tlv_parsed tp;
901 struct gsm_mncc alerting;
902
903 gsm48_stop_cc_timer(trans);
904 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
905
906 memset(&alerting, 0, sizeof(struct gsm_mncc));
907 alerting.callref = trans->callref;
908 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
909 /* facility */
910 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
911 alerting.fields |= MNCC_F_FACILITY;
912 gsm48_decode_facility(&alerting.facility,
913 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
914 }
915
916 /* progress */
917 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
918 alerting.fields |= MNCC_F_PROGRESS;
919 gsm48_decode_progress(&alerting.progress,
920 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
921 }
922 /* ss-version */
923 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
924 alerting.fields |= MNCC_F_SSVERSION;
925 gsm48_decode_ssversion(&alerting.ssversion,
926 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
927 }
928
929 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
930
931 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
932 &alerting);
933}
934
935static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
936{
937 struct gsm_mncc *alerting = arg;
938 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
939 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
940
941 gh->msg_type = GSM48_MT_CC_ALERTING;
942
943 /* facility */
944 if (alerting->fields & MNCC_F_FACILITY)
945 gsm48_encode_facility(msg, 0, &alerting->facility);
946 /* progress */
947 if (alerting->fields & MNCC_F_PROGRESS)
948 gsm48_encode_progress(msg, 0, &alerting->progress);
949 /* user-user */
950 if (alerting->fields & MNCC_F_USERUSER)
951 gsm48_encode_useruser(msg, 0, &alerting->useruser);
952
953 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
954
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100955 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200956}
957
958static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
959{
960 struct gsm_mncc *progress = arg;
961 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
962 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
963
964 gh->msg_type = GSM48_MT_CC_PROGRESS;
965
966 /* progress */
967 gsm48_encode_progress(msg, 1, &progress->progress);
968 /* user-user */
969 if (progress->fields & MNCC_F_USERUSER)
970 gsm48_encode_useruser(msg, 0, &progress->useruser);
971
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100972 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200973}
974
975static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
976{
977 struct gsm_mncc *connect = arg;
978 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
979 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
980
981 gh->msg_type = GSM48_MT_CC_CONNECT;
982
983 gsm48_stop_cc_timer(trans);
984 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
985
986 /* facility */
987 if (connect->fields & MNCC_F_FACILITY)
988 gsm48_encode_facility(msg, 0, &connect->facility);
989 /* progress */
990 if (connect->fields & MNCC_F_PROGRESS)
991 gsm48_encode_progress(msg, 0, &connect->progress);
992 /* connected number */
993 if (connect->fields & MNCC_F_CONNECTED)
994 gsm48_encode_connected(msg, &connect->connected);
995 /* user-user */
996 if (connect->fields & MNCC_F_USERUSER)
997 gsm48_encode_useruser(msg, 0, &connect->useruser);
998
999 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1000
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001001 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001002}
1003
1004static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1005{
1006 struct gsm48_hdr *gh = msgb_l3(msg);
1007 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1008 struct tlv_parsed tp;
1009 struct gsm_mncc connect;
1010
1011 gsm48_stop_cc_timer(trans);
1012
1013 memset(&connect, 0, sizeof(struct gsm_mncc));
1014 connect.callref = trans->callref;
1015 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1016 /* use subscriber as connected party number */
1017 connect.fields |= MNCC_F_CONNECTED;
1018 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1019 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1020
1021 /* facility */
1022 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1023 connect.fields |= MNCC_F_FACILITY;
1024 gsm48_decode_facility(&connect.facility,
1025 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1026 }
1027 /* user-user */
1028 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1029 connect.fields |= MNCC_F_USERUSER;
1030 gsm48_decode_useruser(&connect.useruser,
1031 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1032 }
1033 /* ss-version */
1034 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1035 connect.fields |= MNCC_F_SSVERSION;
1036 gsm48_decode_ssversion(&connect.ssversion,
1037 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1038 }
1039
1040 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001041 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001042
1043 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1044}
1045
1046
1047static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1048{
1049 struct gsm_mncc connect_ack;
1050
1051 gsm48_stop_cc_timer(trans);
1052
1053 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001054 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 +02001055
1056 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1057 connect_ack.callref = trans->callref;
1058
1059 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1060 &connect_ack);
1061}
1062
1063static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1064{
1065 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1066 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1067
1068 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1069
1070 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1071
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001072 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001073}
1074
1075static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1076{
1077 struct gsm48_hdr *gh = msgb_l3(msg);
1078 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1079 struct tlv_parsed tp;
1080 struct gsm_mncc disc;
1081
1082 gsm48_stop_cc_timer(trans);
1083
1084 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1085
1086 memset(&disc, 0, sizeof(struct gsm_mncc));
1087 disc.callref = trans->callref;
1088 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1089 /* cause */
1090 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1091 disc.fields |= MNCC_F_CAUSE;
1092 gsm48_decode_cause(&disc.cause,
1093 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1094 }
1095 /* facility */
1096 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1097 disc.fields |= MNCC_F_FACILITY;
1098 gsm48_decode_facility(&disc.facility,
1099 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1100 }
1101 /* user-user */
1102 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1103 disc.fields |= MNCC_F_USERUSER;
1104 gsm48_decode_useruser(&disc.useruser,
1105 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1106 }
1107 /* ss-version */
1108 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1109 disc.fields |= MNCC_F_SSVERSION;
1110 gsm48_decode_ssversion(&disc.ssversion,
1111 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1112 }
1113
1114 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001115}
1116
1117static struct gsm_mncc_cause default_cause = {
1118 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1119 .coding = 0,
1120 .rec = 0,
1121 .rec_val = 0,
1122 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1123 .diag_len = 0,
1124 .diag = { 0 },
1125};
1126
1127static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1128{
1129 struct gsm_mncc *disc = arg;
1130 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1131 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1132
1133 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1134
1135 gsm48_stop_cc_timer(trans);
1136 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1137
1138 /* cause */
1139 if (disc->fields & MNCC_F_CAUSE)
1140 gsm48_encode_cause(msg, 1, &disc->cause);
1141 else
1142 gsm48_encode_cause(msg, 1, &default_cause);
1143
1144 /* facility */
1145 if (disc->fields & MNCC_F_FACILITY)
1146 gsm48_encode_facility(msg, 0, &disc->facility);
1147 /* progress */
1148 if (disc->fields & MNCC_F_PROGRESS)
1149 gsm48_encode_progress(msg, 0, &disc->progress);
1150 /* user-user */
1151 if (disc->fields & MNCC_F_USERUSER)
1152 gsm48_encode_useruser(msg, 0, &disc->useruser);
1153
1154 /* store disconnect cause for T306 expiry */
1155 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1156
1157 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1158
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001159 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001160}
1161
1162static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1163{
1164 struct gsm48_hdr *gh = msgb_l3(msg);
1165 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1166 struct tlv_parsed tp;
1167 struct gsm_mncc rel;
1168 int rc;
1169
1170 gsm48_stop_cc_timer(trans);
1171
1172 memset(&rel, 0, sizeof(struct gsm_mncc));
1173 rel.callref = trans->callref;
1174 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1175 /* cause */
1176 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1177 rel.fields |= MNCC_F_CAUSE;
1178 gsm48_decode_cause(&rel.cause,
1179 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1180 }
1181 /* facility */
1182 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1183 rel.fields |= MNCC_F_FACILITY;
1184 gsm48_decode_facility(&rel.facility,
1185 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1186 }
1187 /* user-user */
1188 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1189 rel.fields |= MNCC_F_USERUSER;
1190 gsm48_decode_useruser(&rel.useruser,
1191 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1192 }
1193 /* ss-version */
1194 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1195 rel.fields |= MNCC_F_SSVERSION;
1196 gsm48_decode_ssversion(&rel.ssversion,
1197 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1198 }
1199
1200 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1201 /* release collision 5.4.5 */
1202 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1203 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001204 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001205 GSM48_PDISC_CC | (trans->transaction_id << 4),
1206 GSM48_MT_CC_RELEASE_COMPL);
1207 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1208 }
1209
1210 new_cc_state(trans, GSM_CSTATE_NULL);
1211
1212 trans->callref = 0;
1213 trans_free(trans);
1214
1215 return rc;
1216}
1217
1218static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1219{
1220 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001221 struct msgb *msg;
1222 struct gsm48_hdr *gh;
1223
1224 if (!trans->msc_a) {
1225 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1226 return -EINVAL;
1227 }
1228
1229 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1230 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001231
1232 gh->msg_type = GSM48_MT_CC_RELEASE;
1233
1234 gsm48_stop_cc_timer(trans);
1235 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1236
1237 /* cause */
1238 if (rel->fields & MNCC_F_CAUSE)
1239 gsm48_encode_cause(msg, 0, &rel->cause);
1240 /* facility */
1241 if (rel->fields & MNCC_F_FACILITY)
1242 gsm48_encode_facility(msg, 0, &rel->facility);
1243 /* user-user */
1244 if (rel->fields & MNCC_F_USERUSER)
1245 gsm48_encode_useruser(msg, 0, &rel->useruser);
1246
1247 trans->cc.T308_second = 0;
1248 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1249
1250 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1251 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1252
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001253 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001254}
1255
1256static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1257{
1258 struct gsm48_hdr *gh = msgb_l3(msg);
1259 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1260 struct tlv_parsed tp;
1261 struct gsm_mncc rel;
1262 int rc = 0;
1263
1264 gsm48_stop_cc_timer(trans);
1265
1266 memset(&rel, 0, sizeof(struct gsm_mncc));
1267 rel.callref = trans->callref;
1268 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1269 /* cause */
1270 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1271 rel.fields |= MNCC_F_CAUSE;
1272 gsm48_decode_cause(&rel.cause,
1273 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1274 }
1275 /* facility */
1276 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1277 rel.fields |= MNCC_F_FACILITY;
1278 gsm48_decode_facility(&rel.facility,
1279 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1280 }
1281 /* user-user */
1282 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1283 rel.fields |= MNCC_F_USERUSER;
1284 gsm48_decode_useruser(&rel.useruser,
1285 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1286 }
1287 /* ss-version */
1288 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1289 rel.fields |= MNCC_F_SSVERSION;
1290 gsm48_decode_ssversion(&rel.ssversion,
1291 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1292 }
1293
1294 if (trans->callref) {
1295 switch (trans->cc.state) {
1296 case GSM_CSTATE_CALL_PRESENT:
1297 rc = mncc_recvmsg(trans->net, trans,
1298 MNCC_REJ_IND, &rel);
1299 break;
1300 case GSM_CSTATE_RELEASE_REQ:
1301 rc = mncc_recvmsg(trans->net, trans,
1302 MNCC_REL_CNF, &rel);
1303 break;
1304 default:
1305 rc = mncc_recvmsg(trans->net, trans,
1306 MNCC_REL_IND, &rel);
1307 }
1308 }
1309
1310 trans->callref = 0;
1311 trans_free(trans);
1312
1313 return rc;
1314}
1315
1316static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1317{
1318 struct gsm_mncc *rel = arg;
1319 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1320 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1321 int ret;
1322
1323 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1324
1325 trans->callref = 0;
1326
1327 gsm48_stop_cc_timer(trans);
1328
1329 /* cause */
1330 if (rel->fields & MNCC_F_CAUSE)
1331 gsm48_encode_cause(msg, 0, &rel->cause);
1332 /* facility */
1333 if (rel->fields & MNCC_F_FACILITY)
1334 gsm48_encode_facility(msg, 0, &rel->facility);
1335 /* user-user */
1336 if (rel->fields & MNCC_F_USERUSER)
1337 gsm48_encode_useruser(msg, 0, &rel->useruser);
1338
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001339 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001340
1341 trans_free(trans);
1342
1343 return ret;
1344}
1345
1346static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1347{
1348 struct gsm48_hdr *gh = msgb_l3(msg);
1349 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1350 struct tlv_parsed tp;
1351 struct gsm_mncc fac;
1352
1353 memset(&fac, 0, sizeof(struct gsm_mncc));
1354 fac.callref = trans->callref;
1355 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1356 /* facility */
1357 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1358 fac.fields |= MNCC_F_FACILITY;
1359 gsm48_decode_facility(&fac.facility,
1360 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1361 }
1362 /* ss-version */
1363 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1364 fac.fields |= MNCC_F_SSVERSION;
1365 gsm48_decode_ssversion(&fac.ssversion,
1366 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1367 }
1368
1369 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1370}
1371
1372static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1373{
1374 struct gsm_mncc *fac = arg;
1375 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1376 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1377
1378 gh->msg_type = GSM48_MT_CC_FACILITY;
1379
1380 /* facility */
1381 gsm48_encode_facility(msg, 1, &fac->facility);
1382
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001383 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001384}
1385
1386static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1387{
1388 struct gsm_mncc hold;
1389
1390 memset(&hold, 0, sizeof(struct gsm_mncc));
1391 hold.callref = trans->callref;
1392 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1393}
1394
1395static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1396{
1397 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1398 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1399
1400 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1401
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001402 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001403}
1404
1405static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1406{
1407 struct gsm_mncc *hold_rej = arg;
1408 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1409 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1410
1411 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1412
1413 /* cause */
1414 if (hold_rej->fields & MNCC_F_CAUSE)
1415 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1416 else
1417 gsm48_encode_cause(msg, 1, &default_cause);
1418
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001419 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001420}
1421
1422static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1423{
1424 struct gsm_mncc retrieve;
1425
1426 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1427 retrieve.callref = trans->callref;
1428 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1429 &retrieve);
1430}
1431
1432static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1433{
1434 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1435 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1436
1437 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1438
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001439 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001440}
1441
1442static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1443{
1444 struct gsm_mncc *retrieve_rej = arg;
1445 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1446 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1447
1448 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1449
1450 /* cause */
1451 if (retrieve_rej->fields & MNCC_F_CAUSE)
1452 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1453 else
1454 gsm48_encode_cause(msg, 1, &default_cause);
1455
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001456 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001457}
1458
1459static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1460{
1461 struct gsm48_hdr *gh = msgb_l3(msg);
1462 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1463 struct tlv_parsed tp;
1464 struct gsm_mncc dtmf;
1465
1466 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1467 dtmf.callref = trans->callref;
1468 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1469 /* keypad facility */
1470 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1471 dtmf.fields |= MNCC_F_KEYPAD;
1472 gsm48_decode_keypad(&dtmf.keypad,
1473 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1474 }
1475
1476 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1477}
1478
1479static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1480{
1481 struct gsm_mncc *dtmf = arg;
1482 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1483 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1484
1485 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1486
1487 /* keypad */
1488 if (dtmf->fields & MNCC_F_KEYPAD)
1489 gsm48_encode_keypad(msg, dtmf->keypad);
1490
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001491 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001492}
1493
1494static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1495{
1496 struct gsm_mncc *dtmf = arg;
1497 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1498 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1499
1500 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1501
1502 /* cause */
1503 if (dtmf->fields & MNCC_F_CAUSE)
1504 gsm48_encode_cause(msg, 1, &dtmf->cause);
1505 else
1506 gsm48_encode_cause(msg, 1, &default_cause);
1507
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001508 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001509}
1510
1511static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1512{
1513 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1514 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1515
1516 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1517
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001518 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001519}
1520
1521static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1522{
1523 struct gsm_mncc dtmf;
1524
1525 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1526 dtmf.callref = trans->callref;
1527
1528 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1529}
1530
1531static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1532{
1533 struct gsm48_hdr *gh = msgb_l3(msg);
1534 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1535 struct tlv_parsed tp;
1536 struct gsm_mncc modify;
1537
1538 memset(&modify, 0, sizeof(struct gsm_mncc));
1539 modify.callref = trans->callref;
1540 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1541 /* bearer capability */
1542 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1543 modify.fields |= MNCC_F_BEARER_CAP;
1544 gsm48_decode_bearer_cap(&modify.bearer_cap,
1545 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1546
1547 /* Create a copy of the bearer capability
1548 * in the transaction struct, so we can use
1549 * this information later */
1550 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1551 sizeof(trans->bearer_cap));
1552 }
1553
1554 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1555
1556 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1557}
1558
1559static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1560{
1561 struct gsm_mncc *modify = arg;
1562 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1563 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1564
1565 gh->msg_type = GSM48_MT_CC_MODIFY;
1566
1567 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1568
1569 /* bearer capability */
1570 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1571 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1572
1573 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1574
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001575 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001576}
1577
1578static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1579{
1580 struct gsm48_hdr *gh = msgb_l3(msg);
1581 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1582 struct tlv_parsed tp;
1583 struct gsm_mncc modify;
1584
1585 gsm48_stop_cc_timer(trans);
1586
1587 memset(&modify, 0, sizeof(struct gsm_mncc));
1588 modify.callref = trans->callref;
1589 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1590 /* bearer capability */
1591 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1592 modify.fields |= MNCC_F_BEARER_CAP;
1593 gsm48_decode_bearer_cap(&modify.bearer_cap,
1594 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1595
1596 /* Create a copy of the bearer capability
1597 * in the transaction struct, so we can use
1598 * this information later */
1599 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1600 sizeof(trans->bearer_cap));
1601 }
1602
1603 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1604
1605 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1606}
1607
1608static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1609{
1610 struct gsm_mncc *modify = arg;
1611 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1612 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1613
1614 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1615
1616 /* bearer capability */
1617 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1618 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1619
1620 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1621
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001622 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001623}
1624
1625static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1626{
1627 struct gsm48_hdr *gh = msgb_l3(msg);
1628 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1629 struct tlv_parsed tp;
1630 struct gsm_mncc modify;
1631
1632 gsm48_stop_cc_timer(trans);
1633
1634 memset(&modify, 0, sizeof(struct gsm_mncc));
1635 modify.callref = trans->callref;
1636 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1637 /* bearer capability */
1638 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1639 modify.fields |= GSM48_IE_BEARER_CAP;
1640 gsm48_decode_bearer_cap(&modify.bearer_cap,
1641 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1642
1643 /* Create a copy of the bearer capability
1644 * in the transaction struct, so we can use
1645 * this information later */
1646 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1647 sizeof(trans->bearer_cap));
1648 }
1649 /* cause */
1650 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1651 modify.fields |= MNCC_F_CAUSE;
1652 gsm48_decode_cause(&modify.cause,
1653 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1654 }
1655
1656 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1657
1658 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1659}
1660
1661static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1662{
1663 struct gsm_mncc *modify = arg;
1664 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1665 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1666
1667 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1668
1669 /* bearer capability */
1670 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1671 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1672 /* cause */
1673 gsm48_encode_cause(msg, 1, &modify->cause);
1674
1675 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1676
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001677 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001678}
1679
1680static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1681{
1682 struct gsm_mncc *notify = arg;
1683 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1684 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1685
1686 gh->msg_type = GSM48_MT_CC_NOTIFY;
1687
1688 /* notify */
1689 gsm48_encode_notify(msg, notify->notify);
1690
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001691 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001692}
1693
1694static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1695{
1696 struct gsm48_hdr *gh = msgb_l3(msg);
1697 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1698// struct tlv_parsed tp;
1699 struct gsm_mncc notify;
1700
1701 memset(&notify, 0, sizeof(struct gsm_mncc));
1702 notify.callref = trans->callref;
1703// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1704 if (payload_len >= 1)
1705 gsm48_decode_notify(&notify.notify, gh->data);
1706
1707 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1708}
1709
1710static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1711{
1712 struct gsm_mncc *user = arg;
1713 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1714 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1715
1716 gh->msg_type = GSM48_MT_CC_USER_INFO;
1717
1718 /* user-user */
1719 if (user->fields & MNCC_F_USERUSER)
1720 gsm48_encode_useruser(msg, 1, &user->useruser);
1721 /* more data */
1722 if (user->more)
1723 gsm48_encode_more(msg);
1724
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001725 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001726}
1727
1728static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1729{
1730 struct gsm48_hdr *gh = msgb_l3(msg);
1731 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1732 struct tlv_parsed tp;
1733 struct gsm_mncc user;
1734
1735 memset(&user, 0, sizeof(struct gsm_mncc));
1736 user.callref = trans->callref;
1737 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1738 /* user-user */
1739 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1740 user.fields |= MNCC_F_USERUSER;
1741 gsm48_decode_useruser(&user.useruser,
1742 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1743 }
1744 /* more data */
1745 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1746 user.more = 1;
1747
1748 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1749}
1750
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001751static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1752 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1753 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001754{
1755 uint8_t data[sizeof(struct gsm_mncc)];
1756 struct gsm_mncc_rtp *rtp;
1757
1758 memset(&data, 0, sizeof(data));
1759 rtp = (struct gsm_mncc_rtp *) &data[0];
1760
1761 rtp->callref = callref;
1762 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001763 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001764 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1765 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001766 }
Harald Welte27989d42018-06-21 20:39:20 +02001767 rtp->payload_type = payload_type;
1768 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001769 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001770}
1771
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001772static 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 +02001773{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001774 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001775}
1776
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001777static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001778{
1779 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001780
1781 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001782 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001783 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001784 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001785 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001786 return -EIO;
1787 }
1788 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001789 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001790 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001791 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001792 return 0;
1793 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001794 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001795
Harald Welte27989d42018-06-21 20:39:20 +02001796 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001797 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001798}
1799
1800/* Trigger TCH_RTP_CREATE acknowledgement */
1801int gsm48_tch_rtp_create(struct gsm_trans *trans)
1802{
1803 /* This function is called as soon as the port, on which the
1804 * mgcp-gw expects the incoming RTP stream from the remote
1805 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001806 struct msc_a *msc_a = trans->msc_a;
1807 struct gsm_network *net = msc_a_net(msc_a);
1808 struct call_leg *cl = msc_a->cc.call_leg;
1809 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001810 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1811 uint32_t payload_type;
1812 int payload_msg_type;
1813 const struct mgcp_conn_peer *mgcp_info;
Neels Hofmeyra001a702022-10-31 17:57:30 +01001814 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001815
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001816 if (!rtp_cn) {
1817 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1818 return -EINVAL;
1819 }
1820
1821 if (!rtp_cn->codec_known) {
1822 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1823 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1824 return -EINVAL;
1825 }
1826
1827 /* Codec */
Neels Hofmeyra001a702022-10-31 17:57:30 +01001828 m = codec_mapping_by_mgcp_codec(rtp_cn->codec);
1829 if (!m) {
1830 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1831 "Cannot RTP CREATE to MNCC, cannot resolve codec '%s'\n",
1832 osmo_mgcpc_codec_name(rtp_cn->codec));
1833 return -EINVAL;
1834 }
1835 payload_msg_type = m->mncc_payload_msg_type;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001836
1837 /* Payload Type number */
1838 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001839 if (mgcp_info && mgcp_info->ptmap_len)
1840 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1841 else
1842 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001843
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001844 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1845 if (!rtp_cn_local) {
1846 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1847 return -EINVAL;
1848 }
1849
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001850 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 +02001851}
1852
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001853static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001854{
1855 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001856 struct call_leg *cl;
1857 struct rtp_stream *rtps;
1858 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001859
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001860 /* FIXME: in *rtp we should get the codec information of the remote
1861 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1862 * meaningful value based on this information but unfortunately we
1863 * can't do that yet because the mncc API can not signal dynamic
1864 * payload types yet. This must be fixed first. Also there may be
1865 * additional members necessary in trans->conn->rtp because we
1866 * somehow need to deal with dynamic payload types that do not
1867 * comply to 3gpp's assumptions of payload type numbers on the A
1868 * interface. See also related tickets: OS#3399 and OS1683 */
1869
Harald Welte27989d42018-06-21 20:39:20 +02001870 /* Find callref */
1871 trans = trans_find_by_callref(net, rtp->callref);
1872 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001873 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001874 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001875 return -EIO;
1876 }
1877 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001878 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001879 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001880 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001881 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001882 }
1883
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001884 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001885
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001886 cl = trans->msc_a->cc.call_leg;
1887 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1888
1889 if (!rtps) {
1890 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1891 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1892 return -EINVAL;
1893 }
1894
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001895 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1896 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1897 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1898 return -EINVAL;
1899 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001900 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1901 rtp_stream_commit(rtps);
1902 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001903}
1904
1905static struct downstate {
1906 uint32_t states;
1907 int type;
1908 int (*rout) (struct gsm_trans *trans, void *arg);
1909} downstatelist[] = {
1910 /* mobile originating call establishment */
1911 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1912 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1913 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1914 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1915 {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 */
1916 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1917 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1918 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1919 /* mobile terminating call establishment */
1920 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1921 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1922 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1923 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1924 /* signalling during call */
1925 {SBIT(GSM_CSTATE_ACTIVE),
1926 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1927 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1928 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1929 {ALL_STATES,
1930 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1931 {ALL_STATES,
1932 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1933 {ALL_STATES,
1934 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1935 {SBIT(GSM_CSTATE_ACTIVE),
1936 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1937 {SBIT(GSM_CSTATE_ACTIVE),
1938 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1939 {SBIT(GSM_CSTATE_ACTIVE),
1940 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1941 {SBIT(GSM_CSTATE_ACTIVE),
1942 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1943 {SBIT(GSM_CSTATE_ACTIVE),
1944 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1945 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1946 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1947 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1948 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1949 {SBIT(GSM_CSTATE_ACTIVE),
1950 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1951 /* clearing */
1952 {SBIT(GSM_CSTATE_INITIATED),
1953 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1954 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1955 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1956 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1957 MNCC_REL_REQ, gsm48_cc_tx_release},
1958};
1959
1960#define DOWNSLLEN \
1961 (sizeof(downstatelist) / sizeof(struct downstate))
1962
1963
Philipp Maiercd64af72019-08-01 09:46:40 +02001964static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001965{
1966 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001967 struct msc_a *msc_a = NULL;
1968 struct gsm_trans *trans = NULL;
1969 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001970
Harald Welte27989d42018-06-21 20:39:20 +02001971 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001972 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001973 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001974 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001975 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001976 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001977 return rc;
1978 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001979 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001980 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001981 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001982 case MNCC_RTP_FREE:
1983 /* unused right now */
1984 return -EIO;
1985
1986 case MNCC_FRAME_DROP:
1987 case MNCC_FRAME_RECV:
1988 case GSM_TCHF_FRAME:
1989 case GSM_TCHF_FRAME_EFR:
1990 case GSM_TCHH_FRAME:
1991 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001992 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001993 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001994 return -ENOTSUP;
1995 }
1996
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001998
1999 /* Find callref */
2000 trans = trans_find_by_callref(net, data->callref);
2001
2002 /* Callref unknown */
2003 if (!trans) {
2004 struct vlr_subscr *vsub;
2005
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002006 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002007 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002008 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002009 /* Invalid call reference */
2010 return mncc_release_ind(net, NULL, data->callref,
2011 GSM48_CAUSE_LOC_PRN_S_LU,
2012 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2013 }
2014 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002015 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002017 /* Invalid number */
2018 return mncc_release_ind(net, NULL, data->callref,
2019 GSM48_CAUSE_LOC_PRN_S_LU,
2020 GSM48_CC_CAUSE_INV_NR_FORMAT);
2021 }
2022 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002023 if (data->called.number[0]) {
2024 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2025 if (!vsub)
2026 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002027 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002028 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002029 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002030 if (!vsub)
2031 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002032 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002033 }
2034 if (!vsub)
2035 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002036 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002037 /* update the subscriber we deal with */
2038 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2039
Harald Welte27989d42018-06-21 20:39:20 +02002040 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002041 if (!vsub->lu_complete) {
2042 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002043 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002044 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002045 /* Temporarily out of order */
2046 return mncc_release_ind(net, NULL, data->callref,
2047 GSM48_CAUSE_LOC_PRN_S_LU,
2048 GSM48_CC_CAUSE_DEST_OOO);
2049 }
Keith Whyte991bb422019-08-08 15:43:40 +02002050
2051 /* Find valid conn */
2052 msc_a = msc_a_for_vsub(vsub, true);
2053
2054 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2055 if (!net->call_waiting && msc_a) {
2056 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2057 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2058 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2059 "rx '%s' for subscriber %s with trans state (%s)"
2060 " rejecting with USER_BUSY\n",
2061 get_mncc_name(msg->msg_type), data->called.number,
2062 gsm48_cc_state_name(existing_cc_trans->cc.state));
2063 return mncc_release_ind(net, NULL, data->callref,
2064 GSM48_CAUSE_LOC_PRN_S_LU,
2065 GSM48_CC_CAUSE_USER_BUSY);
2066 }
2067 }
2068
Harald Welte27989d42018-06-21 20:39:20 +02002069 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002070 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002071 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002072 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002073 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002074 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002075 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002076 mncc_release_ind(net, NULL, data->callref,
2077 GSM48_CAUSE_LOC_PRN_S_LU,
2078 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2079 return -ENOMEM;
2080 }
2081
Harald Welte27989d42018-06-21 20:39:20 +02002082 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002083 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002084 /* This condition will return before the common logging of the received MNCC message below, so
2085 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002086 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002087
Harald Welte27989d42018-06-21 20:39:20 +02002088 /* store setup information until paging succeeds */
2089 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2090
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002091 /* Request a channel. If Paging already started, paging_request_start() will append the new
2092 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002093 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2094 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002095 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002096 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002097 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002098 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002099 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002100 return 0;
2101 }
2102
2103 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002104 trans->msc_a = msc_a;
2105 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002106 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002107 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002108 } else {
2109 /* update the subscriber we deal with */
2110 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2111 }
2112
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002113 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002114
Philipp Maier9ca7b312018-10-10 17:00:49 +02002115 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002116 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002117
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002118 if (trans->msc_a)
2119 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002120
2121 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002122 if (!msc_a) {
2123 struct gsm_mncc rel = {
2124 .callref = data->callref,
2125 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002126 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002127 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2128 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002129 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002130 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2131 else
2132 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2133 trans->callref = 0;
2134 trans_free(trans);
2135 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002136 }
2137
2138 /* Find function for current state and message */
2139 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002140 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002141 && ((1 << trans->cc.state) & downstatelist[i].states))
2142 break;
2143 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002144 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002145 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002146 return 0;
2147 }
2148
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002149 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002150
2151 return rc;
2152}
2153
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002154struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2155{
2156 uint32_t callref;
2157
2158 switch (msg->msg_type) {
2159 case MNCC_BRIDGE:
2160 callref = msg->bridge.callref[0];
2161 break;
2162 case MNCC_RTP_CREATE:
2163 case MNCC_RTP_CONNECT:
2164 callref = msg->rtp.callref;
2165 break;
2166
2167 case MNCC_RTP_FREE:
2168 case MNCC_FRAME_DROP:
2169 case MNCC_FRAME_RECV:
2170 case GSM_TCHF_FRAME:
2171 case GSM_TCHF_FRAME_EFR:
2172 case GSM_TCHH_FRAME:
2173 case GSM_TCH_FRAME_AMR:
2174 return NULL;
2175
2176 default:
2177 callref = msg->signal.callref;
2178 break;
2179 }
2180
2181 return mncc_call_find_by_callref(callref);
2182}
2183
2184/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002185int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002186{
2187 const union mncc_msg *msg = arg;
2188 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002189
2190 if (msg->msg_type == MNCC_SETUP_REQ) {
2191 /* Incoming call to forward for inter-MSC Handover? */
2192 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2193 if (mncc_call)
2194 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2195 "Incoming call matches pending inter-MSC Handover Number\n");
2196 }
2197 if (!mncc_call) {
2198 /* Find already active MNCC FSM for this callref.
2199 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2200 * MNCC<->GSM-CC call handling. */
2201 mncc_call = mncc_find_by_callref_from_msg(msg);
2202 }
2203 if (mncc_call) {
2204 mncc_call_rx(mncc_call, msg);
2205 return 0;
2206 }
2207
2208 /* None of the above? Then it must be a normal GSM CC call related message. */
2209 return mncc_tx_to_gsm_cc(net, msg);
2210}
Harald Welte27989d42018-06-21 20:39:20 +02002211
2212static struct datastate {
2213 uint32_t states;
2214 int type;
2215 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2216} datastatelist[] = {
2217 /* mobile originating call establishment */
2218 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2219 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2220 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2221 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2222 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2223 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2224 /* mobile terminating call establishment */
2225 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2226 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2227 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2228 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2229 {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 */
2230 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2231 /* signalling during call */
2232 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2233 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2234 {SBIT(GSM_CSTATE_ACTIVE),
2235 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2236 {ALL_STATES,
2237 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2238 {ALL_STATES,
2239 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2240 {ALL_STATES,
2241 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2242 {SBIT(GSM_CSTATE_ACTIVE),
2243 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2244 {SBIT(GSM_CSTATE_ACTIVE),
2245 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2246 {SBIT(GSM_CSTATE_ACTIVE),
2247 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2248 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2249 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2250 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2251 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2252 {SBIT(GSM_CSTATE_ACTIVE),
2253 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2254 /* clearing */
2255 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2256 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2257 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2258 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2259 {ALL_STATES, /* 5.4.3.4 */
2260 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2261};
2262
2263#define DATASLLEN \
2264 (sizeof(datastatelist) / sizeof(struct datastate))
2265
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002266int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002267{
2268 struct gsm48_hdr *gh = msgb_l3(msg);
2269 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2270 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2271 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002272 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2273 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002274 int i, rc = 0;
2275
2276 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002277 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002278 return -EINVAL;
2279 }
2280
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002281 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002282 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002283 return -EINVAL;
2284 }
2285
2286 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002287 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002288
Harald Welte27989d42018-06-21 20:39:20 +02002289 /* Create transaction */
2290 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002291 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002292 trans = trans_alloc(net, vsub,
2293 TRANS_CC,
2294 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002295 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002296 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002297 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002298 GSM48_PDISC_CC | (transaction_id << 4),
2299 GSM48_MT_CC_RELEASE_COMPL);
2300 return -ENOMEM;
2301 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002302 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2303 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2304 trans_free(trans);
2305 return -EINVAL;
2306 }
2307
Harald Welte27989d42018-06-21 20:39:20 +02002308 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002309 msc_a_get(msc_a, MSC_A_USE_CC);
2310 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002311 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002312
2313 /* An earlier CM Service Request for this CC message now has concluded */
2314 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2315 LOG_MSC_A(msc_a, LOGL_ERROR,
2316 "Creating new CC transaction without prior CM Service Request\n");
2317 else
2318 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002319 }
2320
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002321 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2322 gsm48_cc_state_name(trans->cc.state));
2323
Harald Welte27989d42018-06-21 20:39:20 +02002324 /* find function for current state and message */
2325 for (i = 0; i < DATASLLEN; i++)
2326 if ((msg_type == datastatelist[i].type)
2327 && ((1 << trans->cc.state) & datastatelist[i].states))
2328 break;
2329 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002330 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002331
2332 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2333 * transaction right away. */
2334 if (trans->cc.state == GSM_CSTATE_NULL) {
2335 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2336 " -- disarding new CC transaction right away\n");
2337 trans_free(trans);
2338 }
Harald Welte27989d42018-06-21 20:39:20 +02002339 return 0;
2340 }
2341
2342 assert(trans->vsub);
2343
2344 rc = datastatelist[i].rout(trans, msg);
2345
Harald Welte27989d42018-06-21 20:39:20 +02002346 return rc;
2347}