blob: d94c59cfd21e474633908d5065a9d2bcd312cf09 [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));
773 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100774
Harald Welte27989d42018-06-21 20:39:20 +0200775 /* cause */
776 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
777 call_conf.fields |= MNCC_F_CAUSE;
778 gsm48_decode_cause(&call_conf.cause,
779 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
780 }
781 /* cc cap */
782 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
783 call_conf.fields |= MNCC_F_CCCAP;
784 gsm48_decode_cccap(&call_conf.cccap,
785 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
786 }
787
788 /* IMSI of called subscriber */
789 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
790
791 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
792
793 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100794 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200795
796 /* don't continue, if there were problems with
797 * the call assignment. */
798 if (rc)
799 return rc;
800
801 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
802 &call_conf);
803}
804
805static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
806{
807 struct gsm_mncc *proceeding = arg;
808 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
809 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
810 int rc;
811
812 gh->msg_type = GSM48_MT_CC_CALL_PROC;
813
814 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
815
816 /* bearer capability */
817 if (proceeding->fields & MNCC_F_BEARER_CAP) {
818 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
819 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
820 }
821 /* facility */
822 if (proceeding->fields & MNCC_F_FACILITY)
823 gsm48_encode_facility(msg, 0, &proceeding->facility);
824 /* progress */
825 if (proceeding->fields & MNCC_F_PROGRESS)
826 gsm48_encode_progress(msg, 0, &proceeding->progress);
827
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100828 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200829 if (rc)
830 return rc;
831
832 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100833 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200834}
835
836static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
837{
838 struct gsm48_hdr *gh = msgb_l3(msg);
839 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
840 struct tlv_parsed tp;
841 struct gsm_mncc alerting;
842
843 gsm48_stop_cc_timer(trans);
844 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
845
846 memset(&alerting, 0, sizeof(struct gsm_mncc));
847 alerting.callref = trans->callref;
848 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
849 /* facility */
850 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
851 alerting.fields |= MNCC_F_FACILITY;
852 gsm48_decode_facility(&alerting.facility,
853 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
854 }
855
856 /* progress */
857 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
858 alerting.fields |= MNCC_F_PROGRESS;
859 gsm48_decode_progress(&alerting.progress,
860 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
861 }
862 /* ss-version */
863 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
864 alerting.fields |= MNCC_F_SSVERSION;
865 gsm48_decode_ssversion(&alerting.ssversion,
866 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
867 }
868
869 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
870
871 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
872 &alerting);
873}
874
875static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
876{
877 struct gsm_mncc *alerting = arg;
878 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
879 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
880
881 gh->msg_type = GSM48_MT_CC_ALERTING;
882
883 /* facility */
884 if (alerting->fields & MNCC_F_FACILITY)
885 gsm48_encode_facility(msg, 0, &alerting->facility);
886 /* progress */
887 if (alerting->fields & MNCC_F_PROGRESS)
888 gsm48_encode_progress(msg, 0, &alerting->progress);
889 /* user-user */
890 if (alerting->fields & MNCC_F_USERUSER)
891 gsm48_encode_useruser(msg, 0, &alerting->useruser);
892
893 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
894
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100895 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200896}
897
898static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
899{
900 struct gsm_mncc *progress = arg;
901 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
902 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
903
904 gh->msg_type = GSM48_MT_CC_PROGRESS;
905
906 /* progress */
907 gsm48_encode_progress(msg, 1, &progress->progress);
908 /* user-user */
909 if (progress->fields & MNCC_F_USERUSER)
910 gsm48_encode_useruser(msg, 0, &progress->useruser);
911
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100912 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200913}
914
915static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
916{
917 struct gsm_mncc *connect = arg;
918 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
919 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
920
921 gh->msg_type = GSM48_MT_CC_CONNECT;
922
923 gsm48_stop_cc_timer(trans);
924 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
925
926 /* facility */
927 if (connect->fields & MNCC_F_FACILITY)
928 gsm48_encode_facility(msg, 0, &connect->facility);
929 /* progress */
930 if (connect->fields & MNCC_F_PROGRESS)
931 gsm48_encode_progress(msg, 0, &connect->progress);
932 /* connected number */
933 if (connect->fields & MNCC_F_CONNECTED)
934 gsm48_encode_connected(msg, &connect->connected);
935 /* user-user */
936 if (connect->fields & MNCC_F_USERUSER)
937 gsm48_encode_useruser(msg, 0, &connect->useruser);
938
939 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
940
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100941 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200942}
943
944static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
945{
946 struct gsm48_hdr *gh = msgb_l3(msg);
947 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
948 struct tlv_parsed tp;
949 struct gsm_mncc connect;
950
951 gsm48_stop_cc_timer(trans);
952
953 memset(&connect, 0, sizeof(struct gsm_mncc));
954 connect.callref = trans->callref;
955 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
956 /* use subscriber as connected party number */
957 connect.fields |= MNCC_F_CONNECTED;
958 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
959 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
960
961 /* facility */
962 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
963 connect.fields |= MNCC_F_FACILITY;
964 gsm48_decode_facility(&connect.facility,
965 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
966 }
967 /* user-user */
968 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
969 connect.fields |= MNCC_F_USERUSER;
970 gsm48_decode_useruser(&connect.useruser,
971 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
972 }
973 /* ss-version */
974 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
975 connect.fields |= MNCC_F_SSVERSION;
976 gsm48_decode_ssversion(&connect.ssversion,
977 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
978 }
979
980 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200981 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200982
983 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
984}
985
986
987static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
988{
989 struct gsm_mncc connect_ack;
990
991 gsm48_stop_cc_timer(trans);
992
993 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200994 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 +0200995
996 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
997 connect_ack.callref = trans->callref;
998
999 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1000 &connect_ack);
1001}
1002
1003static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1004{
1005 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1006 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1007
1008 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1009
1010 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1011
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001012 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001013}
1014
1015static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1016{
1017 struct gsm48_hdr *gh = msgb_l3(msg);
1018 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1019 struct tlv_parsed tp;
1020 struct gsm_mncc disc;
1021
1022 gsm48_stop_cc_timer(trans);
1023
1024 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1025
1026 memset(&disc, 0, sizeof(struct gsm_mncc));
1027 disc.callref = trans->callref;
1028 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1029 /* cause */
1030 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1031 disc.fields |= MNCC_F_CAUSE;
1032 gsm48_decode_cause(&disc.cause,
1033 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1034 }
1035 /* facility */
1036 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1037 disc.fields |= MNCC_F_FACILITY;
1038 gsm48_decode_facility(&disc.facility,
1039 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1040 }
1041 /* user-user */
1042 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1043 disc.fields |= MNCC_F_USERUSER;
1044 gsm48_decode_useruser(&disc.useruser,
1045 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1046 }
1047 /* ss-version */
1048 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1049 disc.fields |= MNCC_F_SSVERSION;
1050 gsm48_decode_ssversion(&disc.ssversion,
1051 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1052 }
1053
1054 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001055}
1056
1057static struct gsm_mncc_cause default_cause = {
1058 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1059 .coding = 0,
1060 .rec = 0,
1061 .rec_val = 0,
1062 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1063 .diag_len = 0,
1064 .diag = { 0 },
1065};
1066
1067static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1068{
1069 struct gsm_mncc *disc = arg;
1070 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1071 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1072
1073 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1074
1075 gsm48_stop_cc_timer(trans);
1076 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1077
1078 /* cause */
1079 if (disc->fields & MNCC_F_CAUSE)
1080 gsm48_encode_cause(msg, 1, &disc->cause);
1081 else
1082 gsm48_encode_cause(msg, 1, &default_cause);
1083
1084 /* facility */
1085 if (disc->fields & MNCC_F_FACILITY)
1086 gsm48_encode_facility(msg, 0, &disc->facility);
1087 /* progress */
1088 if (disc->fields & MNCC_F_PROGRESS)
1089 gsm48_encode_progress(msg, 0, &disc->progress);
1090 /* user-user */
1091 if (disc->fields & MNCC_F_USERUSER)
1092 gsm48_encode_useruser(msg, 0, &disc->useruser);
1093
1094 /* store disconnect cause for T306 expiry */
1095 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1096
1097 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1098
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001099 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001100}
1101
1102static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1103{
1104 struct gsm48_hdr *gh = msgb_l3(msg);
1105 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1106 struct tlv_parsed tp;
1107 struct gsm_mncc rel;
1108 int rc;
1109
1110 gsm48_stop_cc_timer(trans);
1111
1112 memset(&rel, 0, sizeof(struct gsm_mncc));
1113 rel.callref = trans->callref;
1114 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1115 /* cause */
1116 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1117 rel.fields |= MNCC_F_CAUSE;
1118 gsm48_decode_cause(&rel.cause,
1119 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1120 }
1121 /* facility */
1122 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1123 rel.fields |= MNCC_F_FACILITY;
1124 gsm48_decode_facility(&rel.facility,
1125 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1126 }
1127 /* user-user */
1128 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1129 rel.fields |= MNCC_F_USERUSER;
1130 gsm48_decode_useruser(&rel.useruser,
1131 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1132 }
1133 /* ss-version */
1134 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1135 rel.fields |= MNCC_F_SSVERSION;
1136 gsm48_decode_ssversion(&rel.ssversion,
1137 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1138 }
1139
1140 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1141 /* release collision 5.4.5 */
1142 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1143 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001144 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001145 GSM48_PDISC_CC | (trans->transaction_id << 4),
1146 GSM48_MT_CC_RELEASE_COMPL);
1147 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1148 }
1149
1150 new_cc_state(trans, GSM_CSTATE_NULL);
1151
1152 trans->callref = 0;
1153 trans_free(trans);
1154
1155 return rc;
1156}
1157
1158static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1159{
1160 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001161 struct msgb *msg;
1162 struct gsm48_hdr *gh;
1163
1164 if (!trans->msc_a) {
1165 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1166 return -EINVAL;
1167 }
1168
1169 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1170 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001171
1172 gh->msg_type = GSM48_MT_CC_RELEASE;
1173
1174 gsm48_stop_cc_timer(trans);
1175 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1176
1177 /* cause */
1178 if (rel->fields & MNCC_F_CAUSE)
1179 gsm48_encode_cause(msg, 0, &rel->cause);
1180 /* facility */
1181 if (rel->fields & MNCC_F_FACILITY)
1182 gsm48_encode_facility(msg, 0, &rel->facility);
1183 /* user-user */
1184 if (rel->fields & MNCC_F_USERUSER)
1185 gsm48_encode_useruser(msg, 0, &rel->useruser);
1186
1187 trans->cc.T308_second = 0;
1188 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1189
1190 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1191 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1192
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001193 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001194}
1195
1196static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1197{
1198 struct gsm48_hdr *gh = msgb_l3(msg);
1199 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1200 struct tlv_parsed tp;
1201 struct gsm_mncc rel;
1202 int rc = 0;
1203
1204 gsm48_stop_cc_timer(trans);
1205
1206 memset(&rel, 0, sizeof(struct gsm_mncc));
1207 rel.callref = trans->callref;
1208 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1209 /* cause */
1210 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1211 rel.fields |= MNCC_F_CAUSE;
1212 gsm48_decode_cause(&rel.cause,
1213 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1214 }
1215 /* facility */
1216 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1217 rel.fields |= MNCC_F_FACILITY;
1218 gsm48_decode_facility(&rel.facility,
1219 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1220 }
1221 /* user-user */
1222 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1223 rel.fields |= MNCC_F_USERUSER;
1224 gsm48_decode_useruser(&rel.useruser,
1225 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1226 }
1227 /* ss-version */
1228 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1229 rel.fields |= MNCC_F_SSVERSION;
1230 gsm48_decode_ssversion(&rel.ssversion,
1231 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1232 }
1233
1234 if (trans->callref) {
1235 switch (trans->cc.state) {
1236 case GSM_CSTATE_CALL_PRESENT:
1237 rc = mncc_recvmsg(trans->net, trans,
1238 MNCC_REJ_IND, &rel);
1239 break;
1240 case GSM_CSTATE_RELEASE_REQ:
1241 rc = mncc_recvmsg(trans->net, trans,
1242 MNCC_REL_CNF, &rel);
1243 break;
1244 default:
1245 rc = mncc_recvmsg(trans->net, trans,
1246 MNCC_REL_IND, &rel);
1247 }
1248 }
1249
1250 trans->callref = 0;
1251 trans_free(trans);
1252
1253 return rc;
1254}
1255
1256static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1257{
1258 struct gsm_mncc *rel = arg;
1259 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1260 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1261 int ret;
1262
1263 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1264
1265 trans->callref = 0;
1266
1267 gsm48_stop_cc_timer(trans);
1268
1269 /* cause */
1270 if (rel->fields & MNCC_F_CAUSE)
1271 gsm48_encode_cause(msg, 0, &rel->cause);
1272 /* facility */
1273 if (rel->fields & MNCC_F_FACILITY)
1274 gsm48_encode_facility(msg, 0, &rel->facility);
1275 /* user-user */
1276 if (rel->fields & MNCC_F_USERUSER)
1277 gsm48_encode_useruser(msg, 0, &rel->useruser);
1278
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001279 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001280
1281 trans_free(trans);
1282
1283 return ret;
1284}
1285
1286static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1287{
1288 struct gsm48_hdr *gh = msgb_l3(msg);
1289 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1290 struct tlv_parsed tp;
1291 struct gsm_mncc fac;
1292
1293 memset(&fac, 0, sizeof(struct gsm_mncc));
1294 fac.callref = trans->callref;
1295 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1296 /* facility */
1297 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1298 fac.fields |= MNCC_F_FACILITY;
1299 gsm48_decode_facility(&fac.facility,
1300 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1301 }
1302 /* ss-version */
1303 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1304 fac.fields |= MNCC_F_SSVERSION;
1305 gsm48_decode_ssversion(&fac.ssversion,
1306 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1307 }
1308
1309 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1310}
1311
1312static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1313{
1314 struct gsm_mncc *fac = arg;
1315 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1316 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1317
1318 gh->msg_type = GSM48_MT_CC_FACILITY;
1319
1320 /* facility */
1321 gsm48_encode_facility(msg, 1, &fac->facility);
1322
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001323 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001324}
1325
1326static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1327{
1328 struct gsm_mncc hold;
1329
1330 memset(&hold, 0, sizeof(struct gsm_mncc));
1331 hold.callref = trans->callref;
1332 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1333}
1334
1335static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1336{
1337 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1338 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1339
1340 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001342 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001343}
1344
1345static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1346{
1347 struct gsm_mncc *hold_rej = arg;
1348 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1349 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1350
1351 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1352
1353 /* cause */
1354 if (hold_rej->fields & MNCC_F_CAUSE)
1355 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1356 else
1357 gsm48_encode_cause(msg, 1, &default_cause);
1358
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001359 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001360}
1361
1362static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1363{
1364 struct gsm_mncc retrieve;
1365
1366 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1367 retrieve.callref = trans->callref;
1368 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1369 &retrieve);
1370}
1371
1372static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1373{
1374 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1375 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1376
1377 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1378
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001379 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001380}
1381
1382static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1383{
1384 struct gsm_mncc *retrieve_rej = arg;
1385 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1386 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1387
1388 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1389
1390 /* cause */
1391 if (retrieve_rej->fields & MNCC_F_CAUSE)
1392 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1393 else
1394 gsm48_encode_cause(msg, 1, &default_cause);
1395
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001396 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001397}
1398
1399static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1400{
1401 struct gsm48_hdr *gh = msgb_l3(msg);
1402 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1403 struct tlv_parsed tp;
1404 struct gsm_mncc dtmf;
1405
1406 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1407 dtmf.callref = trans->callref;
1408 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1409 /* keypad facility */
1410 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1411 dtmf.fields |= MNCC_F_KEYPAD;
1412 gsm48_decode_keypad(&dtmf.keypad,
1413 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1414 }
1415
1416 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1417}
1418
1419static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1420{
1421 struct gsm_mncc *dtmf = arg;
1422 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1423 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1424
1425 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1426
1427 /* keypad */
1428 if (dtmf->fields & MNCC_F_KEYPAD)
1429 gsm48_encode_keypad(msg, dtmf->keypad);
1430
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001431 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001432}
1433
1434static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1435{
1436 struct gsm_mncc *dtmf = arg;
1437 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1438 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1439
1440 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1441
1442 /* cause */
1443 if (dtmf->fields & MNCC_F_CAUSE)
1444 gsm48_encode_cause(msg, 1, &dtmf->cause);
1445 else
1446 gsm48_encode_cause(msg, 1, &default_cause);
1447
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001448 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001449}
1450
1451static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1452{
1453 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1454 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1455
1456 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1457
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001458 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001459}
1460
1461static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1462{
1463 struct gsm_mncc dtmf;
1464
1465 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1466 dtmf.callref = trans->callref;
1467
1468 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1469}
1470
1471static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1472{
1473 struct gsm48_hdr *gh = msgb_l3(msg);
1474 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1475 struct tlv_parsed tp;
1476 struct gsm_mncc modify;
1477
1478 memset(&modify, 0, sizeof(struct gsm_mncc));
1479 modify.callref = trans->callref;
1480 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1481 /* bearer capability */
1482 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1483 modify.fields |= MNCC_F_BEARER_CAP;
1484 gsm48_decode_bearer_cap(&modify.bearer_cap,
1485 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1486
1487 /* Create a copy of the bearer capability
1488 * in the transaction struct, so we can use
1489 * this information later */
1490 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1491 sizeof(trans->bearer_cap));
1492 }
1493
1494 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1495
1496 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1497}
1498
1499static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1500{
1501 struct gsm_mncc *modify = arg;
1502 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1503 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1504
1505 gh->msg_type = GSM48_MT_CC_MODIFY;
1506
1507 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1508
1509 /* bearer capability */
1510 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1511 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1512
1513 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1514
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001515 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001516}
1517
1518static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1519{
1520 struct gsm48_hdr *gh = msgb_l3(msg);
1521 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1522 struct tlv_parsed tp;
1523 struct gsm_mncc modify;
1524
1525 gsm48_stop_cc_timer(trans);
1526
1527 memset(&modify, 0, sizeof(struct gsm_mncc));
1528 modify.callref = trans->callref;
1529 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1530 /* bearer capability */
1531 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1532 modify.fields |= MNCC_F_BEARER_CAP;
1533 gsm48_decode_bearer_cap(&modify.bearer_cap,
1534 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1535
1536 /* Create a copy of the bearer capability
1537 * in the transaction struct, so we can use
1538 * this information later */
1539 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1540 sizeof(trans->bearer_cap));
1541 }
1542
1543 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1544
1545 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1546}
1547
1548static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1549{
1550 struct gsm_mncc *modify = arg;
1551 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1553
1554 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1555
1556 /* bearer capability */
1557 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1558 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1559
1560 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1561
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001562 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001563}
1564
1565static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1566{
1567 struct gsm48_hdr *gh = msgb_l3(msg);
1568 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1569 struct tlv_parsed tp;
1570 struct gsm_mncc modify;
1571
1572 gsm48_stop_cc_timer(trans);
1573
1574 memset(&modify, 0, sizeof(struct gsm_mncc));
1575 modify.callref = trans->callref;
1576 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1577 /* bearer capability */
1578 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1579 modify.fields |= GSM48_IE_BEARER_CAP;
1580 gsm48_decode_bearer_cap(&modify.bearer_cap,
1581 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1582
1583 /* Create a copy of the bearer capability
1584 * in the transaction struct, so we can use
1585 * this information later */
1586 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1587 sizeof(trans->bearer_cap));
1588 }
1589 /* cause */
1590 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1591 modify.fields |= MNCC_F_CAUSE;
1592 gsm48_decode_cause(&modify.cause,
1593 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1594 }
1595
1596 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1597
1598 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1599}
1600
1601static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1602{
1603 struct gsm_mncc *modify = arg;
1604 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1605 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1606
1607 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1608
1609 /* bearer capability */
1610 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1611 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1612 /* cause */
1613 gsm48_encode_cause(msg, 1, &modify->cause);
1614
1615 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1616
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001617 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001618}
1619
1620static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1621{
1622 struct gsm_mncc *notify = arg;
1623 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1624 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1625
1626 gh->msg_type = GSM48_MT_CC_NOTIFY;
1627
1628 /* notify */
1629 gsm48_encode_notify(msg, notify->notify);
1630
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001631 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001632}
1633
1634static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1635{
1636 struct gsm48_hdr *gh = msgb_l3(msg);
1637 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1638// struct tlv_parsed tp;
1639 struct gsm_mncc notify;
1640
1641 memset(&notify, 0, sizeof(struct gsm_mncc));
1642 notify.callref = trans->callref;
1643// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1644 if (payload_len >= 1)
1645 gsm48_decode_notify(&notify.notify, gh->data);
1646
1647 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1648}
1649
1650static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1651{
1652 struct gsm_mncc *user = arg;
1653 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1655
1656 gh->msg_type = GSM48_MT_CC_USER_INFO;
1657
1658 /* user-user */
1659 if (user->fields & MNCC_F_USERUSER)
1660 gsm48_encode_useruser(msg, 1, &user->useruser);
1661 /* more data */
1662 if (user->more)
1663 gsm48_encode_more(msg);
1664
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001665 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001666}
1667
1668static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1669{
1670 struct gsm48_hdr *gh = msgb_l3(msg);
1671 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1672 struct tlv_parsed tp;
1673 struct gsm_mncc user;
1674
1675 memset(&user, 0, sizeof(struct gsm_mncc));
1676 user.callref = trans->callref;
1677 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1678 /* user-user */
1679 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1680 user.fields |= MNCC_F_USERUSER;
1681 gsm48_decode_useruser(&user.useruser,
1682 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1683 }
1684 /* more data */
1685 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1686 user.more = 1;
1687
1688 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1689}
1690
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001691static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1692 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1693 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001694{
1695 uint8_t data[sizeof(struct gsm_mncc)];
1696 struct gsm_mncc_rtp *rtp;
1697
1698 memset(&data, 0, sizeof(data));
1699 rtp = (struct gsm_mncc_rtp *) &data[0];
1700
1701 rtp->callref = callref;
1702 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001703 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001704 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1705 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001706 }
Harald Welte27989d42018-06-21 20:39:20 +02001707 rtp->payload_type = payload_type;
1708 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001709 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001710}
1711
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001712static 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 +02001713{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001714 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001715}
1716
1717static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1718{
1719 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001720
1721 /* Find callref */
1722 trans = trans_find_by_callref(net, callref);
1723 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001724 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001725 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001726 return -EIO;
1727 }
1728 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001729 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001730 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001731 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001732 return 0;
1733 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001734 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001735
Harald Welte27989d42018-06-21 20:39:20 +02001736 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001737 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001738}
1739
1740/* Trigger TCH_RTP_CREATE acknowledgement */
1741int gsm48_tch_rtp_create(struct gsm_trans *trans)
1742{
1743 /* This function is called as soon as the port, on which the
1744 * mgcp-gw expects the incoming RTP stream from the remote
1745 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001746 struct msc_a *msc_a = trans->msc_a;
1747 struct gsm_network *net = msc_a_net(msc_a);
1748 struct call_leg *cl = msc_a->cc.call_leg;
1749 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001750 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1751 uint32_t payload_type;
1752 int payload_msg_type;
1753 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001754
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001755 if (!rtp_cn) {
1756 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1757 return -EINVAL;
1758 }
1759
1760 if (!rtp_cn->codec_known) {
1761 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1762 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1763 return -EINVAL;
1764 }
1765
1766 /* Codec */
1767 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1768
1769 /* Payload Type number */
1770 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001771 if (mgcp_info && mgcp_info->ptmap_len)
1772 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1773 else
1774 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001775
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001776 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1777 if (!rtp_cn_local) {
1778 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1779 return -EINVAL;
1780 }
1781
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001782 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, payload_type, payload_msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001783}
1784
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001785static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001786{
1787 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001788 struct call_leg *cl;
1789 struct rtp_stream *rtps;
1790 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001791 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001792
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001793 /* FIXME: in *rtp we should get the codec information of the remote
1794 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1795 * meaningful value based on this information but unfortunately we
1796 * can't do that yet because the mncc API can not signal dynamic
1797 * payload types yet. This must be fixed first. Also there may be
1798 * additional members necessary in trans->conn->rtp because we
1799 * somehow need to deal with dynamic payload types that do not
1800 * comply to 3gpp's assumptions of payload type numbers on the A
1801 * interface. See also related tickets: OS#3399 and OS1683 */
1802
Harald Welte27989d42018-06-21 20:39:20 +02001803 /* Find callref */
1804 trans = trans_find_by_callref(net, rtp->callref);
1805 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001806 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001807 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001808 return -EIO;
1809 }
1810 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001811 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001812 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001813 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001814 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001815 }
1816
Neels Hofmeyr90933d42022-01-13 20:10:52 +01001817 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 +02001818 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1819 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001820
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001821 cl = trans->msc_a->cc.call_leg;
1822 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1823
1824 if (!rtps) {
1825 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1826 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1827 return -EINVAL;
1828 }
1829
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001830 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1831 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1832 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1833 return -EINVAL;
1834 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001835 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1836 rtp_stream_commit(rtps);
1837 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001838}
1839
1840static struct downstate {
1841 uint32_t states;
1842 int type;
1843 int (*rout) (struct gsm_trans *trans, void *arg);
1844} downstatelist[] = {
1845 /* mobile originating call establishment */
1846 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1847 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1848 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1849 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1850 {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 */
1851 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1852 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1853 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1854 /* mobile terminating call establishment */
1855 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1856 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1857 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1858 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1859 /* signalling during call */
1860 {SBIT(GSM_CSTATE_ACTIVE),
1861 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1862 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1863 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1864 {ALL_STATES,
1865 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1866 {ALL_STATES,
1867 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1868 {ALL_STATES,
1869 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1870 {SBIT(GSM_CSTATE_ACTIVE),
1871 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1872 {SBIT(GSM_CSTATE_ACTIVE),
1873 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1874 {SBIT(GSM_CSTATE_ACTIVE),
1875 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1876 {SBIT(GSM_CSTATE_ACTIVE),
1877 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1878 {SBIT(GSM_CSTATE_ACTIVE),
1879 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1880 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1881 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1882 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1883 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1884 {SBIT(GSM_CSTATE_ACTIVE),
1885 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1886 /* clearing */
1887 {SBIT(GSM_CSTATE_INITIATED),
1888 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1889 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1890 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1891 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1892 MNCC_REL_REQ, gsm48_cc_tx_release},
1893};
1894
1895#define DOWNSLLEN \
1896 (sizeof(downstatelist) / sizeof(struct downstate))
1897
1898
Philipp Maiercd64af72019-08-01 09:46:40 +02001899static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001900{
1901 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902 struct msc_a *msc_a = NULL;
1903 struct gsm_trans *trans = NULL;
1904 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001905
Harald Welte27989d42018-06-21 20:39:20 +02001906 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001907 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001908 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001909 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001910 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001911 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001912 return rc;
1913 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001914 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001915 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001916 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001917 case MNCC_RTP_FREE:
1918 /* unused right now */
1919 return -EIO;
1920
1921 case MNCC_FRAME_DROP:
1922 case MNCC_FRAME_RECV:
1923 case GSM_TCHF_FRAME:
1924 case GSM_TCHF_FRAME_EFR:
1925 case GSM_TCHH_FRAME:
1926 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001927 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001928 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001929 return -ENOTSUP;
1930 }
1931
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001932 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001933
1934 /* Find callref */
1935 trans = trans_find_by_callref(net, data->callref);
1936
1937 /* Callref unknown */
1938 if (!trans) {
1939 struct vlr_subscr *vsub;
1940
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001941 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001942 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001943 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001944 /* Invalid call reference */
1945 return mncc_release_ind(net, NULL, data->callref,
1946 GSM48_CAUSE_LOC_PRN_S_LU,
1947 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1948 }
1949 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001950 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001951 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001952 /* Invalid number */
1953 return mncc_release_ind(net, NULL, data->callref,
1954 GSM48_CAUSE_LOC_PRN_S_LU,
1955 GSM48_CC_CAUSE_INV_NR_FORMAT);
1956 }
1957 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001958 if (data->called.number[0]) {
1959 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1960 if (!vsub)
1961 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001962 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001963 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001964 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001965 if (!vsub)
1966 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001967 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001968 }
1969 if (!vsub)
1970 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001971 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001972 /* update the subscriber we deal with */
1973 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1974
Harald Welte27989d42018-06-21 20:39:20 +02001975 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001976 if (!vsub->lu_complete) {
1977 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001978 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001979 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001980 /* Temporarily out of order */
1981 return mncc_release_ind(net, NULL, data->callref,
1982 GSM48_CAUSE_LOC_PRN_S_LU,
1983 GSM48_CC_CAUSE_DEST_OOO);
1984 }
Keith Whyte991bb422019-08-08 15:43:40 +02001985
1986 /* Find valid conn */
1987 msc_a = msc_a_for_vsub(vsub, true);
1988
1989 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1990 if (!net->call_waiting && msc_a) {
1991 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1992 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1993 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1994 "rx '%s' for subscriber %s with trans state (%s)"
1995 " rejecting with USER_BUSY\n",
1996 get_mncc_name(msg->msg_type), data->called.number,
1997 gsm48_cc_state_name(existing_cc_trans->cc.state));
1998 return mncc_release_ind(net, NULL, data->callref,
1999 GSM48_CAUSE_LOC_PRN_S_LU,
2000 GSM48_CC_CAUSE_USER_BUSY);
2001 }
2002 }
2003
Harald Welte27989d42018-06-21 20:39:20 +02002004 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002005 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002006 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002007 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002008 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002009 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002010 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002011 mncc_release_ind(net, NULL, data->callref,
2012 GSM48_CAUSE_LOC_PRN_S_LU,
2013 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2014 return -ENOMEM;
2015 }
2016
Harald Welte27989d42018-06-21 20:39:20 +02002017 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002018 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002019 /* This condition will return before the common logging of the received MNCC message below, so
2020 * log it now. */
2021 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002022
Harald Welte27989d42018-06-21 20:39:20 +02002023 /* store setup information until paging succeeds */
2024 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2025
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002026 /* Request a channel. If Paging already started, paging_request_start() will append the new
2027 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002028 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2029 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002030 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002031 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002032 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002033 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002034 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002035 return 0;
2036 }
2037
2038 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002039 trans->msc_a = msc_a;
2040 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002041 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002042 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002043 } else {
2044 /* update the subscriber we deal with */
2045 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2046 }
2047
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002048 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002049
Philipp Maier9ca7b312018-10-10 17:00:49 +02002050 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002051 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002052
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002053 if (trans->msc_a)
2054 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002055
2056 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002057 if (!msc_a) {
2058 struct gsm_mncc rel = {
2059 .callref = data->callref,
2060 };
2061 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002062 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2063 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002064 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002065 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2066 else
2067 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2068 trans->callref = 0;
2069 trans_free(trans);
2070 return rc;
2071 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002072 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002073 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002074 }
2075
2076 /* Find function for current state and message */
2077 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002078 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002079 && ((1 << trans->cc.state) & downstatelist[i].states))
2080 break;
2081 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002082 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002083 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002084 return 0;
2085 }
2086
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002087 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002088
2089 return rc;
2090}
2091
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002092struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2093{
2094 uint32_t callref;
2095
2096 switch (msg->msg_type) {
2097 case MNCC_BRIDGE:
2098 callref = msg->bridge.callref[0];
2099 break;
2100 case MNCC_RTP_CREATE:
2101 case MNCC_RTP_CONNECT:
2102 callref = msg->rtp.callref;
2103 break;
2104
2105 case MNCC_RTP_FREE:
2106 case MNCC_FRAME_DROP:
2107 case MNCC_FRAME_RECV:
2108 case GSM_TCHF_FRAME:
2109 case GSM_TCHF_FRAME_EFR:
2110 case GSM_TCHH_FRAME:
2111 case GSM_TCH_FRAME_AMR:
2112 return NULL;
2113
2114 default:
2115 callref = msg->signal.callref;
2116 break;
2117 }
2118
2119 return mncc_call_find_by_callref(callref);
2120}
2121
2122/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002123int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002124{
2125 const union mncc_msg *msg = arg;
2126 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002127
2128 if (msg->msg_type == MNCC_SETUP_REQ) {
2129 /* Incoming call to forward for inter-MSC Handover? */
2130 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2131 if (mncc_call)
2132 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2133 "Incoming call matches pending inter-MSC Handover Number\n");
2134 }
2135 if (!mncc_call) {
2136 /* Find already active MNCC FSM for this callref.
2137 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2138 * MNCC<->GSM-CC call handling. */
2139 mncc_call = mncc_find_by_callref_from_msg(msg);
2140 }
2141 if (mncc_call) {
2142 mncc_call_rx(mncc_call, msg);
2143 return 0;
2144 }
2145
2146 /* None of the above? Then it must be a normal GSM CC call related message. */
2147 return mncc_tx_to_gsm_cc(net, msg);
2148}
Harald Welte27989d42018-06-21 20:39:20 +02002149
2150static struct datastate {
2151 uint32_t states;
2152 int type;
2153 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2154} datastatelist[] = {
2155 /* mobile originating call establishment */
2156 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2157 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2158 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2159 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2160 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2161 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2162 /* mobile terminating call establishment */
2163 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2164 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2165 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2166 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2167 {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 */
2168 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2169 /* signalling during call */
2170 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2171 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2172 {SBIT(GSM_CSTATE_ACTIVE),
2173 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2174 {ALL_STATES,
2175 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2176 {ALL_STATES,
2177 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2178 {ALL_STATES,
2179 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2180 {SBIT(GSM_CSTATE_ACTIVE),
2181 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2182 {SBIT(GSM_CSTATE_ACTIVE),
2183 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2184 {SBIT(GSM_CSTATE_ACTIVE),
2185 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2186 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2187 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2188 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2189 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2190 {SBIT(GSM_CSTATE_ACTIVE),
2191 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2192 /* clearing */
2193 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2194 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2195 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2196 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2197 {ALL_STATES, /* 5.4.3.4 */
2198 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2199};
2200
2201#define DATASLLEN \
2202 (sizeof(datastatelist) / sizeof(struct datastate))
2203
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002204int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002205{
2206 struct gsm48_hdr *gh = msgb_l3(msg);
2207 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2208 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2209 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002210 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2211 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002212 int i, rc = 0;
2213
2214 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002215 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002216 return -EINVAL;
2217 }
2218
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002219 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002220 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002221 return -EINVAL;
2222 }
2223
2224 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002225 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002226
Harald Welte27989d42018-06-21 20:39:20 +02002227 /* Create transaction */
2228 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002229 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002230 trans = trans_alloc(net, vsub,
2231 TRANS_CC,
2232 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002233 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002234 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002235 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002236 GSM48_PDISC_CC | (transaction_id << 4),
2237 GSM48_MT_CC_RELEASE_COMPL);
2238 return -ENOMEM;
2239 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002240 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2241 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2242 trans_free(trans);
2243 return -EINVAL;
2244 }
2245
Harald Welte27989d42018-06-21 20:39:20 +02002246 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002247 msc_a_get(msc_a, MSC_A_USE_CC);
2248 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002249 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002250
2251 /* An earlier CM Service Request for this CC message now has concluded */
2252 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2253 LOG_MSC_A(msc_a, LOGL_ERROR,
2254 "Creating new CC transaction without prior CM Service Request\n");
2255 else
2256 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002257 }
2258
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002259 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2260 gsm48_cc_state_name(trans->cc.state));
2261
Harald Welte27989d42018-06-21 20:39:20 +02002262 /* find function for current state and message */
2263 for (i = 0; i < DATASLLEN; i++)
2264 if ((msg_type == datastatelist[i].type)
2265 && ((1 << trans->cc.state) & datastatelist[i].states))
2266 break;
2267 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002268 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002269
2270 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2271 * transaction right away. */
2272 if (trans->cc.state == GSM_CSTATE_NULL) {
2273 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2274 " -- disarding new CC transaction right away\n");
2275 trans_free(trans);
2276 }
Harald Welte27989d42018-06-21 20:39:20 +02002277 return 0;
2278 }
2279
2280 assert(trans->vsub);
2281
2282 rc = datastatelist[i].rout(trans, msg);
2283
Harald Welte27989d42018-06-21 20:39:20 +02002284 return rc;
2285}