blob: 5715429ac629a6146d0d9ec7b567737661222553 [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>
Harald Welte27989d42018-06-21 20:39:20 +020059
60#include <osmocom/gsm/gsm48.h>
61#include <osmocom/gsm/gsm0480.h>
62#include <osmocom/gsm/gsm_utils.h>
63#include <osmocom/gsm/protocol/gsm_04_08.h>
64#include <osmocom/core/msgb.h>
65#include <osmocom/core/talloc.h>
66#include <osmocom/core/utils.h>
67#include <osmocom/core/byteswap.h>
68#include <osmocom/gsm/tlv.h>
69#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020070
71#include <assert.h>
72
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010073static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
75static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
76
77static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
78{
79 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
80 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
81 OMSC_LINKID_CB(msg) = trans->dlci;
82
83 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
84}
85
86uint32_t msc_cc_next_outgoing_callref() {
87 static uint32_t last_callref = 0x80000000;
88 last_callref++;
89 if (last_callref < 0x80000001)
90 last_callref = 0x80000001;
91 return last_callref;
92}
Harald Welte27989d42018-06-21 20:39:20 +020093
Philipp Maier9ca7b312018-10-10 17:00:49 +020094static void gsm48_cc_guard_timeout(void *arg)
95{
96 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010097 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020098 trans_free(trans);
99 return;
100}
101
102static void gsm48_stop_guard_timer(struct gsm_trans *trans)
103{
104 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100105 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200106 osmo_timer_del(&trans->cc.timer_guard);
107 }
108}
109
110static void gsm48_start_guard_timer(struct gsm_trans *trans)
111{
112 /* NOTE: The purpose of this timer is to prevent the cc state machine
113 * from hanging in cases where mncc, gsm48 or both become unresponsive
114 * for some reason. The timer is started initially with the setup from
115 * the gsm48 side and then re-started with every incoming mncc message.
116 * Once the mncc state reaches its active state the timer is stopped.
117 * So if the cc state machine does not show any activity for an
118 * extended amount of time during call setup or teardown the guard
119 * timer will time out and hard-clear the connection. */
120 if (osmo_timer_pending(&trans->cc.timer_guard))
121 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100122 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200123 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
124 osmo_timer_schedule(&trans->cc.timer_guard,
125 trans->net->mncc_guard_timeout, 0);
126}
Harald Welte27989d42018-06-21 20:39:20 +0200127
128/* Call Control */
129
130void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
131{
132 net->mncc_recv(net, msg);
133}
134
135int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
136{
137 struct gsm48_hdr *gh;
138 struct msgb *ss_notify;
139
140 ss_notify = gsm0480_create_notifySS(message);
141 if (!ss_notify)
142 return -1;
143
144 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
145 uint8_t *data = msgb_push(ss_notify, 1);
146 data[0] = ss_notify->len - 1;
147 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
148 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100149 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200150}
151
152/* FIXME: this count_statistics is a state machine behaviour. we should convert
153 * the complete call control into a state machine. Afterwards we can move this
154 * code into state transitions.
155 */
156static void count_statistics(struct gsm_trans *trans, int new_state)
157{
158 int old_state = trans->cc.state;
159 struct rate_ctr_group *msc = trans->net->msc_ctrs;
160
161 if (old_state == new_state)
162 return;
163
164 /* state incoming */
165 switch (new_state) {
166 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200167 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
168 1);
169 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200170 break;
171 }
172
173 /* state outgoing */
174 switch (old_state) {
175 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200176 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
177 1);
Harald Welte27989d42018-06-21 20:39:20 +0200178 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
179 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200180 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200181 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200182 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200183 break;
184 }
185}
186
Harald Welte27989d42018-06-21 20:39:20 +0200187static void new_cc_state(struct gsm_trans *trans, int state)
188{
189 if (state > 31 || state < 0)
190 return;
191
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100192 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
193 gsm48_cc_state_name(trans->cc.state),
194 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200195
196 count_statistics(trans, state);
197 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200198
199 /* Stop the guard timer when a call reaches the active state */
200 if (state == GSM_CSTATE_ACTIVE)
201 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200202}
203
204static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
205{
206 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
207 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
208 uint8_t *cause, *call_state;
209
210 gh->msg_type = GSM48_MT_CC_STATUS;
211
212 cause = msgb_put(msg, 3);
213 cause[0] = 2;
214 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
215 cause[2] = 0x80 | 30; /* response to status inquiry */
216
217 call_state = msgb_put(msg, 1);
218 call_state[0] = 0xc0 | 0x00;
219
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100220 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200221}
222
223static void gsm48_stop_cc_timer(struct gsm_trans *trans)
224{
225 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100226 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200227 osmo_timer_del(&trans->cc.timer);
228 trans->cc.Tcurrent = 0;
229 }
230}
231
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100232/* Log the MNCC tx and rx events.
233 * Depending on msg_type, also log whether RTP information is passed on.
234 * (This is particularly interesting for the doc/sequence_charts/msc_log_to_ladder.py)
235 */
236static void log_mncc_rx_tx(struct gsm_trans *trans, const char *rx_tx, const union mncc_msg *mncc)
237{
238 const char *sdp = NULL;
239 struct sdp_msg sdp_msg = {};
240 struct osmo_sockaddr addr = {};
241
242 if (!log_check_level(DMNCC, LOGL_DEBUG))
243 return;
244
245 switch (mncc->msg_type) {
246 case MNCC_RTP_CREATE:
247 case MNCC_RTP_CONNECT:
248 addr = (struct osmo_sockaddr){ .u.sas = mncc->rtp.addr };
249 sdp = mncc->rtp.sdp;
250 break;
251
252 case MNCC_SETUP_IND:
253 case MNCC_SETUP_REQ:
254 case MNCC_SETUP_COMPL_IND:
255 case MNCC_SETUP_COMPL_REQ:
256 case MNCC_SETUP_RSP:
257 case MNCC_SETUP_CNF:
258 case MNCC_CALL_CONF_IND:
259 case MNCC_CALL_PROC_REQ:
260 case MNCC_ALERT_IND:
261 case MNCC_ALERT_REQ:
262 sdp = mncc->signal.sdp;
263 break;
264
265 default:
266 break;
267 }
268
269 if (sdp && sdp[0] && (sdp_msg_from_sdp_str(&sdp_msg, sdp) == 0)) {
270 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s (RTP=%s)\n",
271 rx_tx,
272 get_mncc_name(mncc->msg_type),
273 sdp_msg_to_str(&sdp_msg));
274 return;
275 }
276
277 if (osmo_sockaddr_is_any(&addr) == 0) {
278 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s (RTP=%s)\n",
279 rx_tx,
280 get_mncc_name(mncc->msg_type),
281 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
282 return;
283 }
284
285 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s\n", rx_tx, get_mncc_name(mncc->msg_type));
286}
287
Harald Welte27989d42018-06-21 20:39:20 +0200288static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
289 int msg_type, struct gsm_mncc *mncc)
290{
291 struct msgb *msg;
292 unsigned char *data;
293
Harald Welte27989d42018-06-21 20:39:20 +0200294 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100295 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200296
297 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
298 if (!msg)
299 return -ENOMEM;
300
301 data = msgb_put(msg, sizeof(struct gsm_mncc));
302 memcpy(data, mncc, sizeof(struct gsm_mncc));
303
304 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200305 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
306 if (trans)
307 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200308
309 return 0;
310}
311
312int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
313 uint32_t callref, int location, int value)
314{
315 struct gsm_mncc rel;
316
317 memset(&rel, 0, sizeof(rel));
318 rel.callref = callref;
319 mncc_set_cause(&rel, location, value);
320 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
321 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
322 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
323}
324
325/* Call Control Specific transaction release.
326 * gets called by trans_free, DO NOT CALL YOURSELF! */
327void _gsm48_cc_trans_free(struct gsm_trans *trans)
328{
329 gsm48_stop_cc_timer(trans);
330
Harald Welte27989d42018-06-21 20:39:20 +0200331 /* send release to L4, if callref still exists */
332 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100333 /* Send MNCC REL.ind (cause='Resource unavailable') */
334 if (trans->cc.mncc_initiated) {
335 mncc_release_ind(trans->net, trans, trans->callref,
336 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100337 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
338 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100339 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
340 }
341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100342 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
343 * CC Release to the MS if it gets freed here. Hack it to do so. */
344 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
345 struct gsm_mncc rel = {};
346 rel.callref = trans->callref;
347 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
348 gsm48_cc_tx_release(trans, &rel);
349 }
Harald Welte27989d42018-06-21 20:39:20 +0200350 /* This is a final freeing of the transaction. The MNCC release may have triggered the
351 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
352 gsm48_stop_cc_timer(trans);
353 }
354 if (trans->cc.state != GSM_CSTATE_NULL)
355 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200356
357 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100358
359 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
360 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200361}
362
Harald Welte27989d42018-06-21 20:39:20 +0200363/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100364static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200365{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100366 if (trans->msc_a) {
367 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
368 "Handle paging error: transaction already associated with subscriber,"
369 " apparently it was already handled. Skip.\n");
370 return;
Harald Welte27989d42018-06-21 20:39:20 +0200371 }
372
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100373 if (msc_a) {
374 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
375 /* Assign conn */
376 msc_a_get(msc_a, MSC_A_USE_CC);
377 trans->msc_a = msc_a;
378 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200379
380 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300381 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200382 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300383 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
384 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
385 &trans->cc.msg.gcr[0],
386 sizeof(trans->cc.msg.gcr));
387 if (rc < 0)
388 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
389 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200390 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200391 }
392
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100393 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
394 /* send SETUP request to called party */
395 gsm48_cc_tx_setup(trans, &trans->cc.msg);
396 } else {
397 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
398 /* Temporarily out of order */
399 mncc_release_ind(trans->net, trans,
400 trans->callref,
401 GSM48_CAUSE_LOC_PRN_S_LU,
402 GSM48_CC_CAUSE_DEST_OOO);
403 trans->callref = 0;
404 trans->paging_request = NULL;
405 trans_free(trans);
406 }
Harald Welte27989d42018-06-21 20:39:20 +0200407}
408
409/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100410static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200411{
412 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
413 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100414 struct call_leg *cl1;
415 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200416
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100417 if (!trans1 || !trans2) {
418 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200419 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100420 }
Harald Welte27989d42018-06-21 20:39:20 +0200421
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100422 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100423 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
424 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 +0200425 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100426 }
427
428 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
429 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200430
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100431 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
432 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
433 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
434 * If we can't, then we must give up. */
435 cl1 = trans1->msc_a->cc.call_leg;
436 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200437
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100438 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200439}
440
441static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
442{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100443 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200444 return gsm48_cc_tx_status(trans, msg);
445}
446
Harald Welte27989d42018-06-21 20:39:20 +0200447static void gsm48_cc_timeout(void *arg)
448{
449 struct gsm_trans *trans = arg;
450 int disconnect = 0, release = 0;
451 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
452 int mo_location = GSM48_CAUSE_LOC_USER;
453 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
454 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
455 struct gsm_mncc mo_rel, l4_rel;
456
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200457 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
458
Harald Welte27989d42018-06-21 20:39:20 +0200459 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
460 mo_rel.callref = trans->callref;
461 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
462 l4_rel.callref = trans->callref;
463
464 switch(trans->cc.Tcurrent) {
465 case 0x303:
466 release = 1;
467 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
468 break;
469 case 0x310:
470 disconnect = 1;
471 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
472 break;
473 case 0x313:
474 disconnect = 1;
475 /* unknown, did not find it in the specs */
476 break;
477 case 0x301:
478 disconnect = 1;
479 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
480 break;
481 case 0x308:
482 if (!trans->cc.T308_second) {
483 /* restart T308 a second time */
484 gsm48_cc_tx_release(trans, &trans->cc.msg);
485 trans->cc.T308_second = 1;
486 break; /* stay in release state */
487 }
488 trans_free(trans);
489 return;
490 case 0x306:
491 release = 1;
492 mo_cause = trans->cc.msg.cause.value;
493 mo_location = trans->cc.msg.cause.location;
494 break;
495 case 0x323:
496 disconnect = 1;
497 break;
498 default:
499 release = 1;
500 }
501
502 if (release && trans->callref) {
503 /* process release towards layer 4 */
504 mncc_release_ind(trans->net, trans, trans->callref,
505 l4_location, l4_cause);
506 trans->callref = 0;
507 }
508
509 if (disconnect && trans->callref) {
510 /* process disconnect towards layer 4 */
511 mncc_set_cause(&l4_rel, l4_location, l4_cause);
512 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
513 }
514
515 /* process disconnect towards mobile station */
516 if (disconnect || release) {
517 mncc_set_cause(&mo_rel, mo_location, mo_cause);
518 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
519 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
520 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
521 mo_rel.cause.diag_len = 3;
522
523 if (disconnect)
524 gsm48_cc_tx_disconnect(trans, &mo_rel);
525 if (release)
526 gsm48_cc_tx_release(trans, &mo_rel);
527 }
528
529}
530
531/* disconnect both calls from the bridge */
532static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100533 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200534{
535 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
536 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
537 struct gsm_mncc mx_rel;
538 if (!trans0 || !trans1)
539 return;
540
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100541 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
542 trans0->callref, trans1->callref, strerror(err));
543 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200544 trans0->callref, trans1->callref, strerror(err));
545
546 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
547 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
548 GSM48_CC_CAUSE_CHAN_UNACCEPT);
549
550 mx_rel.callref = trans0->callref;
551 gsm48_cc_tx_disconnect(trans0, &mx_rel);
552
553 mx_rel.callref = trans1->callref;
554 gsm48_cc_tx_disconnect(trans1, &mx_rel);
555}
556
557static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
558 int sec, int micro)
559{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100560 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200561 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
562 osmo_timer_schedule(&trans->cc.timer, sec, micro);
563 trans->cc.Tcurrent = current;
564}
565
566static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
567{
568 struct gsm48_hdr *gh = msgb_l3(msg);
569 uint8_t msg_type = gsm48_hdr_msg_type(gh);
570 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
571 struct tlv_parsed tp;
572 struct gsm_mncc setup;
573
Philipp Maier9ca7b312018-10-10 17:00:49 +0200574 gsm48_start_guard_timer(trans);
575
Harald Welte27989d42018-06-21 20:39:20 +0200576 memset(&setup, 0, sizeof(struct gsm_mncc));
577 setup.callref = trans->callref;
578
Keith Whytea1a70be2021-05-16 02:59:52 +0200579 /* New Global Call Reference */
580 if (!trans->cc.lcls)
581 trans->cc.lcls = trans_lcls_compose(trans, true);
582
583 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300584 if (trans->cc.lcls) {
585 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
586 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
587 int rc;
588
589 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
590 memcpy(&setup.gcr[0], gcr_msg->data, rc);
591 setup.fields |= MNCC_F_GCR;
592 } else
593 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
594 msgb_free(gcr_msg);
595 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200596
Harald Welte27989d42018-06-21 20:39:20 +0200597 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
598 /* emergency setup is identified by msg_type */
599 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
600 setup.fields |= MNCC_F_EMERGENCY;
601 setup.emergency = 1;
602 /* use destination number as configured by user (if any) */
603 if (trans->net->emergency.route_to_msisdn) {
604 setup.fields |= MNCC_F_CALLED;
605 setup.called.type = 0; /* unknown */
606 setup.called.plan = 0; /* unknown */
607 OSMO_STRLCPY_ARRAY(setup.called.number,
608 trans->net->emergency.route_to_msisdn);
609 }
610 }
611
612 /* use subscriber as calling party number */
613 setup.fields |= MNCC_F_CALLING;
614 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
615 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
616
617 /* bearer capability */
618 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
619 setup.fields |= MNCC_F_BEARER_CAP;
620 gsm48_decode_bearer_cap(&setup.bearer_cap,
621 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
622
623 /* Create a copy of the bearer capability
624 * in the transaction struct, so we can use
625 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100626 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200627 sizeof(trans->bearer_cap));
628 }
629 /* facility */
630 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
631 setup.fields |= MNCC_F_FACILITY;
632 gsm48_decode_facility(&setup.facility,
633 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
634 }
635 /* called party bcd number */
636 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
637 setup.fields |= MNCC_F_CALLED;
638 gsm48_decode_called(&setup.called,
639 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
640 }
641 /* user-user */
642 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
643 setup.fields |= MNCC_F_USERUSER;
644 gsm48_decode_useruser(&setup.useruser,
645 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
646 }
647 /* ss-version */
648 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
649 setup.fields |= MNCC_F_SSVERSION;
650 gsm48_decode_ssversion(&setup.ssversion,
651 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
652 }
653 /* CLIR suppression */
654 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
655 setup.clir.sup = 1;
656 /* CLIR invocation */
657 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
658 setup.clir.inv = 1;
659 /* cc cap */
660 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
661 setup.fields |= MNCC_F_CCCAP;
662 gsm48_decode_cccap(&setup.cccap,
663 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
664 }
665
666 new_cc_state(trans, GSM_CSTATE_INITIATED);
667
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100668 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
669 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200670
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200671 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200672
673 /* indicate setup to MNCC */
674 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
675
676 /* MNCC code will modify the channel asynchronously, we should
677 * ipaccess-bind only after the modification has been made to the
678 * lchan->tch_mode */
679 return 0;
680}
681
682static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
683{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100684 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200685 struct gsm48_hdr *gh;
686 struct gsm_mncc *setup = arg;
687 int rc, trans_id;
688
689 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
690
691 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700692 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100693 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200694 "This is not allowed!\n");
695 /* Temporarily out of order */
696 rc = mncc_release_ind(trans->net, trans, trans->callref,
697 GSM48_CAUSE_LOC_PRN_S_LU,
698 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
699 trans->callref = 0;
700 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200701 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200702 return rc;
703 }
704
705 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200707 if (trans_id < 0) {
708 /* no free transaction ID */
709 rc = mncc_release_ind(trans->net, trans, trans->callref,
710 GSM48_CAUSE_LOC_PRN_S_LU,
711 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
712 trans->callref = 0;
713 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200714 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200715 return rc;
716 }
717 trans->transaction_id = trans_id;
718
719 gh->msg_type = GSM48_MT_CC_SETUP;
720
721 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
722
723 /* bearer capability */
724 if (setup->fields & MNCC_F_BEARER_CAP) {
725 /* Create a copy of the bearer capability in the transaction struct, so we
726 * can use this information later */
727 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
728 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
729 }
730 /* facility */
731 if (setup->fields & MNCC_F_FACILITY)
732 gsm48_encode_facility(msg, 0, &setup->facility);
733 /* progress */
734 if (setup->fields & MNCC_F_PROGRESS)
735 gsm48_encode_progress(msg, 0, &setup->progress);
736 /* calling party BCD number */
737 if (setup->fields & MNCC_F_CALLING)
738 gsm48_encode_calling(msg, &setup->calling);
739 /* called party BCD number */
740 if (setup->fields & MNCC_F_CALLED)
741 gsm48_encode_called(msg, &setup->called);
742 /* user-user */
743 if (setup->fields & MNCC_F_USERUSER)
744 gsm48_encode_useruser(msg, 0, &setup->useruser);
745 /* redirecting party BCD number */
746 if (setup->fields & MNCC_F_REDIRECTING)
747 gsm48_encode_redirecting(msg, &setup->redirecting);
748 /* signal */
749 if (setup->fields & MNCC_F_SIGNAL)
750 gsm48_encode_signal(msg, setup->signal);
751
752 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
753
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200754 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200755
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100756 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200757}
758
759static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
760{
761 struct gsm48_hdr *gh = msgb_l3(msg);
762 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
763 struct tlv_parsed tp;
764 struct gsm_mncc call_conf;
765 int rc;
766
767 gsm48_stop_cc_timer(trans);
768 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
769
770 memset(&call_conf, 0, sizeof(struct gsm_mncc));
771 call_conf.callref = trans->callref;
772
773 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
774#if 0
775 /* repeat */
776 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
777 call_conf.repeat = 1;
778 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
779 call_conf.repeat = 2;
780#endif
781 /* bearer capability */
782 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
783 call_conf.fields |= MNCC_F_BEARER_CAP;
784 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
785 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
786
787 /* Create a copy of the bearer capability
788 * in the transaction struct, so we can use
789 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100790 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200791 sizeof(trans->bearer_cap));
792 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100793
Harald Welte27989d42018-06-21 20:39:20 +0200794 /* cause */
795 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
796 call_conf.fields |= MNCC_F_CAUSE;
797 gsm48_decode_cause(&call_conf.cause,
798 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
799 }
800 /* cc cap */
801 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
802 call_conf.fields |= MNCC_F_CCCAP;
803 gsm48_decode_cccap(&call_conf.cccap,
804 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
805 }
806
807 /* IMSI of called subscriber */
808 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
809
810 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
811
812 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100813 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200814
815 /* don't continue, if there were problems with
816 * the call assignment. */
817 if (rc)
818 return rc;
819
820 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
821 &call_conf);
822}
823
824static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
825{
826 struct gsm_mncc *proceeding = arg;
827 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
828 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
829 int rc;
830
831 gh->msg_type = GSM48_MT_CC_CALL_PROC;
832
833 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
834
835 /* bearer capability */
836 if (proceeding->fields & MNCC_F_BEARER_CAP) {
837 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
838 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
839 }
840 /* facility */
841 if (proceeding->fields & MNCC_F_FACILITY)
842 gsm48_encode_facility(msg, 0, &proceeding->facility);
843 /* progress */
844 if (proceeding->fields & MNCC_F_PROGRESS)
845 gsm48_encode_progress(msg, 0, &proceeding->progress);
846
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100847 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200848 if (rc)
849 return rc;
850
851 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100852 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200853}
854
855static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
856{
857 struct gsm48_hdr *gh = msgb_l3(msg);
858 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
859 struct tlv_parsed tp;
860 struct gsm_mncc alerting;
861
862 gsm48_stop_cc_timer(trans);
863 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
864
865 memset(&alerting, 0, sizeof(struct gsm_mncc));
866 alerting.callref = trans->callref;
867 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
868 /* facility */
869 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
870 alerting.fields |= MNCC_F_FACILITY;
871 gsm48_decode_facility(&alerting.facility,
872 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
873 }
874
875 /* progress */
876 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
877 alerting.fields |= MNCC_F_PROGRESS;
878 gsm48_decode_progress(&alerting.progress,
879 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
880 }
881 /* ss-version */
882 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
883 alerting.fields |= MNCC_F_SSVERSION;
884 gsm48_decode_ssversion(&alerting.ssversion,
885 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
886 }
887
888 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
889
890 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
891 &alerting);
892}
893
894static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
895{
896 struct gsm_mncc *alerting = arg;
897 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
898 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
899
900 gh->msg_type = GSM48_MT_CC_ALERTING;
901
902 /* facility */
903 if (alerting->fields & MNCC_F_FACILITY)
904 gsm48_encode_facility(msg, 0, &alerting->facility);
905 /* progress */
906 if (alerting->fields & MNCC_F_PROGRESS)
907 gsm48_encode_progress(msg, 0, &alerting->progress);
908 /* user-user */
909 if (alerting->fields & MNCC_F_USERUSER)
910 gsm48_encode_useruser(msg, 0, &alerting->useruser);
911
912 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
913
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100914 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200915}
916
917static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
918{
919 struct gsm_mncc *progress = arg;
920 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
921 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
922
923 gh->msg_type = GSM48_MT_CC_PROGRESS;
924
925 /* progress */
926 gsm48_encode_progress(msg, 1, &progress->progress);
927 /* user-user */
928 if (progress->fields & MNCC_F_USERUSER)
929 gsm48_encode_useruser(msg, 0, &progress->useruser);
930
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100931 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200932}
933
934static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
935{
936 struct gsm_mncc *connect = arg;
937 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
938 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
939
940 gh->msg_type = GSM48_MT_CC_CONNECT;
941
942 gsm48_stop_cc_timer(trans);
943 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
944
945 /* facility */
946 if (connect->fields & MNCC_F_FACILITY)
947 gsm48_encode_facility(msg, 0, &connect->facility);
948 /* progress */
949 if (connect->fields & MNCC_F_PROGRESS)
950 gsm48_encode_progress(msg, 0, &connect->progress);
951 /* connected number */
952 if (connect->fields & MNCC_F_CONNECTED)
953 gsm48_encode_connected(msg, &connect->connected);
954 /* user-user */
955 if (connect->fields & MNCC_F_USERUSER)
956 gsm48_encode_useruser(msg, 0, &connect->useruser);
957
958 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
959
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100960 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200961}
962
963static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
964{
965 struct gsm48_hdr *gh = msgb_l3(msg);
966 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
967 struct tlv_parsed tp;
968 struct gsm_mncc connect;
969
970 gsm48_stop_cc_timer(trans);
971
972 memset(&connect, 0, sizeof(struct gsm_mncc));
973 connect.callref = trans->callref;
974 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
975 /* use subscriber as connected party number */
976 connect.fields |= MNCC_F_CONNECTED;
977 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
978 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
979
980 /* facility */
981 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
982 connect.fields |= MNCC_F_FACILITY;
983 gsm48_decode_facility(&connect.facility,
984 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
985 }
986 /* user-user */
987 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
988 connect.fields |= MNCC_F_USERUSER;
989 gsm48_decode_useruser(&connect.useruser,
990 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
991 }
992 /* ss-version */
993 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
994 connect.fields |= MNCC_F_SSVERSION;
995 gsm48_decode_ssversion(&connect.ssversion,
996 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
997 }
998
999 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001000 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001001
1002 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1003}
1004
1005
1006static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1007{
1008 struct gsm_mncc connect_ack;
1009
1010 gsm48_stop_cc_timer(trans);
1011
1012 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001013 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 +02001014
1015 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1016 connect_ack.callref = trans->callref;
1017
1018 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1019 &connect_ack);
1020}
1021
1022static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1023{
1024 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1025 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1026
1027 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1028
1029 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1030
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001031 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001032}
1033
1034static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1035{
1036 struct gsm48_hdr *gh = msgb_l3(msg);
1037 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1038 struct tlv_parsed tp;
1039 struct gsm_mncc disc;
1040
1041 gsm48_stop_cc_timer(trans);
1042
1043 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1044
1045 memset(&disc, 0, sizeof(struct gsm_mncc));
1046 disc.callref = trans->callref;
1047 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1048 /* cause */
1049 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1050 disc.fields |= MNCC_F_CAUSE;
1051 gsm48_decode_cause(&disc.cause,
1052 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1053 }
1054 /* facility */
1055 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1056 disc.fields |= MNCC_F_FACILITY;
1057 gsm48_decode_facility(&disc.facility,
1058 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1059 }
1060 /* user-user */
1061 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1062 disc.fields |= MNCC_F_USERUSER;
1063 gsm48_decode_useruser(&disc.useruser,
1064 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1065 }
1066 /* ss-version */
1067 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1068 disc.fields |= MNCC_F_SSVERSION;
1069 gsm48_decode_ssversion(&disc.ssversion,
1070 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1071 }
1072
1073 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001074}
1075
1076static struct gsm_mncc_cause default_cause = {
1077 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1078 .coding = 0,
1079 .rec = 0,
1080 .rec_val = 0,
1081 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1082 .diag_len = 0,
1083 .diag = { 0 },
1084};
1085
1086static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1087{
1088 struct gsm_mncc *disc = arg;
1089 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1090 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1091
1092 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1093
1094 gsm48_stop_cc_timer(trans);
1095 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1096
1097 /* cause */
1098 if (disc->fields & MNCC_F_CAUSE)
1099 gsm48_encode_cause(msg, 1, &disc->cause);
1100 else
1101 gsm48_encode_cause(msg, 1, &default_cause);
1102
1103 /* facility */
1104 if (disc->fields & MNCC_F_FACILITY)
1105 gsm48_encode_facility(msg, 0, &disc->facility);
1106 /* progress */
1107 if (disc->fields & MNCC_F_PROGRESS)
1108 gsm48_encode_progress(msg, 0, &disc->progress);
1109 /* user-user */
1110 if (disc->fields & MNCC_F_USERUSER)
1111 gsm48_encode_useruser(msg, 0, &disc->useruser);
1112
1113 /* store disconnect cause for T306 expiry */
1114 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1115
1116 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1117
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001118 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001119}
1120
1121static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1122{
1123 struct gsm48_hdr *gh = msgb_l3(msg);
1124 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1125 struct tlv_parsed tp;
1126 struct gsm_mncc rel;
1127 int rc;
1128
1129 gsm48_stop_cc_timer(trans);
1130
1131 memset(&rel, 0, sizeof(struct gsm_mncc));
1132 rel.callref = trans->callref;
1133 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1134 /* cause */
1135 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1136 rel.fields |= MNCC_F_CAUSE;
1137 gsm48_decode_cause(&rel.cause,
1138 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1139 }
1140 /* facility */
1141 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1142 rel.fields |= MNCC_F_FACILITY;
1143 gsm48_decode_facility(&rel.facility,
1144 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1145 }
1146 /* user-user */
1147 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1148 rel.fields |= MNCC_F_USERUSER;
1149 gsm48_decode_useruser(&rel.useruser,
1150 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1151 }
1152 /* ss-version */
1153 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1154 rel.fields |= MNCC_F_SSVERSION;
1155 gsm48_decode_ssversion(&rel.ssversion,
1156 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1157 }
1158
1159 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1160 /* release collision 5.4.5 */
1161 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1162 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001163 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001164 GSM48_PDISC_CC | (trans->transaction_id << 4),
1165 GSM48_MT_CC_RELEASE_COMPL);
1166 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1167 }
1168
1169 new_cc_state(trans, GSM_CSTATE_NULL);
1170
1171 trans->callref = 0;
1172 trans_free(trans);
1173
1174 return rc;
1175}
1176
1177static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1178{
1179 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001180 struct msgb *msg;
1181 struct gsm48_hdr *gh;
1182
1183 if (!trans->msc_a) {
1184 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1185 return -EINVAL;
1186 }
1187
1188 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1189 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001190
1191 gh->msg_type = GSM48_MT_CC_RELEASE;
1192
1193 gsm48_stop_cc_timer(trans);
1194 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1195
1196 /* cause */
1197 if (rel->fields & MNCC_F_CAUSE)
1198 gsm48_encode_cause(msg, 0, &rel->cause);
1199 /* facility */
1200 if (rel->fields & MNCC_F_FACILITY)
1201 gsm48_encode_facility(msg, 0, &rel->facility);
1202 /* user-user */
1203 if (rel->fields & MNCC_F_USERUSER)
1204 gsm48_encode_useruser(msg, 0, &rel->useruser);
1205
1206 trans->cc.T308_second = 0;
1207 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1208
1209 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1210 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1211
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001212 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001213}
1214
1215static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1216{
1217 struct gsm48_hdr *gh = msgb_l3(msg);
1218 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1219 struct tlv_parsed tp;
1220 struct gsm_mncc rel;
1221 int rc = 0;
1222
1223 gsm48_stop_cc_timer(trans);
1224
1225 memset(&rel, 0, sizeof(struct gsm_mncc));
1226 rel.callref = trans->callref;
1227 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1228 /* cause */
1229 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1230 rel.fields |= MNCC_F_CAUSE;
1231 gsm48_decode_cause(&rel.cause,
1232 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1233 }
1234 /* facility */
1235 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1236 rel.fields |= MNCC_F_FACILITY;
1237 gsm48_decode_facility(&rel.facility,
1238 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1239 }
1240 /* user-user */
1241 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1242 rel.fields |= MNCC_F_USERUSER;
1243 gsm48_decode_useruser(&rel.useruser,
1244 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1245 }
1246 /* ss-version */
1247 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1248 rel.fields |= MNCC_F_SSVERSION;
1249 gsm48_decode_ssversion(&rel.ssversion,
1250 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1251 }
1252
1253 if (trans->callref) {
1254 switch (trans->cc.state) {
1255 case GSM_CSTATE_CALL_PRESENT:
1256 rc = mncc_recvmsg(trans->net, trans,
1257 MNCC_REJ_IND, &rel);
1258 break;
1259 case GSM_CSTATE_RELEASE_REQ:
1260 rc = mncc_recvmsg(trans->net, trans,
1261 MNCC_REL_CNF, &rel);
1262 break;
1263 default:
1264 rc = mncc_recvmsg(trans->net, trans,
1265 MNCC_REL_IND, &rel);
1266 }
1267 }
1268
1269 trans->callref = 0;
1270 trans_free(trans);
1271
1272 return rc;
1273}
1274
1275static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1276{
1277 struct gsm_mncc *rel = arg;
1278 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1279 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1280 int ret;
1281
1282 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1283
1284 trans->callref = 0;
1285
1286 gsm48_stop_cc_timer(trans);
1287
1288 /* cause */
1289 if (rel->fields & MNCC_F_CAUSE)
1290 gsm48_encode_cause(msg, 0, &rel->cause);
1291 /* facility */
1292 if (rel->fields & MNCC_F_FACILITY)
1293 gsm48_encode_facility(msg, 0, &rel->facility);
1294 /* user-user */
1295 if (rel->fields & MNCC_F_USERUSER)
1296 gsm48_encode_useruser(msg, 0, &rel->useruser);
1297
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001298 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001299
1300 trans_free(trans);
1301
1302 return ret;
1303}
1304
1305static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1306{
1307 struct gsm48_hdr *gh = msgb_l3(msg);
1308 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1309 struct tlv_parsed tp;
1310 struct gsm_mncc fac;
1311
1312 memset(&fac, 0, sizeof(struct gsm_mncc));
1313 fac.callref = trans->callref;
1314 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1315 /* facility */
1316 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1317 fac.fields |= MNCC_F_FACILITY;
1318 gsm48_decode_facility(&fac.facility,
1319 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1320 }
1321 /* ss-version */
1322 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1323 fac.fields |= MNCC_F_SSVERSION;
1324 gsm48_decode_ssversion(&fac.ssversion,
1325 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1326 }
1327
1328 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1329}
1330
1331static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1332{
1333 struct gsm_mncc *fac = arg;
1334 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1335 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1336
1337 gh->msg_type = GSM48_MT_CC_FACILITY;
1338
1339 /* facility */
1340 gsm48_encode_facility(msg, 1, &fac->facility);
1341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001342 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001343}
1344
1345static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1346{
1347 struct gsm_mncc hold;
1348
1349 memset(&hold, 0, sizeof(struct gsm_mncc));
1350 hold.callref = trans->callref;
1351 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1352}
1353
1354static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1355{
1356 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1357 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1358
1359 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1360
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001361 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001362}
1363
1364static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1365{
1366 struct gsm_mncc *hold_rej = arg;
1367 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1368 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1369
1370 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1371
1372 /* cause */
1373 if (hold_rej->fields & MNCC_F_CAUSE)
1374 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1375 else
1376 gsm48_encode_cause(msg, 1, &default_cause);
1377
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001378 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001379}
1380
1381static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1382{
1383 struct gsm_mncc retrieve;
1384
1385 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1386 retrieve.callref = trans->callref;
1387 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1388 &retrieve);
1389}
1390
1391static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1392{
1393 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1394 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1395
1396 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1397
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001398 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001399}
1400
1401static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1402{
1403 struct gsm_mncc *retrieve_rej = arg;
1404 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1405 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1406
1407 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1408
1409 /* cause */
1410 if (retrieve_rej->fields & MNCC_F_CAUSE)
1411 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1412 else
1413 gsm48_encode_cause(msg, 1, &default_cause);
1414
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001415 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001416}
1417
1418static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1419{
1420 struct gsm48_hdr *gh = msgb_l3(msg);
1421 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1422 struct tlv_parsed tp;
1423 struct gsm_mncc dtmf;
1424
1425 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1426 dtmf.callref = trans->callref;
1427 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1428 /* keypad facility */
1429 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1430 dtmf.fields |= MNCC_F_KEYPAD;
1431 gsm48_decode_keypad(&dtmf.keypad,
1432 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1433 }
1434
1435 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1436}
1437
1438static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1439{
1440 struct gsm_mncc *dtmf = arg;
1441 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1443
1444 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1445
1446 /* keypad */
1447 if (dtmf->fields & MNCC_F_KEYPAD)
1448 gsm48_encode_keypad(msg, dtmf->keypad);
1449
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001450 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001451}
1452
1453static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1454{
1455 struct gsm_mncc *dtmf = arg;
1456 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1457 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1458
1459 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1460
1461 /* cause */
1462 if (dtmf->fields & MNCC_F_CAUSE)
1463 gsm48_encode_cause(msg, 1, &dtmf->cause);
1464 else
1465 gsm48_encode_cause(msg, 1, &default_cause);
1466
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001467 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001468}
1469
1470static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1471{
1472 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1473 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1474
1475 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
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_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1481{
1482 struct gsm_mncc dtmf;
1483
1484 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1485 dtmf.callref = trans->callref;
1486
1487 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1488}
1489
1490static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1491{
1492 struct gsm48_hdr *gh = msgb_l3(msg);
1493 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1494 struct tlv_parsed tp;
1495 struct gsm_mncc modify;
1496
1497 memset(&modify, 0, sizeof(struct gsm_mncc));
1498 modify.callref = trans->callref;
1499 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1500 /* bearer capability */
1501 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1502 modify.fields |= MNCC_F_BEARER_CAP;
1503 gsm48_decode_bearer_cap(&modify.bearer_cap,
1504 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1505
1506 /* Create a copy of the bearer capability
1507 * in the transaction struct, so we can use
1508 * this information later */
1509 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1510 sizeof(trans->bearer_cap));
1511 }
1512
1513 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1514
1515 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1516}
1517
1518static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1519{
1520 struct gsm_mncc *modify = arg;
1521 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1522 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1523
1524 gh->msg_type = GSM48_MT_CC_MODIFY;
1525
1526 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1527
1528 /* bearer capability */
1529 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1530 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1531
1532 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1533
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001534 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001535}
1536
1537static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1538{
1539 struct gsm48_hdr *gh = msgb_l3(msg);
1540 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1541 struct tlv_parsed tp;
1542 struct gsm_mncc modify;
1543
1544 gsm48_stop_cc_timer(trans);
1545
1546 memset(&modify, 0, sizeof(struct gsm_mncc));
1547 modify.callref = trans->callref;
1548 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1549 /* bearer capability */
1550 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1551 modify.fields |= MNCC_F_BEARER_CAP;
1552 gsm48_decode_bearer_cap(&modify.bearer_cap,
1553 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1554
1555 /* Create a copy of the bearer capability
1556 * in the transaction struct, so we can use
1557 * this information later */
1558 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1559 sizeof(trans->bearer_cap));
1560 }
1561
1562 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1563
1564 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1565}
1566
1567static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1568{
1569 struct gsm_mncc *modify = arg;
1570 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1571 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1572
1573 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1574
1575 /* bearer capability */
1576 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1577 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1578
1579 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1580
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001581 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001582}
1583
1584static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1585{
1586 struct gsm48_hdr *gh = msgb_l3(msg);
1587 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1588 struct tlv_parsed tp;
1589 struct gsm_mncc modify;
1590
1591 gsm48_stop_cc_timer(trans);
1592
1593 memset(&modify, 0, sizeof(struct gsm_mncc));
1594 modify.callref = trans->callref;
1595 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1596 /* bearer capability */
1597 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1598 modify.fields |= GSM48_IE_BEARER_CAP;
1599 gsm48_decode_bearer_cap(&modify.bearer_cap,
1600 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1601
1602 /* Create a copy of the bearer capability
1603 * in the transaction struct, so we can use
1604 * this information later */
1605 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1606 sizeof(trans->bearer_cap));
1607 }
1608 /* cause */
1609 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1610 modify.fields |= MNCC_F_CAUSE;
1611 gsm48_decode_cause(&modify.cause,
1612 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1613 }
1614
1615 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1616
1617 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1618}
1619
1620static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1621{
1622 struct gsm_mncc *modify = arg;
1623 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1624 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1625
1626 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1627
1628 /* bearer capability */
1629 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1630 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1631 /* cause */
1632 gsm48_encode_cause(msg, 1, &modify->cause);
1633
1634 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1635
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001636 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001637}
1638
1639static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1640{
1641 struct gsm_mncc *notify = arg;
1642 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1643 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1644
1645 gh->msg_type = GSM48_MT_CC_NOTIFY;
1646
1647 /* notify */
1648 gsm48_encode_notify(msg, notify->notify);
1649
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001650 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001651}
1652
1653static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1654{
1655 struct gsm48_hdr *gh = msgb_l3(msg);
1656 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1657// struct tlv_parsed tp;
1658 struct gsm_mncc notify;
1659
1660 memset(&notify, 0, sizeof(struct gsm_mncc));
1661 notify.callref = trans->callref;
1662// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1663 if (payload_len >= 1)
1664 gsm48_decode_notify(&notify.notify, gh->data);
1665
1666 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1667}
1668
1669static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1670{
1671 struct gsm_mncc *user = arg;
1672 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1673 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1674
1675 gh->msg_type = GSM48_MT_CC_USER_INFO;
1676
1677 /* user-user */
1678 if (user->fields & MNCC_F_USERUSER)
1679 gsm48_encode_useruser(msg, 1, &user->useruser);
1680 /* more data */
1681 if (user->more)
1682 gsm48_encode_more(msg);
1683
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001684 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001685}
1686
1687static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1688{
1689 struct gsm48_hdr *gh = msgb_l3(msg);
1690 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1691 struct tlv_parsed tp;
1692 struct gsm_mncc user;
1693
1694 memset(&user, 0, sizeof(struct gsm_mncc));
1695 user.callref = trans->callref;
1696 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1697 /* user-user */
1698 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1699 user.fields |= MNCC_F_USERUSER;
1700 gsm48_decode_useruser(&user.useruser,
1701 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1702 }
1703 /* more data */
1704 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1705 user.more = 1;
1706
1707 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1708}
1709
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001710static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1711 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1712 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001713{
1714 uint8_t data[sizeof(struct gsm_mncc)];
1715 struct gsm_mncc_rtp *rtp;
1716
1717 memset(&data, 0, sizeof(data));
1718 rtp = (struct gsm_mncc_rtp *) &data[0];
1719
1720 rtp->callref = callref;
1721 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001722 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001723 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1724 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001725 }
Harald Welte27989d42018-06-21 20:39:20 +02001726 rtp->payload_type = payload_type;
1727 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001728 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001729}
1730
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001731static 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 +02001732{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001733 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001734}
1735
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001736static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001737{
1738 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001739
1740 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001741 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001742 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001743 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001744 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001745 return -EIO;
1746 }
1747 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001748 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001749 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001750 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001751 return 0;
1752 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001753 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001754
Harald Welte27989d42018-06-21 20:39:20 +02001755 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001756 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001757}
1758
1759/* Trigger TCH_RTP_CREATE acknowledgement */
1760int gsm48_tch_rtp_create(struct gsm_trans *trans)
1761{
1762 /* This function is called as soon as the port, on which the
1763 * mgcp-gw expects the incoming RTP stream from the remote
1764 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001765 struct msc_a *msc_a = trans->msc_a;
1766 struct gsm_network *net = msc_a_net(msc_a);
1767 struct call_leg *cl = msc_a->cc.call_leg;
1768 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001769 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1770 uint32_t payload_type;
1771 int payload_msg_type;
1772 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001773
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001774 if (!rtp_cn) {
1775 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1776 return -EINVAL;
1777 }
1778
1779 if (!rtp_cn->codec_known) {
1780 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1781 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1782 return -EINVAL;
1783 }
1784
1785 /* Codec */
1786 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1787
1788 /* Payload Type number */
1789 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001790 if (mgcp_info && mgcp_info->ptmap_len)
1791 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1792 else
1793 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001794
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001795 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1796 if (!rtp_cn_local) {
1797 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1798 return -EINVAL;
1799 }
1800
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001801 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 +02001802}
1803
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001804static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001805{
1806 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001807 struct call_leg *cl;
1808 struct rtp_stream *rtps;
1809 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001810
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001811 /* FIXME: in *rtp we should get the codec information of the remote
1812 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1813 * meaningful value based on this information but unfortunately we
1814 * can't do that yet because the mncc API can not signal dynamic
1815 * payload types yet. This must be fixed first. Also there may be
1816 * additional members necessary in trans->conn->rtp because we
1817 * somehow need to deal with dynamic payload types that do not
1818 * comply to 3gpp's assumptions of payload type numbers on the A
1819 * interface. See also related tickets: OS#3399 and OS1683 */
1820
Harald Welte27989d42018-06-21 20:39:20 +02001821 /* Find callref */
1822 trans = trans_find_by_callref(net, rtp->callref);
1823 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001824 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001825 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001826 return -EIO;
1827 }
1828 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001829 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001830 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001831 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001832 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001833 }
1834
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001835 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001836
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001837 cl = trans->msc_a->cc.call_leg;
1838 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1839
1840 if (!rtps) {
1841 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1842 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1843 return -EINVAL;
1844 }
1845
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001846 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1847 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1848 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1849 return -EINVAL;
1850 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001851 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1852 rtp_stream_commit(rtps);
1853 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001854}
1855
1856static struct downstate {
1857 uint32_t states;
1858 int type;
1859 int (*rout) (struct gsm_trans *trans, void *arg);
1860} downstatelist[] = {
1861 /* mobile originating call establishment */
1862 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1863 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1864 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1865 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1866 {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 */
1867 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1868 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1869 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1870 /* mobile terminating call establishment */
1871 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1872 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1873 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1874 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1875 /* signalling during call */
1876 {SBIT(GSM_CSTATE_ACTIVE),
1877 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1878 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1879 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1880 {ALL_STATES,
1881 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1882 {ALL_STATES,
1883 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1884 {ALL_STATES,
1885 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1886 {SBIT(GSM_CSTATE_ACTIVE),
1887 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1888 {SBIT(GSM_CSTATE_ACTIVE),
1889 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1890 {SBIT(GSM_CSTATE_ACTIVE),
1891 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1892 {SBIT(GSM_CSTATE_ACTIVE),
1893 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1894 {SBIT(GSM_CSTATE_ACTIVE),
1895 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1896 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1897 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1898 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1899 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1900 {SBIT(GSM_CSTATE_ACTIVE),
1901 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1902 /* clearing */
1903 {SBIT(GSM_CSTATE_INITIATED),
1904 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1905 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1906 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1907 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1908 MNCC_REL_REQ, gsm48_cc_tx_release},
1909};
1910
1911#define DOWNSLLEN \
1912 (sizeof(downstatelist) / sizeof(struct downstate))
1913
1914
Philipp Maiercd64af72019-08-01 09:46:40 +02001915static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001916{
1917 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001918 struct msc_a *msc_a = NULL;
1919 struct gsm_trans *trans = NULL;
1920 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001921
Harald Welte27989d42018-06-21 20:39:20 +02001922 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001923 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001924 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001925 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001926 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001927 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001928 return rc;
1929 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001930 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001931 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001932 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001933 case MNCC_RTP_FREE:
1934 /* unused right now */
1935 return -EIO;
1936
1937 case MNCC_FRAME_DROP:
1938 case MNCC_FRAME_RECV:
1939 case GSM_TCHF_FRAME:
1940 case GSM_TCHF_FRAME_EFR:
1941 case GSM_TCHH_FRAME:
1942 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001943 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001944 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001945 return -ENOTSUP;
1946 }
1947
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001948 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001949
1950 /* Find callref */
1951 trans = trans_find_by_callref(net, data->callref);
1952
1953 /* Callref unknown */
1954 if (!trans) {
1955 struct vlr_subscr *vsub;
1956
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001957 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001958 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001959 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001960 /* Invalid call reference */
1961 return mncc_release_ind(net, NULL, data->callref,
1962 GSM48_CAUSE_LOC_PRN_S_LU,
1963 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1964 }
1965 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001966 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001967 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001968 /* Invalid number */
1969 return mncc_release_ind(net, NULL, data->callref,
1970 GSM48_CAUSE_LOC_PRN_S_LU,
1971 GSM48_CC_CAUSE_INV_NR_FORMAT);
1972 }
1973 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001974 if (data->called.number[0]) {
1975 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1976 if (!vsub)
1977 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001978 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001979 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001980 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001981 if (!vsub)
1982 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001983 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001984 }
1985 if (!vsub)
1986 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001987 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001988 /* update the subscriber we deal with */
1989 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1990
Harald Welte27989d42018-06-21 20:39:20 +02001991 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001992 if (!vsub->lu_complete) {
1993 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001994 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001995 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001996 /* Temporarily out of order */
1997 return mncc_release_ind(net, NULL, data->callref,
1998 GSM48_CAUSE_LOC_PRN_S_LU,
1999 GSM48_CC_CAUSE_DEST_OOO);
2000 }
Keith Whyte991bb422019-08-08 15:43:40 +02002001
2002 /* Find valid conn */
2003 msc_a = msc_a_for_vsub(vsub, true);
2004
2005 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2006 if (!net->call_waiting && msc_a) {
2007 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2008 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2009 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2010 "rx '%s' for subscriber %s with trans state (%s)"
2011 " rejecting with USER_BUSY\n",
2012 get_mncc_name(msg->msg_type), data->called.number,
2013 gsm48_cc_state_name(existing_cc_trans->cc.state));
2014 return mncc_release_ind(net, NULL, data->callref,
2015 GSM48_CAUSE_LOC_PRN_S_LU,
2016 GSM48_CC_CAUSE_USER_BUSY);
2017 }
2018 }
2019
Harald Welte27989d42018-06-21 20:39:20 +02002020 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002021 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002022 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002023 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002024 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002025 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002026 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002027 mncc_release_ind(net, NULL, data->callref,
2028 GSM48_CAUSE_LOC_PRN_S_LU,
2029 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2030 return -ENOMEM;
2031 }
2032
Harald Welte27989d42018-06-21 20:39:20 +02002033 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002034 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002035 /* This condition will return before the common logging of the received MNCC message below, so
2036 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002037 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002038
Harald Welte27989d42018-06-21 20:39:20 +02002039 /* store setup information until paging succeeds */
2040 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2041
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002042 /* Request a channel. If Paging already started, paging_request_start() will append the new
2043 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002044 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2045 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002046 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002047 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002048 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002049 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002050 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002051 return 0;
2052 }
2053
2054 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002055 trans->msc_a = msc_a;
2056 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002057 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002058 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002059 } else {
2060 /* update the subscriber we deal with */
2061 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2062 }
2063
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002064 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002065
Philipp Maier9ca7b312018-10-10 17:00:49 +02002066 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002067 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002068
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002069 if (trans->msc_a)
2070 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002071
2072 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002073 if (!msc_a) {
2074 struct gsm_mncc rel = {
2075 .callref = data->callref,
2076 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002077 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002078 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2079 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002080 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002081 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2082 else
2083 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2084 trans->callref = 0;
2085 trans_free(trans);
2086 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002087 }
2088
2089 /* Find function for current state and message */
2090 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002091 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002092 && ((1 << trans->cc.state) & downstatelist[i].states))
2093 break;
2094 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002095 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002096 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002097 return 0;
2098 }
2099
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002100 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002101
2102 return rc;
2103}
2104
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002105struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2106{
2107 uint32_t callref;
2108
2109 switch (msg->msg_type) {
2110 case MNCC_BRIDGE:
2111 callref = msg->bridge.callref[0];
2112 break;
2113 case MNCC_RTP_CREATE:
2114 case MNCC_RTP_CONNECT:
2115 callref = msg->rtp.callref;
2116 break;
2117
2118 case MNCC_RTP_FREE:
2119 case MNCC_FRAME_DROP:
2120 case MNCC_FRAME_RECV:
2121 case GSM_TCHF_FRAME:
2122 case GSM_TCHF_FRAME_EFR:
2123 case GSM_TCHH_FRAME:
2124 case GSM_TCH_FRAME_AMR:
2125 return NULL;
2126
2127 default:
2128 callref = msg->signal.callref;
2129 break;
2130 }
2131
2132 return mncc_call_find_by_callref(callref);
2133}
2134
2135/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002136int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002137{
2138 const union mncc_msg *msg = arg;
2139 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002140
2141 if (msg->msg_type == MNCC_SETUP_REQ) {
2142 /* Incoming call to forward for inter-MSC Handover? */
2143 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2144 if (mncc_call)
2145 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2146 "Incoming call matches pending inter-MSC Handover Number\n");
2147 }
2148 if (!mncc_call) {
2149 /* Find already active MNCC FSM for this callref.
2150 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2151 * MNCC<->GSM-CC call handling. */
2152 mncc_call = mncc_find_by_callref_from_msg(msg);
2153 }
2154 if (mncc_call) {
2155 mncc_call_rx(mncc_call, msg);
2156 return 0;
2157 }
2158
2159 /* None of the above? Then it must be a normal GSM CC call related message. */
2160 return mncc_tx_to_gsm_cc(net, msg);
2161}
Harald Welte27989d42018-06-21 20:39:20 +02002162
2163static struct datastate {
2164 uint32_t states;
2165 int type;
2166 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2167} datastatelist[] = {
2168 /* mobile originating call establishment */
2169 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2170 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2171 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2172 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2173 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2174 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2175 /* mobile terminating call establishment */
2176 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2177 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2178 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2179 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2180 {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 */
2181 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2182 /* signalling during call */
2183 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2184 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2185 {SBIT(GSM_CSTATE_ACTIVE),
2186 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2187 {ALL_STATES,
2188 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2189 {ALL_STATES,
2190 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2191 {ALL_STATES,
2192 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2193 {SBIT(GSM_CSTATE_ACTIVE),
2194 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2195 {SBIT(GSM_CSTATE_ACTIVE),
2196 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2197 {SBIT(GSM_CSTATE_ACTIVE),
2198 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2199 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2200 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2201 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2202 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2203 {SBIT(GSM_CSTATE_ACTIVE),
2204 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2205 /* clearing */
2206 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2207 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2208 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2209 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2210 {ALL_STATES, /* 5.4.3.4 */
2211 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2212};
2213
2214#define DATASLLEN \
2215 (sizeof(datastatelist) / sizeof(struct datastate))
2216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002217int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002218{
2219 struct gsm48_hdr *gh = msgb_l3(msg);
2220 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2221 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2222 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002223 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2224 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002225 int i, rc = 0;
2226
2227 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002228 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002229 return -EINVAL;
2230 }
2231
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002232 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002233 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002234 return -EINVAL;
2235 }
2236
2237 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002238 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002239
Harald Welte27989d42018-06-21 20:39:20 +02002240 /* Create transaction */
2241 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002242 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002243 trans = trans_alloc(net, vsub,
2244 TRANS_CC,
2245 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002246 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002247 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002248 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002249 GSM48_PDISC_CC | (transaction_id << 4),
2250 GSM48_MT_CC_RELEASE_COMPL);
2251 return -ENOMEM;
2252 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002253 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2254 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2255 trans_free(trans);
2256 return -EINVAL;
2257 }
2258
Harald Welte27989d42018-06-21 20:39:20 +02002259 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002260 msc_a_get(msc_a, MSC_A_USE_CC);
2261 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002262 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002263
2264 /* An earlier CM Service Request for this CC message now has concluded */
2265 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2266 LOG_MSC_A(msc_a, LOGL_ERROR,
2267 "Creating new CC transaction without prior CM Service Request\n");
2268 else
2269 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002270 }
2271
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002272 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2273 gsm48_cc_state_name(trans->cc.state));
2274
Harald Welte27989d42018-06-21 20:39:20 +02002275 /* find function for current state and message */
2276 for (i = 0; i < DATASLLEN; i++)
2277 if ((msg_type == datastatelist[i].type)
2278 && ((1 << trans->cc.state) & datastatelist[i].states))
2279 break;
2280 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002281 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002282
2283 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2284 * transaction right away. */
2285 if (trans->cc.state == GSM_CSTATE_NULL) {
2286 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2287 " -- disarding new CC transaction right away\n");
2288 trans_free(trans);
2289 }
Harald Welte27989d42018-06-21 20:39:20 +02002290 return 0;
2291 }
2292
2293 assert(trans->vsub);
2294
2295 rc = datastatelist[i].rout(trans, msg);
2296
Harald Welte27989d42018-06-21 20:39:20 +02002297 return rc;
2298}