blob: b234fd5644a2b44673f8c5e9cf09244f6ca2a695 [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
733 /* bearer capability */
734 if (setup->fields & MNCC_F_BEARER_CAP) {
735 /* Create a copy of the bearer capability in the transaction struct, so we
736 * can use this information later */
737 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
738 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
739 }
740 /* facility */
741 if (setup->fields & MNCC_F_FACILITY)
742 gsm48_encode_facility(msg, 0, &setup->facility);
743 /* progress */
744 if (setup->fields & MNCC_F_PROGRESS)
745 gsm48_encode_progress(msg, 0, &setup->progress);
746 /* calling party BCD number */
747 if (setup->fields & MNCC_F_CALLING)
748 gsm48_encode_calling(msg, &setup->calling);
749 /* called party BCD number */
750 if (setup->fields & MNCC_F_CALLED)
751 gsm48_encode_called(msg, &setup->called);
752 /* user-user */
753 if (setup->fields & MNCC_F_USERUSER)
754 gsm48_encode_useruser(msg, 0, &setup->useruser);
755 /* redirecting party BCD number */
756 if (setup->fields & MNCC_F_REDIRECTING)
757 gsm48_encode_redirecting(msg, &setup->redirecting);
758 /* signal */
759 if (setup->fields & MNCC_F_SIGNAL)
760 gsm48_encode_signal(msg, setup->signal);
761
762 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
763
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200764 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200765
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100766 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200767}
768
769static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
770{
771 struct gsm48_hdr *gh = msgb_l3(msg);
772 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
773 struct tlv_parsed tp;
774 struct gsm_mncc call_conf;
775 int rc;
776
777 gsm48_stop_cc_timer(trans);
778 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
779
780 memset(&call_conf, 0, sizeof(struct gsm_mncc));
781 call_conf.callref = trans->callref;
782
783 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
784#if 0
785 /* repeat */
786 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
787 call_conf.repeat = 1;
788 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
789 call_conf.repeat = 2;
790#endif
791 /* bearer capability */
792 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
793 call_conf.fields |= MNCC_F_BEARER_CAP;
794 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
795 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
796
797 /* Create a copy of the bearer capability
798 * in the transaction struct, so we can use
799 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100800 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200801 sizeof(trans->bearer_cap));
802 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100803
Harald Welte27989d42018-06-21 20:39:20 +0200804 /* cause */
805 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
806 call_conf.fields |= MNCC_F_CAUSE;
807 gsm48_decode_cause(&call_conf.cause,
808 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
809 }
810 /* cc cap */
811 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
812 call_conf.fields |= MNCC_F_CCCAP;
813 gsm48_decode_cccap(&call_conf.cccap,
814 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
815 }
816
817 /* IMSI of called subscriber */
818 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
819
820 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
821
822 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100823 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200824
825 /* don't continue, if there were problems with
826 * the call assignment. */
827 if (rc)
828 return rc;
829
830 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
831 &call_conf);
832}
833
834static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
835{
836 struct gsm_mncc *proceeding = arg;
837 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
838 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
839 int rc;
840
841 gh->msg_type = GSM48_MT_CC_CALL_PROC;
842
843 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
844
845 /* bearer capability */
846 if (proceeding->fields & MNCC_F_BEARER_CAP) {
847 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
848 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
849 }
850 /* facility */
851 if (proceeding->fields & MNCC_F_FACILITY)
852 gsm48_encode_facility(msg, 0, &proceeding->facility);
853 /* progress */
854 if (proceeding->fields & MNCC_F_PROGRESS)
855 gsm48_encode_progress(msg, 0, &proceeding->progress);
856
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100857 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200858 if (rc)
859 return rc;
860
861 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100862 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200863}
864
865static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
866{
867 struct gsm48_hdr *gh = msgb_l3(msg);
868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
869 struct tlv_parsed tp;
870 struct gsm_mncc alerting;
871
872 gsm48_stop_cc_timer(trans);
873 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
874
875 memset(&alerting, 0, sizeof(struct gsm_mncc));
876 alerting.callref = trans->callref;
877 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
878 /* facility */
879 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
880 alerting.fields |= MNCC_F_FACILITY;
881 gsm48_decode_facility(&alerting.facility,
882 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
883 }
884
885 /* progress */
886 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
887 alerting.fields |= MNCC_F_PROGRESS;
888 gsm48_decode_progress(&alerting.progress,
889 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
890 }
891 /* ss-version */
892 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
893 alerting.fields |= MNCC_F_SSVERSION;
894 gsm48_decode_ssversion(&alerting.ssversion,
895 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
896 }
897
898 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
899
900 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
901 &alerting);
902}
903
904static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
905{
906 struct gsm_mncc *alerting = arg;
907 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
908 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
909
910 gh->msg_type = GSM48_MT_CC_ALERTING;
911
912 /* facility */
913 if (alerting->fields & MNCC_F_FACILITY)
914 gsm48_encode_facility(msg, 0, &alerting->facility);
915 /* progress */
916 if (alerting->fields & MNCC_F_PROGRESS)
917 gsm48_encode_progress(msg, 0, &alerting->progress);
918 /* user-user */
919 if (alerting->fields & MNCC_F_USERUSER)
920 gsm48_encode_useruser(msg, 0, &alerting->useruser);
921
922 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
923
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100924 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200925}
926
927static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
928{
929 struct gsm_mncc *progress = arg;
930 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
931 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
932
933 gh->msg_type = GSM48_MT_CC_PROGRESS;
934
935 /* progress */
936 gsm48_encode_progress(msg, 1, &progress->progress);
937 /* user-user */
938 if (progress->fields & MNCC_F_USERUSER)
939 gsm48_encode_useruser(msg, 0, &progress->useruser);
940
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100941 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200942}
943
944static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
945{
946 struct gsm_mncc *connect = arg;
947 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
948 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
949
950 gh->msg_type = GSM48_MT_CC_CONNECT;
951
952 gsm48_stop_cc_timer(trans);
953 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
954
955 /* facility */
956 if (connect->fields & MNCC_F_FACILITY)
957 gsm48_encode_facility(msg, 0, &connect->facility);
958 /* progress */
959 if (connect->fields & MNCC_F_PROGRESS)
960 gsm48_encode_progress(msg, 0, &connect->progress);
961 /* connected number */
962 if (connect->fields & MNCC_F_CONNECTED)
963 gsm48_encode_connected(msg, &connect->connected);
964 /* user-user */
965 if (connect->fields & MNCC_F_USERUSER)
966 gsm48_encode_useruser(msg, 0, &connect->useruser);
967
968 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
969
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100970 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200971}
972
973static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
974{
975 struct gsm48_hdr *gh = msgb_l3(msg);
976 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
977 struct tlv_parsed tp;
978 struct gsm_mncc connect;
979
980 gsm48_stop_cc_timer(trans);
981
982 memset(&connect, 0, sizeof(struct gsm_mncc));
983 connect.callref = trans->callref;
984 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
985 /* use subscriber as connected party number */
986 connect.fields |= MNCC_F_CONNECTED;
987 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
988 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
989
990 /* facility */
991 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
992 connect.fields |= MNCC_F_FACILITY;
993 gsm48_decode_facility(&connect.facility,
994 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
995 }
996 /* user-user */
997 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
998 connect.fields |= MNCC_F_USERUSER;
999 gsm48_decode_useruser(&connect.useruser,
1000 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1001 }
1002 /* ss-version */
1003 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1004 connect.fields |= MNCC_F_SSVERSION;
1005 gsm48_decode_ssversion(&connect.ssversion,
1006 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1007 }
1008
1009 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001010 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001011
1012 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1013}
1014
1015
1016static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1017{
1018 struct gsm_mncc connect_ack;
1019
1020 gsm48_stop_cc_timer(trans);
1021
1022 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001023 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 +02001024
1025 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1026 connect_ack.callref = trans->callref;
1027
1028 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1029 &connect_ack);
1030}
1031
1032static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1033{
1034 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1035 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1036
1037 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1038
1039 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1040
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001041 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001042}
1043
1044static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1045{
1046 struct gsm48_hdr *gh = msgb_l3(msg);
1047 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1048 struct tlv_parsed tp;
1049 struct gsm_mncc disc;
1050
1051 gsm48_stop_cc_timer(trans);
1052
1053 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1054
1055 memset(&disc, 0, sizeof(struct gsm_mncc));
1056 disc.callref = trans->callref;
1057 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1058 /* cause */
1059 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1060 disc.fields |= MNCC_F_CAUSE;
1061 gsm48_decode_cause(&disc.cause,
1062 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1063 }
1064 /* facility */
1065 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1066 disc.fields |= MNCC_F_FACILITY;
1067 gsm48_decode_facility(&disc.facility,
1068 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1069 }
1070 /* user-user */
1071 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1072 disc.fields |= MNCC_F_USERUSER;
1073 gsm48_decode_useruser(&disc.useruser,
1074 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1075 }
1076 /* ss-version */
1077 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1078 disc.fields |= MNCC_F_SSVERSION;
1079 gsm48_decode_ssversion(&disc.ssversion,
1080 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1081 }
1082
1083 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001084}
1085
1086static struct gsm_mncc_cause default_cause = {
1087 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1088 .coding = 0,
1089 .rec = 0,
1090 .rec_val = 0,
1091 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1092 .diag_len = 0,
1093 .diag = { 0 },
1094};
1095
1096static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1097{
1098 struct gsm_mncc *disc = arg;
1099 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1100 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1101
1102 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1103
1104 gsm48_stop_cc_timer(trans);
1105 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1106
1107 /* cause */
1108 if (disc->fields & MNCC_F_CAUSE)
1109 gsm48_encode_cause(msg, 1, &disc->cause);
1110 else
1111 gsm48_encode_cause(msg, 1, &default_cause);
1112
1113 /* facility */
1114 if (disc->fields & MNCC_F_FACILITY)
1115 gsm48_encode_facility(msg, 0, &disc->facility);
1116 /* progress */
1117 if (disc->fields & MNCC_F_PROGRESS)
1118 gsm48_encode_progress(msg, 0, &disc->progress);
1119 /* user-user */
1120 if (disc->fields & MNCC_F_USERUSER)
1121 gsm48_encode_useruser(msg, 0, &disc->useruser);
1122
1123 /* store disconnect cause for T306 expiry */
1124 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1125
1126 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1127
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001128 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001129}
1130
1131static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1132{
1133 struct gsm48_hdr *gh = msgb_l3(msg);
1134 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1135 struct tlv_parsed tp;
1136 struct gsm_mncc rel;
1137 int rc;
1138
1139 gsm48_stop_cc_timer(trans);
1140
1141 memset(&rel, 0, sizeof(struct gsm_mncc));
1142 rel.callref = trans->callref;
1143 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1144 /* cause */
1145 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1146 rel.fields |= MNCC_F_CAUSE;
1147 gsm48_decode_cause(&rel.cause,
1148 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1149 }
1150 /* facility */
1151 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1152 rel.fields |= MNCC_F_FACILITY;
1153 gsm48_decode_facility(&rel.facility,
1154 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1155 }
1156 /* user-user */
1157 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1158 rel.fields |= MNCC_F_USERUSER;
1159 gsm48_decode_useruser(&rel.useruser,
1160 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1161 }
1162 /* ss-version */
1163 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1164 rel.fields |= MNCC_F_SSVERSION;
1165 gsm48_decode_ssversion(&rel.ssversion,
1166 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1167 }
1168
1169 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1170 /* release collision 5.4.5 */
1171 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1172 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001173 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001174 GSM48_PDISC_CC | (trans->transaction_id << 4),
1175 GSM48_MT_CC_RELEASE_COMPL);
1176 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1177 }
1178
1179 new_cc_state(trans, GSM_CSTATE_NULL);
1180
1181 trans->callref = 0;
1182 trans_free(trans);
1183
1184 return rc;
1185}
1186
1187static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1188{
1189 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001190 struct msgb *msg;
1191 struct gsm48_hdr *gh;
1192
1193 if (!trans->msc_a) {
1194 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1195 return -EINVAL;
1196 }
1197
1198 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1199 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001200
1201 gh->msg_type = GSM48_MT_CC_RELEASE;
1202
1203 gsm48_stop_cc_timer(trans);
1204 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1205
1206 /* cause */
1207 if (rel->fields & MNCC_F_CAUSE)
1208 gsm48_encode_cause(msg, 0, &rel->cause);
1209 /* facility */
1210 if (rel->fields & MNCC_F_FACILITY)
1211 gsm48_encode_facility(msg, 0, &rel->facility);
1212 /* user-user */
1213 if (rel->fields & MNCC_F_USERUSER)
1214 gsm48_encode_useruser(msg, 0, &rel->useruser);
1215
1216 trans->cc.T308_second = 0;
1217 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1218
1219 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1220 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1221
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001222 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001223}
1224
1225static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1226{
1227 struct gsm48_hdr *gh = msgb_l3(msg);
1228 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1229 struct tlv_parsed tp;
1230 struct gsm_mncc rel;
1231 int rc = 0;
1232
1233 gsm48_stop_cc_timer(trans);
1234
1235 memset(&rel, 0, sizeof(struct gsm_mncc));
1236 rel.callref = trans->callref;
1237 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1238 /* cause */
1239 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1240 rel.fields |= MNCC_F_CAUSE;
1241 gsm48_decode_cause(&rel.cause,
1242 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1243 }
1244 /* facility */
1245 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1246 rel.fields |= MNCC_F_FACILITY;
1247 gsm48_decode_facility(&rel.facility,
1248 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1249 }
1250 /* user-user */
1251 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1252 rel.fields |= MNCC_F_USERUSER;
1253 gsm48_decode_useruser(&rel.useruser,
1254 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1255 }
1256 /* ss-version */
1257 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1258 rel.fields |= MNCC_F_SSVERSION;
1259 gsm48_decode_ssversion(&rel.ssversion,
1260 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1261 }
1262
1263 if (trans->callref) {
1264 switch (trans->cc.state) {
1265 case GSM_CSTATE_CALL_PRESENT:
1266 rc = mncc_recvmsg(trans->net, trans,
1267 MNCC_REJ_IND, &rel);
1268 break;
1269 case GSM_CSTATE_RELEASE_REQ:
1270 rc = mncc_recvmsg(trans->net, trans,
1271 MNCC_REL_CNF, &rel);
1272 break;
1273 default:
1274 rc = mncc_recvmsg(trans->net, trans,
1275 MNCC_REL_IND, &rel);
1276 }
1277 }
1278
1279 trans->callref = 0;
1280 trans_free(trans);
1281
1282 return rc;
1283}
1284
1285static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1286{
1287 struct gsm_mncc *rel = arg;
1288 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1289 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1290 int ret;
1291
1292 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1293
1294 trans->callref = 0;
1295
1296 gsm48_stop_cc_timer(trans);
1297
1298 /* cause */
1299 if (rel->fields & MNCC_F_CAUSE)
1300 gsm48_encode_cause(msg, 0, &rel->cause);
1301 /* facility */
1302 if (rel->fields & MNCC_F_FACILITY)
1303 gsm48_encode_facility(msg, 0, &rel->facility);
1304 /* user-user */
1305 if (rel->fields & MNCC_F_USERUSER)
1306 gsm48_encode_useruser(msg, 0, &rel->useruser);
1307
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001308 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001309
1310 trans_free(trans);
1311
1312 return ret;
1313}
1314
1315static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1316{
1317 struct gsm48_hdr *gh = msgb_l3(msg);
1318 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1319 struct tlv_parsed tp;
1320 struct gsm_mncc fac;
1321
1322 memset(&fac, 0, sizeof(struct gsm_mncc));
1323 fac.callref = trans->callref;
1324 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1325 /* facility */
1326 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1327 fac.fields |= MNCC_F_FACILITY;
1328 gsm48_decode_facility(&fac.facility,
1329 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1330 }
1331 /* ss-version */
1332 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1333 fac.fields |= MNCC_F_SSVERSION;
1334 gsm48_decode_ssversion(&fac.ssversion,
1335 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1336 }
1337
1338 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1339}
1340
1341static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1342{
1343 struct gsm_mncc *fac = arg;
1344 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1345 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1346
1347 gh->msg_type = GSM48_MT_CC_FACILITY;
1348
1349 /* facility */
1350 gsm48_encode_facility(msg, 1, &fac->facility);
1351
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001352 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001353}
1354
1355static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1356{
1357 struct gsm_mncc hold;
1358
1359 memset(&hold, 0, sizeof(struct gsm_mncc));
1360 hold.callref = trans->callref;
1361 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1362}
1363
1364static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1365{
1366 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1367 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1368
1369 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1370
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001371 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001372}
1373
1374static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1375{
1376 struct gsm_mncc *hold_rej = arg;
1377 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1379
1380 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1381
1382 /* cause */
1383 if (hold_rej->fields & MNCC_F_CAUSE)
1384 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1385 else
1386 gsm48_encode_cause(msg, 1, &default_cause);
1387
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001388 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001389}
1390
1391static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1392{
1393 struct gsm_mncc retrieve;
1394
1395 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1396 retrieve.callref = trans->callref;
1397 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1398 &retrieve);
1399}
1400
1401static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1402{
1403 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1404 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1405
1406 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1407
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001408 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001409}
1410
1411static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1412{
1413 struct gsm_mncc *retrieve_rej = arg;
1414 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1416
1417 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1418
1419 /* cause */
1420 if (retrieve_rej->fields & MNCC_F_CAUSE)
1421 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1422 else
1423 gsm48_encode_cause(msg, 1, &default_cause);
1424
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001425 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001426}
1427
1428static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1429{
1430 struct gsm48_hdr *gh = msgb_l3(msg);
1431 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1432 struct tlv_parsed tp;
1433 struct gsm_mncc dtmf;
1434
1435 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1436 dtmf.callref = trans->callref;
1437 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1438 /* keypad facility */
1439 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1440 dtmf.fields |= MNCC_F_KEYPAD;
1441 gsm48_decode_keypad(&dtmf.keypad,
1442 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1443 }
1444
1445 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1446}
1447
1448static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1449{
1450 struct gsm_mncc *dtmf = arg;
1451 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1452 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1453
1454 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1455
1456 /* keypad */
1457 if (dtmf->fields & MNCC_F_KEYPAD)
1458 gsm48_encode_keypad(msg, dtmf->keypad);
1459
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001460 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001461}
1462
1463static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1464{
1465 struct gsm_mncc *dtmf = arg;
1466 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1468
1469 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1470
1471 /* cause */
1472 if (dtmf->fields & MNCC_F_CAUSE)
1473 gsm48_encode_cause(msg, 1, &dtmf->cause);
1474 else
1475 gsm48_encode_cause(msg, 1, &default_cause);
1476
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001477 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001478}
1479
1480static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1481{
1482 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1483 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1484
1485 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1486
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001487 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001488}
1489
1490static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1491{
1492 struct gsm_mncc dtmf;
1493
1494 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1495 dtmf.callref = trans->callref;
1496
1497 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1498}
1499
1500static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1501{
1502 struct gsm48_hdr *gh = msgb_l3(msg);
1503 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1504 struct tlv_parsed tp;
1505 struct gsm_mncc modify;
1506
1507 memset(&modify, 0, sizeof(struct gsm_mncc));
1508 modify.callref = trans->callref;
1509 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1510 /* bearer capability */
1511 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1512 modify.fields |= MNCC_F_BEARER_CAP;
1513 gsm48_decode_bearer_cap(&modify.bearer_cap,
1514 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1515
1516 /* Create a copy of the bearer capability
1517 * in the transaction struct, so we can use
1518 * this information later */
1519 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1520 sizeof(trans->bearer_cap));
1521 }
1522
1523 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1524
1525 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1526}
1527
1528static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1529{
1530 struct gsm_mncc *modify = arg;
1531 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1532 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1533
1534 gh->msg_type = GSM48_MT_CC_MODIFY;
1535
1536 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1537
1538 /* bearer capability */
1539 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1540 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1541
1542 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1543
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001544 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001545}
1546
1547static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1548{
1549 struct gsm48_hdr *gh = msgb_l3(msg);
1550 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1551 struct tlv_parsed tp;
1552 struct gsm_mncc modify;
1553
1554 gsm48_stop_cc_timer(trans);
1555
1556 memset(&modify, 0, sizeof(struct gsm_mncc));
1557 modify.callref = trans->callref;
1558 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1559 /* bearer capability */
1560 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1561 modify.fields |= MNCC_F_BEARER_CAP;
1562 gsm48_decode_bearer_cap(&modify.bearer_cap,
1563 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1564
1565 /* Create a copy of the bearer capability
1566 * in the transaction struct, so we can use
1567 * this information later */
1568 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1569 sizeof(trans->bearer_cap));
1570 }
1571
1572 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1573
1574 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1575}
1576
1577static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1578{
1579 struct gsm_mncc *modify = arg;
1580 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1581 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1582
1583 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1584
1585 /* bearer capability */
1586 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1587 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1588
1589 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1590
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001591 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001592}
1593
1594static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1595{
1596 struct gsm48_hdr *gh = msgb_l3(msg);
1597 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1598 struct tlv_parsed tp;
1599 struct gsm_mncc modify;
1600
1601 gsm48_stop_cc_timer(trans);
1602
1603 memset(&modify, 0, sizeof(struct gsm_mncc));
1604 modify.callref = trans->callref;
1605 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1606 /* bearer capability */
1607 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1608 modify.fields |= GSM48_IE_BEARER_CAP;
1609 gsm48_decode_bearer_cap(&modify.bearer_cap,
1610 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1611
1612 /* Create a copy of the bearer capability
1613 * in the transaction struct, so we can use
1614 * this information later */
1615 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1616 sizeof(trans->bearer_cap));
1617 }
1618 /* cause */
1619 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1620 modify.fields |= MNCC_F_CAUSE;
1621 gsm48_decode_cause(&modify.cause,
1622 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1623 }
1624
1625 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1626
1627 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1628}
1629
1630static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1631{
1632 struct gsm_mncc *modify = arg;
1633 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1634 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1635
1636 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1637
1638 /* bearer capability */
1639 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1640 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1641 /* cause */
1642 gsm48_encode_cause(msg, 1, &modify->cause);
1643
1644 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1645
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001646 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001647}
1648
1649static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1650{
1651 struct gsm_mncc *notify = arg;
1652 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1653 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1654
1655 gh->msg_type = GSM48_MT_CC_NOTIFY;
1656
1657 /* notify */
1658 gsm48_encode_notify(msg, notify->notify);
1659
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001660 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001661}
1662
1663static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1664{
1665 struct gsm48_hdr *gh = msgb_l3(msg);
1666 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1667// struct tlv_parsed tp;
1668 struct gsm_mncc notify;
1669
1670 memset(&notify, 0, sizeof(struct gsm_mncc));
1671 notify.callref = trans->callref;
1672// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1673 if (payload_len >= 1)
1674 gsm48_decode_notify(&notify.notify, gh->data);
1675
1676 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1677}
1678
1679static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1680{
1681 struct gsm_mncc *user = arg;
1682 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1684
1685 gh->msg_type = GSM48_MT_CC_USER_INFO;
1686
1687 /* user-user */
1688 if (user->fields & MNCC_F_USERUSER)
1689 gsm48_encode_useruser(msg, 1, &user->useruser);
1690 /* more data */
1691 if (user->more)
1692 gsm48_encode_more(msg);
1693
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001694 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001695}
1696
1697static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1698{
1699 struct gsm48_hdr *gh = msgb_l3(msg);
1700 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1701 struct tlv_parsed tp;
1702 struct gsm_mncc user;
1703
1704 memset(&user, 0, sizeof(struct gsm_mncc));
1705 user.callref = trans->callref;
1706 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1707 /* user-user */
1708 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1709 user.fields |= MNCC_F_USERUSER;
1710 gsm48_decode_useruser(&user.useruser,
1711 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1712 }
1713 /* more data */
1714 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1715 user.more = 1;
1716
1717 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1718}
1719
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001720static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1721 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1722 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001723{
1724 uint8_t data[sizeof(struct gsm_mncc)];
1725 struct gsm_mncc_rtp *rtp;
1726
1727 memset(&data, 0, sizeof(data));
1728 rtp = (struct gsm_mncc_rtp *) &data[0];
1729
1730 rtp->callref = callref;
1731 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001732 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001733 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1734 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001735 }
Harald Welte27989d42018-06-21 20:39:20 +02001736 rtp->payload_type = payload_type;
1737 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001738 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001739}
1740
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001741static 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 +02001742{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001743 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001744}
1745
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001746static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001747{
1748 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001749
1750 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001751 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001752 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001753 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001754 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001755 return -EIO;
1756 }
1757 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001758 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001759 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001760 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001761 return 0;
1762 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001763 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001764
Harald Welte27989d42018-06-21 20:39:20 +02001765 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001766 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001767}
1768
1769/* Trigger TCH_RTP_CREATE acknowledgement */
1770int gsm48_tch_rtp_create(struct gsm_trans *trans)
1771{
1772 /* This function is called as soon as the port, on which the
1773 * mgcp-gw expects the incoming RTP stream from the remote
1774 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001775 struct msc_a *msc_a = trans->msc_a;
1776 struct gsm_network *net = msc_a_net(msc_a);
1777 struct call_leg *cl = msc_a->cc.call_leg;
1778 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001779 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1780 uint32_t payload_type;
1781 int payload_msg_type;
1782 const struct mgcp_conn_peer *mgcp_info;
Neels Hofmeyra001a702022-10-31 17:57:30 +01001783 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001784
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001785 if (!rtp_cn) {
1786 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1787 return -EINVAL;
1788 }
1789
1790 if (!rtp_cn->codec_known) {
1791 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1792 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1793 return -EINVAL;
1794 }
1795
1796 /* Codec */
Neels Hofmeyra001a702022-10-31 17:57:30 +01001797 m = codec_mapping_by_mgcp_codec(rtp_cn->codec);
1798 if (!m) {
1799 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1800 "Cannot RTP CREATE to MNCC, cannot resolve codec '%s'\n",
1801 osmo_mgcpc_codec_name(rtp_cn->codec));
1802 return -EINVAL;
1803 }
1804 payload_msg_type = m->mncc_payload_msg_type;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001805
1806 /* Payload Type number */
1807 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001808 if (mgcp_info && mgcp_info->ptmap_len)
1809 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1810 else
1811 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001812
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001813 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1814 if (!rtp_cn_local) {
1815 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1816 return -EINVAL;
1817 }
1818
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001819 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 +02001820}
1821
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001822static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001823{
1824 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001825 struct call_leg *cl;
1826 struct rtp_stream *rtps;
1827 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001828
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001829 /* FIXME: in *rtp we should get the codec information of the remote
1830 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1831 * meaningful value based on this information but unfortunately we
1832 * can't do that yet because the mncc API can not signal dynamic
1833 * payload types yet. This must be fixed first. Also there may be
1834 * additional members necessary in trans->conn->rtp because we
1835 * somehow need to deal with dynamic payload types that do not
1836 * comply to 3gpp's assumptions of payload type numbers on the A
1837 * interface. See also related tickets: OS#3399 and OS1683 */
1838
Harald Welte27989d42018-06-21 20:39:20 +02001839 /* Find callref */
1840 trans = trans_find_by_callref(net, rtp->callref);
1841 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001842 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001843 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001844 return -EIO;
1845 }
1846 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001847 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001848 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001849 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001850 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001851 }
1852
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001853 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001854
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001855 cl = trans->msc_a->cc.call_leg;
1856 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1857
1858 if (!rtps) {
1859 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1860 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1861 return -EINVAL;
1862 }
1863
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001864 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1865 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1866 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1867 return -EINVAL;
1868 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001869 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1870 rtp_stream_commit(rtps);
1871 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001872}
1873
1874static struct downstate {
1875 uint32_t states;
1876 int type;
1877 int (*rout) (struct gsm_trans *trans, void *arg);
1878} downstatelist[] = {
1879 /* mobile originating call establishment */
1880 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1881 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1882 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1883 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1884 {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 */
1885 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1886 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1887 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1888 /* mobile terminating call establishment */
1889 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1890 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1891 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1892 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1893 /* signalling during call */
1894 {SBIT(GSM_CSTATE_ACTIVE),
1895 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1896 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1897 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1898 {ALL_STATES,
1899 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1900 {ALL_STATES,
1901 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1902 {ALL_STATES,
1903 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1904 {SBIT(GSM_CSTATE_ACTIVE),
1905 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1906 {SBIT(GSM_CSTATE_ACTIVE),
1907 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1908 {SBIT(GSM_CSTATE_ACTIVE),
1909 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1910 {SBIT(GSM_CSTATE_ACTIVE),
1911 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1912 {SBIT(GSM_CSTATE_ACTIVE),
1913 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1914 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1915 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1916 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1917 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1918 {SBIT(GSM_CSTATE_ACTIVE),
1919 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1920 /* clearing */
1921 {SBIT(GSM_CSTATE_INITIATED),
1922 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1923 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1924 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1925 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1926 MNCC_REL_REQ, gsm48_cc_tx_release},
1927};
1928
1929#define DOWNSLLEN \
1930 (sizeof(downstatelist) / sizeof(struct downstate))
1931
1932
Philipp Maiercd64af72019-08-01 09:46:40 +02001933static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001934{
1935 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001936 struct msc_a *msc_a = NULL;
1937 struct gsm_trans *trans = NULL;
1938 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001939
Harald Welte27989d42018-06-21 20:39:20 +02001940 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001941 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001942 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001943 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001944 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001945 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001946 return rc;
1947 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001948 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001949 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001950 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001951 case MNCC_RTP_FREE:
1952 /* unused right now */
1953 return -EIO;
1954
1955 case MNCC_FRAME_DROP:
1956 case MNCC_FRAME_RECV:
1957 case GSM_TCHF_FRAME:
1958 case GSM_TCHF_FRAME_EFR:
1959 case GSM_TCHH_FRAME:
1960 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001961 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001962 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001963 return -ENOTSUP;
1964 }
1965
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001966 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001967
1968 /* Find callref */
1969 trans = trans_find_by_callref(net, data->callref);
1970
1971 /* Callref unknown */
1972 if (!trans) {
1973 struct vlr_subscr *vsub;
1974
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001975 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001976 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001977 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001978 /* Invalid call reference */
1979 return mncc_release_ind(net, NULL, data->callref,
1980 GSM48_CAUSE_LOC_PRN_S_LU,
1981 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1982 }
1983 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001984 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001985 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001986 /* Invalid number */
1987 return mncc_release_ind(net, NULL, data->callref,
1988 GSM48_CAUSE_LOC_PRN_S_LU,
1989 GSM48_CC_CAUSE_INV_NR_FORMAT);
1990 }
1991 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001992 if (data->called.number[0]) {
1993 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1994 if (!vsub)
1995 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001996 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001997 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001998 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001999 if (!vsub)
2000 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002001 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002002 }
2003 if (!vsub)
2004 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002005 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002006 /* update the subscriber we deal with */
2007 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2008
Harald Welte27989d42018-06-21 20:39:20 +02002009 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002010 if (!vsub->lu_complete) {
2011 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002012 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002013 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002014 /* Temporarily out of order */
2015 return mncc_release_ind(net, NULL, data->callref,
2016 GSM48_CAUSE_LOC_PRN_S_LU,
2017 GSM48_CC_CAUSE_DEST_OOO);
2018 }
Keith Whyte991bb422019-08-08 15:43:40 +02002019
2020 /* Find valid conn */
2021 msc_a = msc_a_for_vsub(vsub, true);
2022
2023 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2024 if (!net->call_waiting && msc_a) {
2025 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2026 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2027 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2028 "rx '%s' for subscriber %s with trans state (%s)"
2029 " rejecting with USER_BUSY\n",
2030 get_mncc_name(msg->msg_type), data->called.number,
2031 gsm48_cc_state_name(existing_cc_trans->cc.state));
2032 return mncc_release_ind(net, NULL, data->callref,
2033 GSM48_CAUSE_LOC_PRN_S_LU,
2034 GSM48_CC_CAUSE_USER_BUSY);
2035 }
2036 }
2037
Harald Welte27989d42018-06-21 20:39:20 +02002038 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002039 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002040 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002041 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002042 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002043 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002044 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002045 mncc_release_ind(net, NULL, data->callref,
2046 GSM48_CAUSE_LOC_PRN_S_LU,
2047 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2048 return -ENOMEM;
2049 }
2050
Harald Welte27989d42018-06-21 20:39:20 +02002051 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002052 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002053 /* This condition will return before the common logging of the received MNCC message below, so
2054 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002055 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002056
Harald Welte27989d42018-06-21 20:39:20 +02002057 /* store setup information until paging succeeds */
2058 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2059
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002060 /* Request a channel. If Paging already started, paging_request_start() will append the new
2061 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002062 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2063 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002064 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002065 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002066 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002067 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002068 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002069 return 0;
2070 }
2071
2072 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002073 trans->msc_a = msc_a;
2074 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002075 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002076 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002077 } else {
2078 /* update the subscriber we deal with */
2079 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2080 }
2081
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002082 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002083
Philipp Maier9ca7b312018-10-10 17:00:49 +02002084 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002085 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002086
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002087 if (trans->msc_a)
2088 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002089
2090 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002091 if (!msc_a) {
2092 struct gsm_mncc rel = {
2093 .callref = data->callref,
2094 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002095 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002096 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2097 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002098 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002099 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2100 else
2101 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2102 trans->callref = 0;
2103 trans_free(trans);
2104 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002105 }
2106
2107 /* Find function for current state and message */
2108 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002109 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002110 && ((1 << trans->cc.state) & downstatelist[i].states))
2111 break;
2112 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002113 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002114 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002115 return 0;
2116 }
2117
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002118 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002119
2120 return rc;
2121}
2122
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002123struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2124{
2125 uint32_t callref;
2126
2127 switch (msg->msg_type) {
2128 case MNCC_BRIDGE:
2129 callref = msg->bridge.callref[0];
2130 break;
2131 case MNCC_RTP_CREATE:
2132 case MNCC_RTP_CONNECT:
2133 callref = msg->rtp.callref;
2134 break;
2135
2136 case MNCC_RTP_FREE:
2137 case MNCC_FRAME_DROP:
2138 case MNCC_FRAME_RECV:
2139 case GSM_TCHF_FRAME:
2140 case GSM_TCHF_FRAME_EFR:
2141 case GSM_TCHH_FRAME:
2142 case GSM_TCH_FRAME_AMR:
2143 return NULL;
2144
2145 default:
2146 callref = msg->signal.callref;
2147 break;
2148 }
2149
2150 return mncc_call_find_by_callref(callref);
2151}
2152
2153/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002154int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002155{
2156 const union mncc_msg *msg = arg;
2157 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002158
2159 if (msg->msg_type == MNCC_SETUP_REQ) {
2160 /* Incoming call to forward for inter-MSC Handover? */
2161 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2162 if (mncc_call)
2163 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2164 "Incoming call matches pending inter-MSC Handover Number\n");
2165 }
2166 if (!mncc_call) {
2167 /* Find already active MNCC FSM for this callref.
2168 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2169 * MNCC<->GSM-CC call handling. */
2170 mncc_call = mncc_find_by_callref_from_msg(msg);
2171 }
2172 if (mncc_call) {
2173 mncc_call_rx(mncc_call, msg);
2174 return 0;
2175 }
2176
2177 /* None of the above? Then it must be a normal GSM CC call related message. */
2178 return mncc_tx_to_gsm_cc(net, msg);
2179}
Harald Welte27989d42018-06-21 20:39:20 +02002180
2181static struct datastate {
2182 uint32_t states;
2183 int type;
2184 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2185} datastatelist[] = {
2186 /* mobile originating call establishment */
2187 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2188 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2189 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2190 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2191 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2192 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2193 /* mobile terminating call establishment */
2194 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2195 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2196 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2197 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2198 {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 */
2199 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2200 /* signalling during call */
2201 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2202 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2203 {SBIT(GSM_CSTATE_ACTIVE),
2204 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2205 {ALL_STATES,
2206 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2207 {ALL_STATES,
2208 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2209 {ALL_STATES,
2210 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2211 {SBIT(GSM_CSTATE_ACTIVE),
2212 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2213 {SBIT(GSM_CSTATE_ACTIVE),
2214 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2215 {SBIT(GSM_CSTATE_ACTIVE),
2216 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2217 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2218 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2219 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2220 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2221 {SBIT(GSM_CSTATE_ACTIVE),
2222 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2223 /* clearing */
2224 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2225 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2226 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2227 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2228 {ALL_STATES, /* 5.4.3.4 */
2229 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2230};
2231
2232#define DATASLLEN \
2233 (sizeof(datastatelist) / sizeof(struct datastate))
2234
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002235int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002236{
2237 struct gsm48_hdr *gh = msgb_l3(msg);
2238 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2239 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2240 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002241 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2242 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002243 int i, rc = 0;
2244
2245 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002246 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002247 return -EINVAL;
2248 }
2249
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002250 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002251 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002252 return -EINVAL;
2253 }
2254
2255 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002256 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002257
Harald Welte27989d42018-06-21 20:39:20 +02002258 /* Create transaction */
2259 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002260 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002261 trans = trans_alloc(net, vsub,
2262 TRANS_CC,
2263 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002264 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002265 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002266 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002267 GSM48_PDISC_CC | (transaction_id << 4),
2268 GSM48_MT_CC_RELEASE_COMPL);
2269 return -ENOMEM;
2270 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002271 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2272 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2273 trans_free(trans);
2274 return -EINVAL;
2275 }
2276
Harald Welte27989d42018-06-21 20:39:20 +02002277 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002278 msc_a_get(msc_a, MSC_A_USE_CC);
2279 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002280 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002281
2282 /* An earlier CM Service Request for this CC message now has concluded */
2283 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2284 LOG_MSC_A(msc_a, LOGL_ERROR,
2285 "Creating new CC transaction without prior CM Service Request\n");
2286 else
2287 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002288 }
2289
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002290 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2291 gsm48_cc_state_name(trans->cc.state));
2292
Harald Welte27989d42018-06-21 20:39:20 +02002293 /* find function for current state and message */
2294 for (i = 0; i < DATASLLEN; i++)
2295 if ((msg_type == datastatelist[i].type)
2296 && ((1 << trans->cc.state) & datastatelist[i].states))
2297 break;
2298 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002299 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002300
2301 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2302 * transaction right away. */
2303 if (trans->cc.state == GSM_CSTATE_NULL) {
2304 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2305 " -- disarding new CC transaction right away\n");
2306 trans_free(trans);
2307 }
Harald Welte27989d42018-06-21 20:39:20 +02002308 return 0;
2309 }
2310
2311 assert(trans->vsub);
2312
2313 rc = datastatelist[i].rout(trans, msg);
2314
Harald Welte27989d42018-06-21 20:39:20 +02002315 return rc;
2316}