blob: 965847e762b931184972366703aef803aae30e7b [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 Hofmeyr9a539f32022-01-13 21:39:11 +010058#include <osmocom/msc/codec_sdp_cc_t9n.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
232static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
233 int msg_type, struct gsm_mncc *mncc)
234{
235 struct msgb *msg;
236 unsigned char *data;
237
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100238 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200239
240 mncc->msg_type = msg_type;
241
242 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
243 if (!msg)
244 return -ENOMEM;
245
246 data = msgb_put(msg, sizeof(struct gsm_mncc));
247 memcpy(data, mncc, sizeof(struct gsm_mncc));
248
249 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200250 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
251 if (trans)
252 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200253
254 return 0;
255}
256
257int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
258 uint32_t callref, int location, int value)
259{
260 struct gsm_mncc rel;
261
262 memset(&rel, 0, sizeof(rel));
263 rel.callref = callref;
264 mncc_set_cause(&rel, location, value);
265 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
266 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
267 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
268}
269
270/* Call Control Specific transaction release.
271 * gets called by trans_free, DO NOT CALL YOURSELF! */
272void _gsm48_cc_trans_free(struct gsm_trans *trans)
273{
274 gsm48_stop_cc_timer(trans);
275
Harald Welte27989d42018-06-21 20:39:20 +0200276 /* send release to L4, if callref still exists */
277 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100278 /* Send MNCC REL.ind (cause='Resource unavailable') */
279 if (trans->cc.mncc_initiated) {
280 mncc_release_ind(trans->net, trans, trans->callref,
281 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100282 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
283 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100284 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
285 }
286
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100287 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
288 * CC Release to the MS if it gets freed here. Hack it to do so. */
289 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
290 struct gsm_mncc rel = {};
291 rel.callref = trans->callref;
292 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
293 gsm48_cc_tx_release(trans, &rel);
294 }
Harald Welte27989d42018-06-21 20:39:20 +0200295 /* This is a final freeing of the transaction. The MNCC release may have triggered the
296 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
297 gsm48_stop_cc_timer(trans);
298 }
299 if (trans->cc.state != GSM_CSTATE_NULL)
300 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200301
302 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100303
304 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
305 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200306}
307
Harald Welte27989d42018-06-21 20:39:20 +0200308/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100309static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200310{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100311 if (trans->msc_a) {
312 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
313 "Handle paging error: transaction already associated with subscriber,"
314 " apparently it was already handled. Skip.\n");
315 return;
Harald Welte27989d42018-06-21 20:39:20 +0200316 }
317
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100318 if (msc_a) {
319 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
320 /* Assign conn */
321 msc_a_get(msc_a, MSC_A_USE_CC);
322 trans->msc_a = msc_a;
323 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200324
325 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300326 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200327 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300328 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
329 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
330 &trans->cc.msg.gcr[0],
331 sizeof(trans->cc.msg.gcr));
332 if (rc < 0)
333 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
334 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200335 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200336 }
337
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100338 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
339 /* send SETUP request to called party */
340 gsm48_cc_tx_setup(trans, &trans->cc.msg);
341 } else {
342 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
343 /* Temporarily out of order */
344 mncc_release_ind(trans->net, trans,
345 trans->callref,
346 GSM48_CAUSE_LOC_PRN_S_LU,
347 GSM48_CC_CAUSE_DEST_OOO);
348 trans->callref = 0;
349 trans->paging_request = NULL;
350 trans_free(trans);
351 }
Harald Welte27989d42018-06-21 20:39:20 +0200352}
353
354/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100355static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200356{
357 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
358 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359 struct call_leg *cl1;
360 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200361
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100362 if (!trans1 || !trans2) {
363 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200364 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100365 }
Harald Welte27989d42018-06-21 20:39:20 +0200366
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100367 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100368 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
369 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 +0200370 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100371 }
372
373 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
374 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200375
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100376 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
377 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
378 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
379 * If we can't, then we must give up. */
380 cl1 = trans1->msc_a->cc.call_leg;
381 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200382
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100383 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200384}
385
386static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
387{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100388 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200389 return gsm48_cc_tx_status(trans, msg);
390}
391
Harald Welte27989d42018-06-21 20:39:20 +0200392static void gsm48_cc_timeout(void *arg)
393{
394 struct gsm_trans *trans = arg;
395 int disconnect = 0, release = 0;
396 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
397 int mo_location = GSM48_CAUSE_LOC_USER;
398 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
399 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
400 struct gsm_mncc mo_rel, l4_rel;
401
Neels Hofmeyrac7d9a62022-08-06 14:16:55 +0200402 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
403
Harald Welte27989d42018-06-21 20:39:20 +0200404 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
405 mo_rel.callref = trans->callref;
406 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
407 l4_rel.callref = trans->callref;
408
409 switch(trans->cc.Tcurrent) {
410 case 0x303:
411 release = 1;
412 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
413 break;
414 case 0x310:
415 disconnect = 1;
416 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
417 break;
418 case 0x313:
419 disconnect = 1;
420 /* unknown, did not find it in the specs */
421 break;
422 case 0x301:
423 disconnect = 1;
424 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
425 break;
426 case 0x308:
427 if (!trans->cc.T308_second) {
428 /* restart T308 a second time */
429 gsm48_cc_tx_release(trans, &trans->cc.msg);
430 trans->cc.T308_second = 1;
431 break; /* stay in release state */
432 }
433 trans_free(trans);
434 return;
435 case 0x306:
436 release = 1;
437 mo_cause = trans->cc.msg.cause.value;
438 mo_location = trans->cc.msg.cause.location;
439 break;
440 case 0x323:
441 disconnect = 1;
442 break;
443 default:
444 release = 1;
445 }
446
447 if (release && trans->callref) {
448 /* process release towards layer 4 */
449 mncc_release_ind(trans->net, trans, trans->callref,
450 l4_location, l4_cause);
451 trans->callref = 0;
452 }
453
454 if (disconnect && trans->callref) {
455 /* process disconnect towards layer 4 */
456 mncc_set_cause(&l4_rel, l4_location, l4_cause);
457 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
458 }
459
460 /* process disconnect towards mobile station */
461 if (disconnect || release) {
462 mncc_set_cause(&mo_rel, mo_location, mo_cause);
463 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
464 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
465 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
466 mo_rel.cause.diag_len = 3;
467
468 if (disconnect)
469 gsm48_cc_tx_disconnect(trans, &mo_rel);
470 if (release)
471 gsm48_cc_tx_release(trans, &mo_rel);
472 }
473
474}
475
476/* disconnect both calls from the bridge */
477static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100478 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200479{
480 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
481 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
482 struct gsm_mncc mx_rel;
483 if (!trans0 || !trans1)
484 return;
485
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100486 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
487 trans0->callref, trans1->callref, strerror(err));
488 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200489 trans0->callref, trans1->callref, strerror(err));
490
491 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
492 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
493 GSM48_CC_CAUSE_CHAN_UNACCEPT);
494
495 mx_rel.callref = trans0->callref;
496 gsm48_cc_tx_disconnect(trans0, &mx_rel);
497
498 mx_rel.callref = trans1->callref;
499 gsm48_cc_tx_disconnect(trans1, &mx_rel);
500}
501
502static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
503 int sec, int micro)
504{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100505 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200506 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
507 osmo_timer_schedule(&trans->cc.timer, sec, micro);
508 trans->cc.Tcurrent = current;
509}
510
511static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
512{
513 struct gsm48_hdr *gh = msgb_l3(msg);
514 uint8_t msg_type = gsm48_hdr_msg_type(gh);
515 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
516 struct tlv_parsed tp;
517 struct gsm_mncc setup;
518
Philipp Maier9ca7b312018-10-10 17:00:49 +0200519 gsm48_start_guard_timer(trans);
520
Harald Welte27989d42018-06-21 20:39:20 +0200521 memset(&setup, 0, sizeof(struct gsm_mncc));
522 setup.callref = trans->callref;
523
Keith Whytea1a70be2021-05-16 02:59:52 +0200524 /* New Global Call Reference */
525 if (!trans->cc.lcls)
526 trans->cc.lcls = trans_lcls_compose(trans, true);
527
528 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300529 if (trans->cc.lcls) {
530 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
531 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
532 int rc;
533
534 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
535 memcpy(&setup.gcr[0], gcr_msg->data, rc);
536 setup.fields |= MNCC_F_GCR;
537 } else
538 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
539 msgb_free(gcr_msg);
540 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200541
Harald Welte27989d42018-06-21 20:39:20 +0200542 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
543 /* emergency setup is identified by msg_type */
544 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
545 setup.fields |= MNCC_F_EMERGENCY;
546 setup.emergency = 1;
547 /* use destination number as configured by user (if any) */
548 if (trans->net->emergency.route_to_msisdn) {
549 setup.fields |= MNCC_F_CALLED;
550 setup.called.type = 0; /* unknown */
551 setup.called.plan = 0; /* unknown */
552 OSMO_STRLCPY_ARRAY(setup.called.number,
553 trans->net->emergency.route_to_msisdn);
554 }
555 }
556
557 /* use subscriber as calling party number */
558 setup.fields |= MNCC_F_CALLING;
559 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
560 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
561
562 /* bearer capability */
563 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
564 setup.fields |= MNCC_F_BEARER_CAP;
565 gsm48_decode_bearer_cap(&setup.bearer_cap,
566 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
567
568 /* Create a copy of the bearer capability
569 * in the transaction struct, so we can use
570 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100571 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200572 sizeof(trans->bearer_cap));
573 }
574 /* facility */
575 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
576 setup.fields |= MNCC_F_FACILITY;
577 gsm48_decode_facility(&setup.facility,
578 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
579 }
580 /* called party bcd number */
581 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
582 setup.fields |= MNCC_F_CALLED;
583 gsm48_decode_called(&setup.called,
584 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
585 }
586 /* user-user */
587 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
588 setup.fields |= MNCC_F_USERUSER;
589 gsm48_decode_useruser(&setup.useruser,
590 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
591 }
592 /* ss-version */
593 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
594 setup.fields |= MNCC_F_SSVERSION;
595 gsm48_decode_ssversion(&setup.ssversion,
596 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
597 }
598 /* CLIR suppression */
599 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
600 setup.clir.sup = 1;
601 /* CLIR invocation */
602 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
603 setup.clir.inv = 1;
604 /* cc cap */
605 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
606 setup.fields |= MNCC_F_CCCAP;
607 gsm48_decode_cccap(&setup.cccap,
608 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
609 }
610
611 new_cc_state(trans, GSM_CSTATE_INITIATED);
612
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100613 /* MO call leg starting, gather all codec information so far known: */
614 codec_filter_init(&trans->cc.codecs);
615 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
616 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
617 if (setup.fields & MNCC_F_BEARER_CAP)
618 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
619 codec_filter_run(&trans->cc.codecs);
620
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100621 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
622 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100623 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200624
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200625 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200626
627 /* indicate setup to MNCC */
628 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
629
630 /* MNCC code will modify the channel asynchronously, we should
631 * ipaccess-bind only after the modification has been made to the
632 * lchan->tch_mode */
633 return 0;
634}
635
636static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
637{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100638 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200639 struct gsm48_hdr *gh;
640 struct gsm_mncc *setup = arg;
641 int rc, trans_id;
642
643 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
644
645 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700646 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100647 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200648 "This is not allowed!\n");
649 /* Temporarily out of order */
650 rc = mncc_release_ind(trans->net, trans, trans->callref,
651 GSM48_CAUSE_LOC_PRN_S_LU,
652 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
653 trans->callref = 0;
654 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200655 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200656 return rc;
657 }
658
659 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100660 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200661 if (trans_id < 0) {
662 /* no free transaction ID */
663 rc = mncc_release_ind(trans->net, trans, trans->callref,
664 GSM48_CAUSE_LOC_PRN_S_LU,
665 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
666 trans->callref = 0;
667 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200668 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200669 return rc;
670 }
671 trans->transaction_id = trans_id;
672
673 gh->msg_type = GSM48_MT_CC_SETUP;
674
675 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
676
Neels Hofmeyra4195db2022-01-13 21:40:58 +0100677 /* MT call leg is starting. Gather all codecs information so far known.
678 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
679 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
680 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
681 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
682 * Capabilities. */
683 codec_filter_init(&trans->cc.codecs);
684 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
685 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
686 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
687 if (setup->sdp[0]) {
688 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
689 if (rc)
690 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
691 }
692 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
693 * MNCC, if any. */
694 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
695 trans->cc.codecs.remote = (struct sdp_msg){};
696 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
697 &setup->bearer_cap);
698 }
699 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
700 if (!trans->cc.codecs.remote.audio_codecs.count)
701 LOG_TRANS(trans, LOGL_ERROR,
702 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
703
Harald Welte27989d42018-06-21 20:39:20 +0200704 /* bearer capability */
705 if (setup->fields & MNCC_F_BEARER_CAP) {
706 /* Create a copy of the bearer capability in the transaction struct, so we
707 * can use this information later */
708 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
709 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
710 }
711 /* facility */
712 if (setup->fields & MNCC_F_FACILITY)
713 gsm48_encode_facility(msg, 0, &setup->facility);
714 /* progress */
715 if (setup->fields & MNCC_F_PROGRESS)
716 gsm48_encode_progress(msg, 0, &setup->progress);
717 /* calling party BCD number */
718 if (setup->fields & MNCC_F_CALLING)
719 gsm48_encode_calling(msg, &setup->calling);
720 /* called party BCD number */
721 if (setup->fields & MNCC_F_CALLED)
722 gsm48_encode_called(msg, &setup->called);
723 /* user-user */
724 if (setup->fields & MNCC_F_USERUSER)
725 gsm48_encode_useruser(msg, 0, &setup->useruser);
726 /* redirecting party BCD number */
727 if (setup->fields & MNCC_F_REDIRECTING)
728 gsm48_encode_redirecting(msg, &setup->redirecting);
729 /* signal */
730 if (setup->fields & MNCC_F_SIGNAL)
731 gsm48_encode_signal(msg, setup->signal);
732
733 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
734
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200735 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200736
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100737 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200738}
739
740static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
741{
742 struct gsm48_hdr *gh = msgb_l3(msg);
743 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
744 struct tlv_parsed tp;
745 struct gsm_mncc call_conf;
746 int rc;
747
748 gsm48_stop_cc_timer(trans);
749 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
750
751 memset(&call_conf, 0, sizeof(struct gsm_mncc));
752 call_conf.callref = trans->callref;
753
754 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
755#if 0
756 /* repeat */
757 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
758 call_conf.repeat = 1;
759 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
760 call_conf.repeat = 2;
761#endif
762 /* bearer capability */
763 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
764 call_conf.fields |= MNCC_F_BEARER_CAP;
765 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
766 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
767
768 /* Create a copy of the bearer capability
769 * in the transaction struct, so we can use
770 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100771 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200772 sizeof(trans->bearer_cap));
Neels Hofmeyrdb61f732022-01-13 19:59:02 +0100773
774 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
775 * Store in codecs filter. */
776 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200777 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100778
Harald Welte27989d42018-06-21 20:39:20 +0200779 /* cause */
780 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
781 call_conf.fields |= MNCC_F_CAUSE;
782 gsm48_decode_cause(&call_conf.cause,
783 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
784 }
785 /* cc cap */
786 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
787 call_conf.fields |= MNCC_F_CCCAP;
788 gsm48_decode_cccap(&call_conf.cccap,
789 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
790 }
791
792 /* IMSI of called subscriber */
793 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
794
795 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
796
797 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100798 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200799
800 /* don't continue, if there were problems with
801 * the call assignment. */
802 if (rc)
803 return rc;
804
805 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
806 &call_conf);
807}
808
809static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
810{
811 struct gsm_mncc *proceeding = arg;
812 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
813 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
814 int rc;
815
816 gh->msg_type = GSM48_MT_CC_CALL_PROC;
817
818 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
819
820 /* bearer capability */
821 if (proceeding->fields & MNCC_F_BEARER_CAP) {
822 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
823 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
824 }
825 /* facility */
826 if (proceeding->fields & MNCC_F_FACILITY)
827 gsm48_encode_facility(msg, 0, &proceeding->facility);
828 /* progress */
829 if (proceeding->fields & MNCC_F_PROGRESS)
830 gsm48_encode_progress(msg, 0, &proceeding->progress);
831
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100832 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200833 if (rc)
834 return rc;
835
836 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100837 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200838}
839
840static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
841{
842 struct gsm48_hdr *gh = msgb_l3(msg);
843 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
844 struct tlv_parsed tp;
845 struct gsm_mncc alerting;
846
847 gsm48_stop_cc_timer(trans);
848 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
849
850 memset(&alerting, 0, sizeof(struct gsm_mncc));
851 alerting.callref = trans->callref;
852 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
853 /* facility */
854 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
855 alerting.fields |= MNCC_F_FACILITY;
856 gsm48_decode_facility(&alerting.facility,
857 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
858 }
859
860 /* progress */
861 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
862 alerting.fields |= MNCC_F_PROGRESS;
863 gsm48_decode_progress(&alerting.progress,
864 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
865 }
866 /* ss-version */
867 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
868 alerting.fields |= MNCC_F_SSVERSION;
869 gsm48_decode_ssversion(&alerting.ssversion,
870 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
871 }
872
873 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
874
875 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
876 &alerting);
877}
878
879static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
880{
881 struct gsm_mncc *alerting = arg;
882 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
883 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
884
885 gh->msg_type = GSM48_MT_CC_ALERTING;
886
887 /* facility */
888 if (alerting->fields & MNCC_F_FACILITY)
889 gsm48_encode_facility(msg, 0, &alerting->facility);
890 /* progress */
891 if (alerting->fields & MNCC_F_PROGRESS)
892 gsm48_encode_progress(msg, 0, &alerting->progress);
893 /* user-user */
894 if (alerting->fields & MNCC_F_USERUSER)
895 gsm48_encode_useruser(msg, 0, &alerting->useruser);
896
897 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
898
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100899 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200900}
901
902static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
903{
904 struct gsm_mncc *progress = arg;
905 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
906 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
907
908 gh->msg_type = GSM48_MT_CC_PROGRESS;
909
910 /* progress */
911 gsm48_encode_progress(msg, 1, &progress->progress);
912 /* user-user */
913 if (progress->fields & MNCC_F_USERUSER)
914 gsm48_encode_useruser(msg, 0, &progress->useruser);
915
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100916 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200917}
918
919static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
920{
921 struct gsm_mncc *connect = arg;
922 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
923 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
924
925 gh->msg_type = GSM48_MT_CC_CONNECT;
926
927 gsm48_stop_cc_timer(trans);
928 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
929
930 /* facility */
931 if (connect->fields & MNCC_F_FACILITY)
932 gsm48_encode_facility(msg, 0, &connect->facility);
933 /* progress */
934 if (connect->fields & MNCC_F_PROGRESS)
935 gsm48_encode_progress(msg, 0, &connect->progress);
936 /* connected number */
937 if (connect->fields & MNCC_F_CONNECTED)
938 gsm48_encode_connected(msg, &connect->connected);
939 /* user-user */
940 if (connect->fields & MNCC_F_USERUSER)
941 gsm48_encode_useruser(msg, 0, &connect->useruser);
942
943 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
944
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100945 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200946}
947
948static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
949{
950 struct gsm48_hdr *gh = msgb_l3(msg);
951 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
952 struct tlv_parsed tp;
953 struct gsm_mncc connect;
954
955 gsm48_stop_cc_timer(trans);
956
957 memset(&connect, 0, sizeof(struct gsm_mncc));
958 connect.callref = trans->callref;
959 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
960 /* use subscriber as connected party number */
961 connect.fields |= MNCC_F_CONNECTED;
962 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
963 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
964
965 /* facility */
966 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
967 connect.fields |= MNCC_F_FACILITY;
968 gsm48_decode_facility(&connect.facility,
969 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
970 }
971 /* user-user */
972 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
973 connect.fields |= MNCC_F_USERUSER;
974 gsm48_decode_useruser(&connect.useruser,
975 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
976 }
977 /* ss-version */
978 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
979 connect.fields |= MNCC_F_SSVERSION;
980 gsm48_decode_ssversion(&connect.ssversion,
981 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
982 }
983
984 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200985 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200986
987 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
988}
989
990
991static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
992{
993 struct gsm_mncc connect_ack;
994
995 gsm48_stop_cc_timer(trans);
996
997 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200998 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 +0200999
1000 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1001 connect_ack.callref = trans->callref;
1002
1003 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1004 &connect_ack);
1005}
1006
1007static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1008{
1009 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1010 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1011
1012 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1013
1014 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1015
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001016 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001017}
1018
1019static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1020{
1021 struct gsm48_hdr *gh = msgb_l3(msg);
1022 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1023 struct tlv_parsed tp;
1024 struct gsm_mncc disc;
1025
1026 gsm48_stop_cc_timer(trans);
1027
1028 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1029
1030 memset(&disc, 0, sizeof(struct gsm_mncc));
1031 disc.callref = trans->callref;
1032 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1033 /* cause */
1034 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1035 disc.fields |= MNCC_F_CAUSE;
1036 gsm48_decode_cause(&disc.cause,
1037 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1038 }
1039 /* facility */
1040 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1041 disc.fields |= MNCC_F_FACILITY;
1042 gsm48_decode_facility(&disc.facility,
1043 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1044 }
1045 /* user-user */
1046 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1047 disc.fields |= MNCC_F_USERUSER;
1048 gsm48_decode_useruser(&disc.useruser,
1049 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1050 }
1051 /* ss-version */
1052 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1053 disc.fields |= MNCC_F_SSVERSION;
1054 gsm48_decode_ssversion(&disc.ssversion,
1055 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1056 }
1057
1058 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001059}
1060
1061static struct gsm_mncc_cause default_cause = {
1062 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1063 .coding = 0,
1064 .rec = 0,
1065 .rec_val = 0,
1066 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1067 .diag_len = 0,
1068 .diag = { 0 },
1069};
1070
1071static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1072{
1073 struct gsm_mncc *disc = arg;
1074 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1075 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1076
1077 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1078
1079 gsm48_stop_cc_timer(trans);
1080 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1081
1082 /* cause */
1083 if (disc->fields & MNCC_F_CAUSE)
1084 gsm48_encode_cause(msg, 1, &disc->cause);
1085 else
1086 gsm48_encode_cause(msg, 1, &default_cause);
1087
1088 /* facility */
1089 if (disc->fields & MNCC_F_FACILITY)
1090 gsm48_encode_facility(msg, 0, &disc->facility);
1091 /* progress */
1092 if (disc->fields & MNCC_F_PROGRESS)
1093 gsm48_encode_progress(msg, 0, &disc->progress);
1094 /* user-user */
1095 if (disc->fields & MNCC_F_USERUSER)
1096 gsm48_encode_useruser(msg, 0, &disc->useruser);
1097
1098 /* store disconnect cause for T306 expiry */
1099 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1100
1101 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1102
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001103 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001104}
1105
1106static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1107{
1108 struct gsm48_hdr *gh = msgb_l3(msg);
1109 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1110 struct tlv_parsed tp;
1111 struct gsm_mncc rel;
1112 int rc;
1113
1114 gsm48_stop_cc_timer(trans);
1115
1116 memset(&rel, 0, sizeof(struct gsm_mncc));
1117 rel.callref = trans->callref;
1118 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1119 /* cause */
1120 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1121 rel.fields |= MNCC_F_CAUSE;
1122 gsm48_decode_cause(&rel.cause,
1123 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1124 }
1125 /* facility */
1126 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1127 rel.fields |= MNCC_F_FACILITY;
1128 gsm48_decode_facility(&rel.facility,
1129 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1130 }
1131 /* user-user */
1132 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1133 rel.fields |= MNCC_F_USERUSER;
1134 gsm48_decode_useruser(&rel.useruser,
1135 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1136 }
1137 /* ss-version */
1138 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1139 rel.fields |= MNCC_F_SSVERSION;
1140 gsm48_decode_ssversion(&rel.ssversion,
1141 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1142 }
1143
1144 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1145 /* release collision 5.4.5 */
1146 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1147 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001148 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001149 GSM48_PDISC_CC | (trans->transaction_id << 4),
1150 GSM48_MT_CC_RELEASE_COMPL);
1151 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1152 }
1153
1154 new_cc_state(trans, GSM_CSTATE_NULL);
1155
1156 trans->callref = 0;
1157 trans_free(trans);
1158
1159 return rc;
1160}
1161
1162static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1163{
1164 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001165 struct msgb *msg;
1166 struct gsm48_hdr *gh;
1167
1168 if (!trans->msc_a) {
1169 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1170 return -EINVAL;
1171 }
1172
1173 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1174 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001175
1176 gh->msg_type = GSM48_MT_CC_RELEASE;
1177
1178 gsm48_stop_cc_timer(trans);
1179 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1180
1181 /* cause */
1182 if (rel->fields & MNCC_F_CAUSE)
1183 gsm48_encode_cause(msg, 0, &rel->cause);
1184 /* facility */
1185 if (rel->fields & MNCC_F_FACILITY)
1186 gsm48_encode_facility(msg, 0, &rel->facility);
1187 /* user-user */
1188 if (rel->fields & MNCC_F_USERUSER)
1189 gsm48_encode_useruser(msg, 0, &rel->useruser);
1190
1191 trans->cc.T308_second = 0;
1192 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1193
1194 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1195 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1196
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001197 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001198}
1199
1200static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1201{
1202 struct gsm48_hdr *gh = msgb_l3(msg);
1203 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1204 struct tlv_parsed tp;
1205 struct gsm_mncc rel;
1206 int rc = 0;
1207
1208 gsm48_stop_cc_timer(trans);
1209
1210 memset(&rel, 0, sizeof(struct gsm_mncc));
1211 rel.callref = trans->callref;
1212 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1213 /* cause */
1214 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1215 rel.fields |= MNCC_F_CAUSE;
1216 gsm48_decode_cause(&rel.cause,
1217 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1218 }
1219 /* facility */
1220 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1221 rel.fields |= MNCC_F_FACILITY;
1222 gsm48_decode_facility(&rel.facility,
1223 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1224 }
1225 /* user-user */
1226 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1227 rel.fields |= MNCC_F_USERUSER;
1228 gsm48_decode_useruser(&rel.useruser,
1229 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1230 }
1231 /* ss-version */
1232 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1233 rel.fields |= MNCC_F_SSVERSION;
1234 gsm48_decode_ssversion(&rel.ssversion,
1235 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1236 }
1237
1238 if (trans->callref) {
1239 switch (trans->cc.state) {
1240 case GSM_CSTATE_CALL_PRESENT:
1241 rc = mncc_recvmsg(trans->net, trans,
1242 MNCC_REJ_IND, &rel);
1243 break;
1244 case GSM_CSTATE_RELEASE_REQ:
1245 rc = mncc_recvmsg(trans->net, trans,
1246 MNCC_REL_CNF, &rel);
1247 break;
1248 default:
1249 rc = mncc_recvmsg(trans->net, trans,
1250 MNCC_REL_IND, &rel);
1251 }
1252 }
1253
1254 trans->callref = 0;
1255 trans_free(trans);
1256
1257 return rc;
1258}
1259
1260static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1261{
1262 struct gsm_mncc *rel = arg;
1263 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1264 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1265 int ret;
1266
1267 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1268
1269 trans->callref = 0;
1270
1271 gsm48_stop_cc_timer(trans);
1272
1273 /* cause */
1274 if (rel->fields & MNCC_F_CAUSE)
1275 gsm48_encode_cause(msg, 0, &rel->cause);
1276 /* facility */
1277 if (rel->fields & MNCC_F_FACILITY)
1278 gsm48_encode_facility(msg, 0, &rel->facility);
1279 /* user-user */
1280 if (rel->fields & MNCC_F_USERUSER)
1281 gsm48_encode_useruser(msg, 0, &rel->useruser);
1282
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001283 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001284
1285 trans_free(trans);
1286
1287 return ret;
1288}
1289
1290static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1291{
1292 struct gsm48_hdr *gh = msgb_l3(msg);
1293 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1294 struct tlv_parsed tp;
1295 struct gsm_mncc fac;
1296
1297 memset(&fac, 0, sizeof(struct gsm_mncc));
1298 fac.callref = trans->callref;
1299 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1300 /* facility */
1301 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1302 fac.fields |= MNCC_F_FACILITY;
1303 gsm48_decode_facility(&fac.facility,
1304 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1305 }
1306 /* ss-version */
1307 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1308 fac.fields |= MNCC_F_SSVERSION;
1309 gsm48_decode_ssversion(&fac.ssversion,
1310 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1311 }
1312
1313 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1314}
1315
1316static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1317{
1318 struct gsm_mncc *fac = arg;
1319 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1320 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1321
1322 gh->msg_type = GSM48_MT_CC_FACILITY;
1323
1324 /* facility */
1325 gsm48_encode_facility(msg, 1, &fac->facility);
1326
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001327 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001328}
1329
1330static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1331{
1332 struct gsm_mncc hold;
1333
1334 memset(&hold, 0, sizeof(struct gsm_mncc));
1335 hold.callref = trans->callref;
1336 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1337}
1338
1339static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1340{
1341 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1342 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1343
1344 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1345
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001346 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001347}
1348
1349static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1350{
1351 struct gsm_mncc *hold_rej = arg;
1352 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1353 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1354
1355 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1356
1357 /* cause */
1358 if (hold_rej->fields & MNCC_F_CAUSE)
1359 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1360 else
1361 gsm48_encode_cause(msg, 1, &default_cause);
1362
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001363 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001364}
1365
1366static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1367{
1368 struct gsm_mncc retrieve;
1369
1370 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1371 retrieve.callref = trans->callref;
1372 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1373 &retrieve);
1374}
1375
1376static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1377{
1378 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1379 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1380
1381 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1382
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001383 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001384}
1385
1386static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1387{
1388 struct gsm_mncc *retrieve_rej = arg;
1389 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1390 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1391
1392 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1393
1394 /* cause */
1395 if (retrieve_rej->fields & MNCC_F_CAUSE)
1396 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1397 else
1398 gsm48_encode_cause(msg, 1, &default_cause);
1399
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001400 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001401}
1402
1403static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1404{
1405 struct gsm48_hdr *gh = msgb_l3(msg);
1406 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1407 struct tlv_parsed tp;
1408 struct gsm_mncc dtmf;
1409
1410 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1411 dtmf.callref = trans->callref;
1412 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1413 /* keypad facility */
1414 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1415 dtmf.fields |= MNCC_F_KEYPAD;
1416 gsm48_decode_keypad(&dtmf.keypad,
1417 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1418 }
1419
1420 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1421}
1422
1423static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1424{
1425 struct gsm_mncc *dtmf = arg;
1426 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1427 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1428
1429 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1430
1431 /* keypad */
1432 if (dtmf->fields & MNCC_F_KEYPAD)
1433 gsm48_encode_keypad(msg, dtmf->keypad);
1434
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001435 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001436}
1437
1438static int gsm48_cc_tx_start_dtmf_rej(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 REJ");
1442 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1443
1444 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1445
1446 /* cause */
1447 if (dtmf->fields & MNCC_F_CAUSE)
1448 gsm48_encode_cause(msg, 1, &dtmf->cause);
1449 else
1450 gsm48_encode_cause(msg, 1, &default_cause);
1451
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001452 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001453}
1454
1455static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1456{
1457 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1458 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1459
1460 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1461
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001462 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001463}
1464
1465static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1466{
1467 struct gsm_mncc dtmf;
1468
1469 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1470 dtmf.callref = trans->callref;
1471
1472 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1473}
1474
1475static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1476{
1477 struct gsm48_hdr *gh = msgb_l3(msg);
1478 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1479 struct tlv_parsed tp;
1480 struct gsm_mncc modify;
1481
1482 memset(&modify, 0, sizeof(struct gsm_mncc));
1483 modify.callref = trans->callref;
1484 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1485 /* bearer capability */
1486 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1487 modify.fields |= MNCC_F_BEARER_CAP;
1488 gsm48_decode_bearer_cap(&modify.bearer_cap,
1489 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1490
1491 /* Create a copy of the bearer capability
1492 * in the transaction struct, so we can use
1493 * this information later */
1494 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1495 sizeof(trans->bearer_cap));
1496 }
1497
1498 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1499
1500 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1501}
1502
1503static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1504{
1505 struct gsm_mncc *modify = arg;
1506 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1507 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1508
1509 gh->msg_type = GSM48_MT_CC_MODIFY;
1510
1511 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1512
1513 /* bearer capability */
1514 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1515 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1516
1517 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1518
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001519 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001520}
1521
1522static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1523{
1524 struct gsm48_hdr *gh = msgb_l3(msg);
1525 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1526 struct tlv_parsed tp;
1527 struct gsm_mncc modify;
1528
1529 gsm48_stop_cc_timer(trans);
1530
1531 memset(&modify, 0, sizeof(struct gsm_mncc));
1532 modify.callref = trans->callref;
1533 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1534 /* bearer capability */
1535 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1536 modify.fields |= MNCC_F_BEARER_CAP;
1537 gsm48_decode_bearer_cap(&modify.bearer_cap,
1538 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1539
1540 /* Create a copy of the bearer capability
1541 * in the transaction struct, so we can use
1542 * this information later */
1543 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1544 sizeof(trans->bearer_cap));
1545 }
1546
1547 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1548
1549 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1550}
1551
1552static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1553{
1554 struct gsm_mncc *modify = arg;
1555 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1556 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1557
1558 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1559
1560 /* bearer capability */
1561 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1562 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1563
1564 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1565
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001566 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001567}
1568
1569static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1570{
1571 struct gsm48_hdr *gh = msgb_l3(msg);
1572 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1573 struct tlv_parsed tp;
1574 struct gsm_mncc modify;
1575
1576 gsm48_stop_cc_timer(trans);
1577
1578 memset(&modify, 0, sizeof(struct gsm_mncc));
1579 modify.callref = trans->callref;
1580 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1581 /* bearer capability */
1582 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1583 modify.fields |= GSM48_IE_BEARER_CAP;
1584 gsm48_decode_bearer_cap(&modify.bearer_cap,
1585 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1586
1587 /* Create a copy of the bearer capability
1588 * in the transaction struct, so we can use
1589 * this information later */
1590 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1591 sizeof(trans->bearer_cap));
1592 }
1593 /* cause */
1594 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1595 modify.fields |= MNCC_F_CAUSE;
1596 gsm48_decode_cause(&modify.cause,
1597 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1598 }
1599
1600 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1601
1602 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1603}
1604
1605static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1606{
1607 struct gsm_mncc *modify = arg;
1608 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1609 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1610
1611 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1612
1613 /* bearer capability */
1614 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1615 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1616 /* cause */
1617 gsm48_encode_cause(msg, 1, &modify->cause);
1618
1619 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1620
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001621 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001622}
1623
1624static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1625{
1626 struct gsm_mncc *notify = arg;
1627 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1628 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1629
1630 gh->msg_type = GSM48_MT_CC_NOTIFY;
1631
1632 /* notify */
1633 gsm48_encode_notify(msg, notify->notify);
1634
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001635 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001636}
1637
1638static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1639{
1640 struct gsm48_hdr *gh = msgb_l3(msg);
1641 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1642// struct tlv_parsed tp;
1643 struct gsm_mncc notify;
1644
1645 memset(&notify, 0, sizeof(struct gsm_mncc));
1646 notify.callref = trans->callref;
1647// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1648 if (payload_len >= 1)
1649 gsm48_decode_notify(&notify.notify, gh->data);
1650
1651 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1652}
1653
1654static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1655{
1656 struct gsm_mncc *user = arg;
1657 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1658 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1659
1660 gh->msg_type = GSM48_MT_CC_USER_INFO;
1661
1662 /* user-user */
1663 if (user->fields & MNCC_F_USERUSER)
1664 gsm48_encode_useruser(msg, 1, &user->useruser);
1665 /* more data */
1666 if (user->more)
1667 gsm48_encode_more(msg);
1668
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001669 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001670}
1671
1672static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1673{
1674 struct gsm48_hdr *gh = msgb_l3(msg);
1675 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1676 struct tlv_parsed tp;
1677 struct gsm_mncc user;
1678
1679 memset(&user, 0, sizeof(struct gsm_mncc));
1680 user.callref = trans->callref;
1681 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1682 /* user-user */
1683 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1684 user.fields |= MNCC_F_USERUSER;
1685 gsm48_decode_useruser(&user.useruser,
1686 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1687 }
1688 /* more data */
1689 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1690 user.more = 1;
1691
1692 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1693}
1694
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001695static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1696 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1697 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001698{
1699 uint8_t data[sizeof(struct gsm_mncc)];
1700 struct gsm_mncc_rtp *rtp;
1701
1702 memset(&data, 0, sizeof(data));
1703 rtp = (struct gsm_mncc_rtp *) &data[0];
1704
1705 rtp->callref = callref;
1706 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001707 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001708 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1709 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001710 }
Harald Welte27989d42018-06-21 20:39:20 +02001711 rtp->payload_type = payload_type;
1712 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001713 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001714}
1715
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001716static 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 +02001717{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001718 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001719}
1720
1721static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1722{
1723 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001724
1725 /* Find callref */
1726 trans = trans_find_by_callref(net, callref);
1727 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001728 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001729 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001730 return -EIO;
1731 }
1732 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001733 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001734 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001735 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001736 return 0;
1737 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001738 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001739
Harald Welte27989d42018-06-21 20:39:20 +02001740 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001741 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001742}
1743
1744/* Trigger TCH_RTP_CREATE acknowledgement */
1745int gsm48_tch_rtp_create(struct gsm_trans *trans)
1746{
1747 /* This function is called as soon as the port, on which the
1748 * mgcp-gw expects the incoming RTP stream from the remote
1749 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001750 struct msc_a *msc_a = trans->msc_a;
1751 struct gsm_network *net = msc_a_net(msc_a);
1752 struct call_leg *cl = msc_a->cc.call_leg;
1753 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001754 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001755 int mncc_payload_msg_type;
1756 struct sdp_audio_codec *codec;
1757 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001758
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001759 if (!rtp_cn) {
1760 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1761 return -EINVAL;
1762 }
1763
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001764 codec_filter_run(&trans->cc.codecs);
1765 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1766
1767 if (!trans->cc.codecs.result.audio_codecs.count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001768 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001769 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001770 return -EINVAL;
1771 }
1772
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001773 /* Modify the MGW endpoint if necessary, usually this should already match and not cause MGCP. */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001774 rtp_stream_set_codecs(rtp_cn, &trans->cc.codecs.result.audio_codecs);
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001775 rtp_stream_commit(rtp_cn);
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001776
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001777 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001778 codec = &rtp_cn->codecs.codec[0];
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001779 m = codec_mapping_by_subtype_name(codec->subtype_name);
1780 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02001781
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001782 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1783 if (!rtp_cn_local) {
1784 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1785 return -EINVAL;
1786 }
1787
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001788 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
1789 codec->payload_type, mncc_payload_msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001790}
1791
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001792static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001793{
1794 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001795 struct call_leg *cl;
1796 struct rtp_stream *rtps;
1797 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001798 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001799
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001800 /* FIXME: in *rtp we should get the codec information of the remote
1801 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1802 * meaningful value based on this information but unfortunately we
1803 * can't do that yet because the mncc API can not signal dynamic
1804 * payload types yet. This must be fixed first. Also there may be
1805 * additional members necessary in trans->conn->rtp because we
1806 * somehow need to deal with dynamic payload types that do not
1807 * comply to 3gpp's assumptions of payload type numbers on the A
1808 * interface. See also related tickets: OS#3399 and OS1683 */
1809
Harald Welte27989d42018-06-21 20:39:20 +02001810 /* Find callref */
1811 trans = trans_find_by_callref(net, rtp->callref);
1812 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001813 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001814 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001815 return -EIO;
1816 }
1817 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001818 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001819 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001820 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001821 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001822 }
1823
Neels Hofmeyr90933d42022-01-13 20:10:52 +01001824 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(rtp->msg_type),
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001825 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1826 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001827
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001828 cl = trans->msc_a->cc.call_leg;
1829 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1830
1831 if (!rtps) {
1832 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1833 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1834 return -EINVAL;
1835 }
1836
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001837 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1838 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1839 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1840 return -EINVAL;
1841 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001842 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1843 rtp_stream_commit(rtps);
1844 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001845}
1846
1847static struct downstate {
1848 uint32_t states;
1849 int type;
1850 int (*rout) (struct gsm_trans *trans, void *arg);
1851} downstatelist[] = {
1852 /* mobile originating call establishment */
1853 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1854 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1855 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1856 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1857 {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 */
1858 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1859 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1860 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1861 /* mobile terminating call establishment */
1862 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1863 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1864 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1865 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1866 /* signalling during call */
1867 {SBIT(GSM_CSTATE_ACTIVE),
1868 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1869 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1870 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1871 {ALL_STATES,
1872 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1873 {ALL_STATES,
1874 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1875 {ALL_STATES,
1876 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1877 {SBIT(GSM_CSTATE_ACTIVE),
1878 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1879 {SBIT(GSM_CSTATE_ACTIVE),
1880 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1881 {SBIT(GSM_CSTATE_ACTIVE),
1882 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1883 {SBIT(GSM_CSTATE_ACTIVE),
1884 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1885 {SBIT(GSM_CSTATE_ACTIVE),
1886 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1887 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1888 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1889 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1890 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1891 {SBIT(GSM_CSTATE_ACTIVE),
1892 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1893 /* clearing */
1894 {SBIT(GSM_CSTATE_INITIATED),
1895 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1896 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1897 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1898 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1899 MNCC_REL_REQ, gsm48_cc_tx_release},
1900};
1901
1902#define DOWNSLLEN \
1903 (sizeof(downstatelist) / sizeof(struct downstate))
1904
1905
Philipp Maiercd64af72019-08-01 09:46:40 +02001906static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001907{
1908 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001909 struct msc_a *msc_a = NULL;
1910 struct gsm_trans *trans = NULL;
1911 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001912
Harald Welte27989d42018-06-21 20:39:20 +02001913 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001914 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001915 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001916 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001917 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001918 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001919 return rc;
1920 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001921 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001922 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001923 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001924 case MNCC_RTP_FREE:
1925 /* unused right now */
1926 return -EIO;
1927
1928 case MNCC_FRAME_DROP:
1929 case MNCC_FRAME_RECV:
1930 case GSM_TCHF_FRAME:
1931 case GSM_TCHF_FRAME_EFR:
1932 case GSM_TCHH_FRAME:
1933 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001934 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001935 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001936 return -ENOTSUP;
1937 }
1938
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001939 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001940
1941 /* Find callref */
1942 trans = trans_find_by_callref(net, data->callref);
1943
1944 /* Callref unknown */
1945 if (!trans) {
1946 struct vlr_subscr *vsub;
1947
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001948 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001949 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001950 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001951 /* Invalid call reference */
1952 return mncc_release_ind(net, NULL, data->callref,
1953 GSM48_CAUSE_LOC_PRN_S_LU,
1954 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1955 }
1956 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001957 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001958 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001959 /* Invalid number */
1960 return mncc_release_ind(net, NULL, data->callref,
1961 GSM48_CAUSE_LOC_PRN_S_LU,
1962 GSM48_CC_CAUSE_INV_NR_FORMAT);
1963 }
1964 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001965 if (data->called.number[0]) {
1966 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1967 if (!vsub)
1968 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001969 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001970 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001971 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001972 if (!vsub)
1973 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001974 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001975 }
1976 if (!vsub)
1977 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001978 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001979 /* update the subscriber we deal with */
1980 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1981
Harald Welte27989d42018-06-21 20:39:20 +02001982 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001983 if (!vsub->lu_complete) {
1984 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001985 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001986 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001987 /* Temporarily out of order */
1988 return mncc_release_ind(net, NULL, data->callref,
1989 GSM48_CAUSE_LOC_PRN_S_LU,
1990 GSM48_CC_CAUSE_DEST_OOO);
1991 }
Keith Whyte991bb422019-08-08 15:43:40 +02001992
1993 /* Find valid conn */
1994 msc_a = msc_a_for_vsub(vsub, true);
1995
1996 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1997 if (!net->call_waiting && msc_a) {
1998 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1999 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2000 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2001 "rx '%s' for subscriber %s with trans state (%s)"
2002 " rejecting with USER_BUSY\n",
2003 get_mncc_name(msg->msg_type), data->called.number,
2004 gsm48_cc_state_name(existing_cc_trans->cc.state));
2005 return mncc_release_ind(net, NULL, data->callref,
2006 GSM48_CAUSE_LOC_PRN_S_LU,
2007 GSM48_CC_CAUSE_USER_BUSY);
2008 }
2009 }
2010
Harald Welte27989d42018-06-21 20:39:20 +02002011 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002012 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002013 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002014 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002015 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002016 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002017 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002018 mncc_release_ind(net, NULL, data->callref,
2019 GSM48_CAUSE_LOC_PRN_S_LU,
2020 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2021 return -ENOMEM;
2022 }
2023
Harald Welte27989d42018-06-21 20:39:20 +02002024 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002025 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002026 /* This condition will return before the common logging of the received MNCC message below, so
2027 * log it now. */
2028 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002029
Harald Welte27989d42018-06-21 20:39:20 +02002030 /* store setup information until paging succeeds */
2031 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2032
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002033 /* Request a channel. If Paging already started, paging_request_start() will append the new
2034 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002035 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2036 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002037 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002038 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002039 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002040 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002041 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002042 return 0;
2043 }
2044
2045 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002046 trans->msc_a = msc_a;
2047 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002048 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002049 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002050 } else {
2051 /* update the subscriber we deal with */
2052 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2053 }
2054
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002055 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002056
Philipp Maier9ca7b312018-10-10 17:00:49 +02002057 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002058 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002059
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002060 if (trans->msc_a)
2061 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002062
2063 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002064 if (!msc_a) {
2065 struct gsm_mncc rel = {
2066 .callref = data->callref,
2067 };
2068 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002069 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2070 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002071 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002072 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2073 else
2074 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2075 trans->callref = 0;
2076 trans_free(trans);
2077 return rc;
2078 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002079 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002080 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002081 }
2082
2083 /* Find function for current state and message */
2084 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002085 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002086 && ((1 << trans->cc.state) & downstatelist[i].states))
2087 break;
2088 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002089 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002090 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002091 return 0;
2092 }
2093
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002094 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002095
2096 return rc;
2097}
2098
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002099struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2100{
2101 uint32_t callref;
2102
2103 switch (msg->msg_type) {
2104 case MNCC_BRIDGE:
2105 callref = msg->bridge.callref[0];
2106 break;
2107 case MNCC_RTP_CREATE:
2108 case MNCC_RTP_CONNECT:
2109 callref = msg->rtp.callref;
2110 break;
2111
2112 case MNCC_RTP_FREE:
2113 case MNCC_FRAME_DROP:
2114 case MNCC_FRAME_RECV:
2115 case GSM_TCHF_FRAME:
2116 case GSM_TCHF_FRAME_EFR:
2117 case GSM_TCHH_FRAME:
2118 case GSM_TCH_FRAME_AMR:
2119 return NULL;
2120
2121 default:
2122 callref = msg->signal.callref;
2123 break;
2124 }
2125
2126 return mncc_call_find_by_callref(callref);
2127}
2128
2129/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002130int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002131{
2132 const union mncc_msg *msg = arg;
2133 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002134
2135 if (msg->msg_type == MNCC_SETUP_REQ) {
2136 /* Incoming call to forward for inter-MSC Handover? */
2137 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2138 if (mncc_call)
2139 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2140 "Incoming call matches pending inter-MSC Handover Number\n");
2141 }
2142 if (!mncc_call) {
2143 /* Find already active MNCC FSM for this callref.
2144 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2145 * MNCC<->GSM-CC call handling. */
2146 mncc_call = mncc_find_by_callref_from_msg(msg);
2147 }
2148 if (mncc_call) {
2149 mncc_call_rx(mncc_call, msg);
2150 return 0;
2151 }
2152
2153 /* None of the above? Then it must be a normal GSM CC call related message. */
2154 return mncc_tx_to_gsm_cc(net, msg);
2155}
Harald Welte27989d42018-06-21 20:39:20 +02002156
2157static struct datastate {
2158 uint32_t states;
2159 int type;
2160 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2161} datastatelist[] = {
2162 /* mobile originating call establishment */
2163 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2164 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2165 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2166 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2167 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2168 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2169 /* mobile terminating call establishment */
2170 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2171 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2172 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2173 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2174 {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 */
2175 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2176 /* signalling during call */
2177 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2178 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2179 {SBIT(GSM_CSTATE_ACTIVE),
2180 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2181 {ALL_STATES,
2182 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2183 {ALL_STATES,
2184 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2185 {ALL_STATES,
2186 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2187 {SBIT(GSM_CSTATE_ACTIVE),
2188 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2189 {SBIT(GSM_CSTATE_ACTIVE),
2190 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2191 {SBIT(GSM_CSTATE_ACTIVE),
2192 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2193 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2194 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2195 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2196 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2197 {SBIT(GSM_CSTATE_ACTIVE),
2198 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2199 /* clearing */
2200 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2201 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2202 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2203 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2204 {ALL_STATES, /* 5.4.3.4 */
2205 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2206};
2207
2208#define DATASLLEN \
2209 (sizeof(datastatelist) / sizeof(struct datastate))
2210
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002211int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002212{
2213 struct gsm48_hdr *gh = msgb_l3(msg);
2214 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2215 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2216 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002217 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2218 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002219 int i, rc = 0;
2220
2221 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002222 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002223 return -EINVAL;
2224 }
2225
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002226 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002227 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002228 return -EINVAL;
2229 }
2230
2231 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002232 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002233
Harald Welte27989d42018-06-21 20:39:20 +02002234 /* Create transaction */
2235 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002236 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002237 trans = trans_alloc(net, vsub,
2238 TRANS_CC,
2239 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002240 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002241 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002242 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002243 GSM48_PDISC_CC | (transaction_id << 4),
2244 GSM48_MT_CC_RELEASE_COMPL);
2245 return -ENOMEM;
2246 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002247 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2248 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2249 trans_free(trans);
2250 return -EINVAL;
2251 }
2252
Harald Welte27989d42018-06-21 20:39:20 +02002253 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002254 msc_a_get(msc_a, MSC_A_USE_CC);
2255 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002256 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002257
2258 /* An earlier CM Service Request for this CC message now has concluded */
2259 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2260 LOG_MSC_A(msc_a, LOGL_ERROR,
2261 "Creating new CC transaction without prior CM Service Request\n");
2262 else
2263 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002264 }
2265
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002266 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2267 gsm48_cc_state_name(trans->cc.state));
2268
Harald Welte27989d42018-06-21 20:39:20 +02002269 /* find function for current state and message */
2270 for (i = 0; i < DATASLLEN; i++)
2271 if ((msg_type == datastatelist[i].type)
2272 && ((1 << trans->cc.state) & datastatelist[i].states))
2273 break;
2274 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002275 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002276
2277 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2278 * transaction right away. */
2279 if (trans->cc.state == GSM_CSTATE_NULL) {
2280 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2281 " -- disarding new CC transaction right away\n");
2282 trans_free(trans);
2283 }
Harald Welte27989d42018-06-21 20:39:20 +02002284 return 0;
2285 }
2286
2287 assert(trans->vsub);
2288
2289 rc = datastatelist[i].rout(trans, msg);
2290
Harald Welte27989d42018-06-21 20:39:20 +02002291 return rc;
2292}