blob: a06e9973583e546d0b6c2a6ad7e06a3d9133f023 [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>
Harald Welte27989d42018-06-21 20:39:20 +020058
59#include <osmocom/gsm/gsm48.h>
60#include <osmocom/gsm/gsm0480.h>
61#include <osmocom/gsm/gsm_utils.h>
62#include <osmocom/gsm/protocol/gsm_04_08.h>
63#include <osmocom/core/msgb.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/core/utils.h>
66#include <osmocom/core/byteswap.h>
67#include <osmocom/gsm/tlv.h>
68#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020069
70#include <assert.h>
71
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010072static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
73static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
75
76static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
77{
78 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
79 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
80 OMSC_LINKID_CB(msg) = trans->dlci;
81
82 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
83}
84
85uint32_t msc_cc_next_outgoing_callref() {
86 static uint32_t last_callref = 0x80000000;
87 last_callref++;
88 if (last_callref < 0x80000001)
89 last_callref = 0x80000001;
90 return last_callref;
91}
Harald Welte27989d42018-06-21 20:39:20 +020092
Philipp Maier9ca7b312018-10-10 17:00:49 +020093static void gsm48_cc_guard_timeout(void *arg)
94{
95 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010096 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020097 trans_free(trans);
98 return;
99}
100
101static void gsm48_stop_guard_timer(struct gsm_trans *trans)
102{
103 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100104 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200105 osmo_timer_del(&trans->cc.timer_guard);
106 }
107}
108
109static void gsm48_start_guard_timer(struct gsm_trans *trans)
110{
111 /* NOTE: The purpose of this timer is to prevent the cc state machine
112 * from hanging in cases where mncc, gsm48 or both become unresponsive
113 * for some reason. The timer is started initially with the setup from
114 * the gsm48 side and then re-started with every incoming mncc message.
115 * Once the mncc state reaches its active state the timer is stopped.
116 * So if the cc state machine does not show any activity for an
117 * extended amount of time during call setup or teardown the guard
118 * timer will time out and hard-clear the connection. */
119 if (osmo_timer_pending(&trans->cc.timer_guard))
120 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100121 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200122 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
123 osmo_timer_schedule(&trans->cc.timer_guard,
124 trans->net->mncc_guard_timeout, 0);
125}
Harald Welte27989d42018-06-21 20:39:20 +0200126
127/* Call Control */
128
129void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
130{
131 net->mncc_recv(net, msg);
132}
133
134int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
135{
136 struct gsm48_hdr *gh;
137 struct msgb *ss_notify;
138
139 ss_notify = gsm0480_create_notifySS(message);
140 if (!ss_notify)
141 return -1;
142
143 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
144 uint8_t *data = msgb_push(ss_notify, 1);
145 data[0] = ss_notify->len - 1;
146 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
147 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100148 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200149}
150
151/* FIXME: this count_statistics is a state machine behaviour. we should convert
152 * the complete call control into a state machine. Afterwards we can move this
153 * code into state transitions.
154 */
155static void count_statistics(struct gsm_trans *trans, int new_state)
156{
157 int old_state = trans->cc.state;
158 struct rate_ctr_group *msc = trans->net->msc_ctrs;
159
160 if (old_state == new_state)
161 return;
162
163 /* state incoming */
164 switch (new_state) {
165 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200166 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
167 1);
168 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200169 break;
170 }
171
172 /* state outgoing */
173 switch (old_state) {
174 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200175 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
176 1);
Harald Welte27989d42018-06-21 20:39:20 +0200177 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
178 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200179 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200180 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200181 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200182 break;
183 }
184}
185
Harald Welte27989d42018-06-21 20:39:20 +0200186static void new_cc_state(struct gsm_trans *trans, int state)
187{
188 if (state > 31 || state < 0)
189 return;
190
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100191 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
192 gsm48_cc_state_name(trans->cc.state),
193 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200194
195 count_statistics(trans, state);
196 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200197
198 /* Stop the guard timer when a call reaches the active state */
199 if (state == GSM_CSTATE_ACTIVE)
200 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200201}
202
203static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
204{
205 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
207 uint8_t *cause, *call_state;
208
209 gh->msg_type = GSM48_MT_CC_STATUS;
210
211 cause = msgb_put(msg, 3);
212 cause[0] = 2;
213 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
214 cause[2] = 0x80 | 30; /* response to status inquiry */
215
216 call_state = msgb_put(msg, 1);
217 call_state[0] = 0xc0 | 0x00;
218
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100219 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200220}
221
222static void gsm48_stop_cc_timer(struct gsm_trans *trans)
223{
224 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100225 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200226 osmo_timer_del(&trans->cc.timer);
227 trans->cc.Tcurrent = 0;
228 }
229}
230
231static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
232 int msg_type, struct gsm_mncc *mncc)
233{
234 struct msgb *msg;
235 unsigned char *data;
236
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100237 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200238
239 mncc->msg_type = msg_type;
240
241 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
242 if (!msg)
243 return -ENOMEM;
244
245 data = msgb_put(msg, sizeof(struct gsm_mncc));
246 memcpy(data, mncc, sizeof(struct gsm_mncc));
247
248 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200249 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
250 if (trans)
251 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200252
253 return 0;
254}
255
256int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
257 uint32_t callref, int location, int value)
258{
259 struct gsm_mncc rel;
260
261 memset(&rel, 0, sizeof(rel));
262 rel.callref = callref;
263 mncc_set_cause(&rel, location, value);
264 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
265 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
266 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
267}
268
269/* Call Control Specific transaction release.
270 * gets called by trans_free, DO NOT CALL YOURSELF! */
271void _gsm48_cc_trans_free(struct gsm_trans *trans)
272{
273 gsm48_stop_cc_timer(trans);
274
Harald Welte27989d42018-06-21 20:39:20 +0200275 /* send release to L4, if callref still exists */
276 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100277 /* Send MNCC REL.ind (cause='Resource unavailable') */
278 if (trans->cc.mncc_initiated) {
279 mncc_release_ind(trans->net, trans, trans->callref,
280 GSM48_CAUSE_LOC_PRN_S_LU,
281 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
282 }
283
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100284 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
285 * CC Release to the MS if it gets freed here. Hack it to do so. */
286 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
287 struct gsm_mncc rel = {};
288 rel.callref = trans->callref;
289 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
290 gsm48_cc_tx_release(trans, &rel);
291 }
Harald Welte27989d42018-06-21 20:39:20 +0200292 /* This is a final freeing of the transaction. The MNCC release may have triggered the
293 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
294 gsm48_stop_cc_timer(trans);
295 }
296 if (trans->cc.state != GSM_CSTATE_NULL)
297 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200298
299 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100300
301 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
302 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200303}
304
Harald Welte27989d42018-06-21 20:39:20 +0200305/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100306static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200307{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100308 if (trans->msc_a) {
309 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
310 "Handle paging error: transaction already associated with subscriber,"
311 " apparently it was already handled. Skip.\n");
312 return;
Harald Welte27989d42018-06-21 20:39:20 +0200313 }
314
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100315 if (msc_a) {
316 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
317 /* Assign conn */
318 msc_a_get(msc_a, MSC_A_USE_CC);
319 trans->msc_a = msc_a;
320 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200321
322 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300323 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200324 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300325 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
326 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
327 &trans->cc.msg.gcr[0],
328 sizeof(trans->cc.msg.gcr));
329 if (rc < 0)
330 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
331 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200332 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200333 }
334
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100335 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
336 /* send SETUP request to called party */
337 gsm48_cc_tx_setup(trans, &trans->cc.msg);
338 } else {
339 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
340 /* Temporarily out of order */
341 mncc_release_ind(trans->net, trans,
342 trans->callref,
343 GSM48_CAUSE_LOC_PRN_S_LU,
344 GSM48_CC_CAUSE_DEST_OOO);
345 trans->callref = 0;
346 trans->paging_request = NULL;
347 trans_free(trans);
348 }
Harald Welte27989d42018-06-21 20:39:20 +0200349}
350
351/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100352static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200353{
354 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
355 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100356 struct call_leg *cl1;
357 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200358
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100359 if (!trans1 || !trans2) {
360 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200361 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100362 }
Harald Welte27989d42018-06-21 20:39:20 +0200363
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100364 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100365 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
366 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 +0200367 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100368 }
369
370 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
371 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200372
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100373 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
374 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
375 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
376 * If we can't, then we must give up. */
377 cl1 = trans1->msc_a->cc.call_leg;
378 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200379
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100380 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200381}
382
383static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
384{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100385 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200386 return gsm48_cc_tx_status(trans, msg);
387}
388
Harald Welte27989d42018-06-21 20:39:20 +0200389static void gsm48_cc_timeout(void *arg)
390{
391 struct gsm_trans *trans = arg;
392 int disconnect = 0, release = 0;
393 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
394 int mo_location = GSM48_CAUSE_LOC_USER;
395 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
396 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
397 struct gsm_mncc mo_rel, l4_rel;
398
399 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
400 mo_rel.callref = trans->callref;
401 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
402 l4_rel.callref = trans->callref;
403
404 switch(trans->cc.Tcurrent) {
405 case 0x303:
406 release = 1;
407 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
408 break;
409 case 0x310:
410 disconnect = 1;
411 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
412 break;
413 case 0x313:
414 disconnect = 1;
415 /* unknown, did not find it in the specs */
416 break;
417 case 0x301:
418 disconnect = 1;
419 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
420 break;
421 case 0x308:
422 if (!trans->cc.T308_second) {
423 /* restart T308 a second time */
424 gsm48_cc_tx_release(trans, &trans->cc.msg);
425 trans->cc.T308_second = 1;
426 break; /* stay in release state */
427 }
428 trans_free(trans);
429 return;
430 case 0x306:
431 release = 1;
432 mo_cause = trans->cc.msg.cause.value;
433 mo_location = trans->cc.msg.cause.location;
434 break;
435 case 0x323:
436 disconnect = 1;
437 break;
438 default:
439 release = 1;
440 }
441
442 if (release && trans->callref) {
443 /* process release towards layer 4 */
444 mncc_release_ind(trans->net, trans, trans->callref,
445 l4_location, l4_cause);
446 trans->callref = 0;
447 }
448
449 if (disconnect && trans->callref) {
450 /* process disconnect towards layer 4 */
451 mncc_set_cause(&l4_rel, l4_location, l4_cause);
452 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
453 }
454
455 /* process disconnect towards mobile station */
456 if (disconnect || release) {
457 mncc_set_cause(&mo_rel, mo_location, mo_cause);
458 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
459 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
460 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
461 mo_rel.cause.diag_len = 3;
462
463 if (disconnect)
464 gsm48_cc_tx_disconnect(trans, &mo_rel);
465 if (release)
466 gsm48_cc_tx_release(trans, &mo_rel);
467 }
468
469}
470
471/* disconnect both calls from the bridge */
472static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100473 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200474{
475 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
476 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
477 struct gsm_mncc mx_rel;
478 if (!trans0 || !trans1)
479 return;
480
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100481 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
482 trans0->callref, trans1->callref, strerror(err));
483 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200484 trans0->callref, trans1->callref, strerror(err));
485
486 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
487 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
488 GSM48_CC_CAUSE_CHAN_UNACCEPT);
489
490 mx_rel.callref = trans0->callref;
491 gsm48_cc_tx_disconnect(trans0, &mx_rel);
492
493 mx_rel.callref = trans1->callref;
494 gsm48_cc_tx_disconnect(trans1, &mx_rel);
495}
496
497static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
498 int sec, int micro)
499{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100500 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200501 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
502 osmo_timer_schedule(&trans->cc.timer, sec, micro);
503 trans->cc.Tcurrent = current;
504}
505
506static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
507{
508 struct gsm48_hdr *gh = msgb_l3(msg);
509 uint8_t msg_type = gsm48_hdr_msg_type(gh);
510 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
511 struct tlv_parsed tp;
512 struct gsm_mncc setup;
513
Philipp Maier9ca7b312018-10-10 17:00:49 +0200514 gsm48_start_guard_timer(trans);
515
Harald Welte27989d42018-06-21 20:39:20 +0200516 memset(&setup, 0, sizeof(struct gsm_mncc));
517 setup.callref = trans->callref;
518
Keith Whytea1a70be2021-05-16 02:59:52 +0200519 /* New Global Call Reference */
520 if (!trans->cc.lcls)
521 trans->cc.lcls = trans_lcls_compose(trans, true);
522
523 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300524 if (trans->cc.lcls) {
525 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
526 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
527 int rc;
528
529 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
530 memcpy(&setup.gcr[0], gcr_msg->data, rc);
531 setup.fields |= MNCC_F_GCR;
532 } else
533 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
534 msgb_free(gcr_msg);
535 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200536
Harald Welte27989d42018-06-21 20:39:20 +0200537 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
538 /* emergency setup is identified by msg_type */
539 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
540 setup.fields |= MNCC_F_EMERGENCY;
541 setup.emergency = 1;
542 /* use destination number as configured by user (if any) */
543 if (trans->net->emergency.route_to_msisdn) {
544 setup.fields |= MNCC_F_CALLED;
545 setup.called.type = 0; /* unknown */
546 setup.called.plan = 0; /* unknown */
547 OSMO_STRLCPY_ARRAY(setup.called.number,
548 trans->net->emergency.route_to_msisdn);
549 }
550 }
551
552 /* use subscriber as calling party number */
553 setup.fields |= MNCC_F_CALLING;
554 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
555 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
556
557 /* bearer capability */
558 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
559 setup.fields |= MNCC_F_BEARER_CAP;
560 gsm48_decode_bearer_cap(&setup.bearer_cap,
561 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
562
563 /* Create a copy of the bearer capability
564 * in the transaction struct, so we can use
565 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100566 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200567 sizeof(trans->bearer_cap));
568 }
569 /* facility */
570 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
571 setup.fields |= MNCC_F_FACILITY;
572 gsm48_decode_facility(&setup.facility,
573 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
574 }
575 /* called party bcd number */
576 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
577 setup.fields |= MNCC_F_CALLED;
578 gsm48_decode_called(&setup.called,
579 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
580 }
581 /* user-user */
582 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
583 setup.fields |= MNCC_F_USERUSER;
584 gsm48_decode_useruser(&setup.useruser,
585 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
586 }
587 /* ss-version */
588 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
589 setup.fields |= MNCC_F_SSVERSION;
590 gsm48_decode_ssversion(&setup.ssversion,
591 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
592 }
593 /* CLIR suppression */
594 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
595 setup.clir.sup = 1;
596 /* CLIR invocation */
597 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
598 setup.clir.inv = 1;
599 /* cc cap */
600 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
601 setup.fields |= MNCC_F_CCCAP;
602 gsm48_decode_cccap(&setup.cccap,
603 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
604 }
605
606 new_cc_state(trans, GSM_CSTATE_INITIATED);
607
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100608 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
609 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200610
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200611 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200612
613 /* indicate setup to MNCC */
614 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
615
616 /* MNCC code will modify the channel asynchronously, we should
617 * ipaccess-bind only after the modification has been made to the
618 * lchan->tch_mode */
619 return 0;
620}
621
622static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
623{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100624 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200625 struct gsm48_hdr *gh;
626 struct gsm_mncc *setup = arg;
627 int rc, trans_id;
628
629 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
630
631 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700632 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100633 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200634 "This is not allowed!\n");
635 /* Temporarily out of order */
636 rc = mncc_release_ind(trans->net, trans, trans->callref,
637 GSM48_CAUSE_LOC_PRN_S_LU,
638 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
639 trans->callref = 0;
640 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200641 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200642 return rc;
643 }
644
645 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100646 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200647 if (trans_id < 0) {
648 /* no free transaction ID */
649 rc = mncc_release_ind(trans->net, trans, trans->callref,
650 GSM48_CAUSE_LOC_PRN_S_LU,
651 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
652 trans->callref = 0;
653 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200654 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200655 return rc;
656 }
657 trans->transaction_id = trans_id;
658
659 gh->msg_type = GSM48_MT_CC_SETUP;
660
661 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
662
663 /* bearer capability */
664 if (setup->fields & MNCC_F_BEARER_CAP) {
665 /* Create a copy of the bearer capability in the transaction struct, so we
666 * can use this information later */
667 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
668 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
669 }
670 /* facility */
671 if (setup->fields & MNCC_F_FACILITY)
672 gsm48_encode_facility(msg, 0, &setup->facility);
673 /* progress */
674 if (setup->fields & MNCC_F_PROGRESS)
675 gsm48_encode_progress(msg, 0, &setup->progress);
676 /* calling party BCD number */
677 if (setup->fields & MNCC_F_CALLING)
678 gsm48_encode_calling(msg, &setup->calling);
679 /* called party BCD number */
680 if (setup->fields & MNCC_F_CALLED)
681 gsm48_encode_called(msg, &setup->called);
682 /* user-user */
683 if (setup->fields & MNCC_F_USERUSER)
684 gsm48_encode_useruser(msg, 0, &setup->useruser);
685 /* redirecting party BCD number */
686 if (setup->fields & MNCC_F_REDIRECTING)
687 gsm48_encode_redirecting(msg, &setup->redirecting);
688 /* signal */
689 if (setup->fields & MNCC_F_SIGNAL)
690 gsm48_encode_signal(msg, setup->signal);
691
692 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
693
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200694 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200695
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100696 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200697}
698
699static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
700{
701 struct gsm48_hdr *gh = msgb_l3(msg);
702 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
703 struct tlv_parsed tp;
704 struct gsm_mncc call_conf;
705 int rc;
706
707 gsm48_stop_cc_timer(trans);
708 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
709
710 memset(&call_conf, 0, sizeof(struct gsm_mncc));
711 call_conf.callref = trans->callref;
712
713 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
714#if 0
715 /* repeat */
716 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
717 call_conf.repeat = 1;
718 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
719 call_conf.repeat = 2;
720#endif
721 /* bearer capability */
722 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
723 call_conf.fields |= MNCC_F_BEARER_CAP;
724 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
725 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
726
727 /* Create a copy of the bearer capability
728 * in the transaction struct, so we can use
729 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100730 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200731 sizeof(trans->bearer_cap));
732 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100733
Harald Welte27989d42018-06-21 20:39:20 +0200734 /* cause */
735 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
736 call_conf.fields |= MNCC_F_CAUSE;
737 gsm48_decode_cause(&call_conf.cause,
738 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
739 }
740 /* cc cap */
741 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
742 call_conf.fields |= MNCC_F_CCCAP;
743 gsm48_decode_cccap(&call_conf.cccap,
744 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
745 }
746
747 /* IMSI of called subscriber */
748 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
749
750 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
751
752 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100753 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200754
755 /* don't continue, if there were problems with
756 * the call assignment. */
757 if (rc)
758 return rc;
759
760 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
761 &call_conf);
762}
763
764static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
765{
766 struct gsm_mncc *proceeding = arg;
767 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
768 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
769 int rc;
770
771 gh->msg_type = GSM48_MT_CC_CALL_PROC;
772
773 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
774
775 /* bearer capability */
776 if (proceeding->fields & MNCC_F_BEARER_CAP) {
777 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
778 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
779 }
780 /* facility */
781 if (proceeding->fields & MNCC_F_FACILITY)
782 gsm48_encode_facility(msg, 0, &proceeding->facility);
783 /* progress */
784 if (proceeding->fields & MNCC_F_PROGRESS)
785 gsm48_encode_progress(msg, 0, &proceeding->progress);
786
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100787 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200788 if (rc)
789 return rc;
790
791 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100792 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200793}
794
795static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
796{
797 struct gsm48_hdr *gh = msgb_l3(msg);
798 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
799 struct tlv_parsed tp;
800 struct gsm_mncc alerting;
801
802 gsm48_stop_cc_timer(trans);
803 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
804
805 memset(&alerting, 0, sizeof(struct gsm_mncc));
806 alerting.callref = trans->callref;
807 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
808 /* facility */
809 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
810 alerting.fields |= MNCC_F_FACILITY;
811 gsm48_decode_facility(&alerting.facility,
812 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
813 }
814
815 /* progress */
816 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
817 alerting.fields |= MNCC_F_PROGRESS;
818 gsm48_decode_progress(&alerting.progress,
819 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
820 }
821 /* ss-version */
822 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
823 alerting.fields |= MNCC_F_SSVERSION;
824 gsm48_decode_ssversion(&alerting.ssversion,
825 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
826 }
827
828 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
829
830 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
831 &alerting);
832}
833
834static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
835{
836 struct gsm_mncc *alerting = arg;
837 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
838 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
839
840 gh->msg_type = GSM48_MT_CC_ALERTING;
841
842 /* facility */
843 if (alerting->fields & MNCC_F_FACILITY)
844 gsm48_encode_facility(msg, 0, &alerting->facility);
845 /* progress */
846 if (alerting->fields & MNCC_F_PROGRESS)
847 gsm48_encode_progress(msg, 0, &alerting->progress);
848 /* user-user */
849 if (alerting->fields & MNCC_F_USERUSER)
850 gsm48_encode_useruser(msg, 0, &alerting->useruser);
851
852 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
853
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100854 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200855}
856
857static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
858{
859 struct gsm_mncc *progress = arg;
860 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
861 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
862
863 gh->msg_type = GSM48_MT_CC_PROGRESS;
864
865 /* progress */
866 gsm48_encode_progress(msg, 1, &progress->progress);
867 /* user-user */
868 if (progress->fields & MNCC_F_USERUSER)
869 gsm48_encode_useruser(msg, 0, &progress->useruser);
870
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100871 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200872}
873
874static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
875{
876 struct gsm_mncc *connect = arg;
877 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
878 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
879
880 gh->msg_type = GSM48_MT_CC_CONNECT;
881
882 gsm48_stop_cc_timer(trans);
883 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
884
885 /* facility */
886 if (connect->fields & MNCC_F_FACILITY)
887 gsm48_encode_facility(msg, 0, &connect->facility);
888 /* progress */
889 if (connect->fields & MNCC_F_PROGRESS)
890 gsm48_encode_progress(msg, 0, &connect->progress);
891 /* connected number */
892 if (connect->fields & MNCC_F_CONNECTED)
893 gsm48_encode_connected(msg, &connect->connected);
894 /* user-user */
895 if (connect->fields & MNCC_F_USERUSER)
896 gsm48_encode_useruser(msg, 0, &connect->useruser);
897
898 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
899
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100900 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200901}
902
903static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
904{
905 struct gsm48_hdr *gh = msgb_l3(msg);
906 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
907 struct tlv_parsed tp;
908 struct gsm_mncc connect;
909
910 gsm48_stop_cc_timer(trans);
911
912 memset(&connect, 0, sizeof(struct gsm_mncc));
913 connect.callref = trans->callref;
914 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
915 /* use subscriber as connected party number */
916 connect.fields |= MNCC_F_CONNECTED;
917 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
918 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
919
920 /* facility */
921 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
922 connect.fields |= MNCC_F_FACILITY;
923 gsm48_decode_facility(&connect.facility,
924 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
925 }
926 /* user-user */
927 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
928 connect.fields |= MNCC_F_USERUSER;
929 gsm48_decode_useruser(&connect.useruser,
930 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
931 }
932 /* ss-version */
933 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
934 connect.fields |= MNCC_F_SSVERSION;
935 gsm48_decode_ssversion(&connect.ssversion,
936 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
937 }
938
939 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200940 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200941
942 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
943}
944
945
946static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
947{
948 struct gsm_mncc connect_ack;
949
950 gsm48_stop_cc_timer(trans);
951
952 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200953 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 +0200954
955 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
956 connect_ack.callref = trans->callref;
957
958 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
959 &connect_ack);
960}
961
962static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
963{
964 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
965 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
966
967 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
968
969 new_cc_state(trans, GSM_CSTATE_ACTIVE);
970
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100971 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200972}
973
974static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
975{
976 struct gsm48_hdr *gh = msgb_l3(msg);
977 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
978 struct tlv_parsed tp;
979 struct gsm_mncc disc;
980
981 gsm48_stop_cc_timer(trans);
982
983 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
984
985 memset(&disc, 0, sizeof(struct gsm_mncc));
986 disc.callref = trans->callref;
987 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
988 /* cause */
989 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
990 disc.fields |= MNCC_F_CAUSE;
991 gsm48_decode_cause(&disc.cause,
992 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
993 }
994 /* facility */
995 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
996 disc.fields |= MNCC_F_FACILITY;
997 gsm48_decode_facility(&disc.facility,
998 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
999 }
1000 /* user-user */
1001 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1002 disc.fields |= MNCC_F_USERUSER;
1003 gsm48_decode_useruser(&disc.useruser,
1004 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1005 }
1006 /* ss-version */
1007 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1008 disc.fields |= MNCC_F_SSVERSION;
1009 gsm48_decode_ssversion(&disc.ssversion,
1010 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1011 }
1012
1013 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001014}
1015
1016static struct gsm_mncc_cause default_cause = {
1017 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1018 .coding = 0,
1019 .rec = 0,
1020 .rec_val = 0,
1021 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1022 .diag_len = 0,
1023 .diag = { 0 },
1024};
1025
1026static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1027{
1028 struct gsm_mncc *disc = arg;
1029 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1030 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1031
1032 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1033
1034 gsm48_stop_cc_timer(trans);
1035 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1036
1037 /* cause */
1038 if (disc->fields & MNCC_F_CAUSE)
1039 gsm48_encode_cause(msg, 1, &disc->cause);
1040 else
1041 gsm48_encode_cause(msg, 1, &default_cause);
1042
1043 /* facility */
1044 if (disc->fields & MNCC_F_FACILITY)
1045 gsm48_encode_facility(msg, 0, &disc->facility);
1046 /* progress */
1047 if (disc->fields & MNCC_F_PROGRESS)
1048 gsm48_encode_progress(msg, 0, &disc->progress);
1049 /* user-user */
1050 if (disc->fields & MNCC_F_USERUSER)
1051 gsm48_encode_useruser(msg, 0, &disc->useruser);
1052
1053 /* store disconnect cause for T306 expiry */
1054 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1055
1056 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1057
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001058 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001059}
1060
1061static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1062{
1063 struct gsm48_hdr *gh = msgb_l3(msg);
1064 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1065 struct tlv_parsed tp;
1066 struct gsm_mncc rel;
1067 int rc;
1068
1069 gsm48_stop_cc_timer(trans);
1070
1071 memset(&rel, 0, sizeof(struct gsm_mncc));
1072 rel.callref = trans->callref;
1073 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1074 /* cause */
1075 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1076 rel.fields |= MNCC_F_CAUSE;
1077 gsm48_decode_cause(&rel.cause,
1078 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1079 }
1080 /* facility */
1081 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1082 rel.fields |= MNCC_F_FACILITY;
1083 gsm48_decode_facility(&rel.facility,
1084 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1085 }
1086 /* user-user */
1087 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1088 rel.fields |= MNCC_F_USERUSER;
1089 gsm48_decode_useruser(&rel.useruser,
1090 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1091 }
1092 /* ss-version */
1093 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1094 rel.fields |= MNCC_F_SSVERSION;
1095 gsm48_decode_ssversion(&rel.ssversion,
1096 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1097 }
1098
1099 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1100 /* release collision 5.4.5 */
1101 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1102 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001103 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001104 GSM48_PDISC_CC | (trans->transaction_id << 4),
1105 GSM48_MT_CC_RELEASE_COMPL);
1106 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1107 }
1108
1109 new_cc_state(trans, GSM_CSTATE_NULL);
1110
1111 trans->callref = 0;
1112 trans_free(trans);
1113
1114 return rc;
1115}
1116
1117static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1118{
1119 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001120 struct msgb *msg;
1121 struct gsm48_hdr *gh;
1122
1123 if (!trans->msc_a) {
1124 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1125 return -EINVAL;
1126 }
1127
1128 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1129 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001130
1131 gh->msg_type = GSM48_MT_CC_RELEASE;
1132
1133 gsm48_stop_cc_timer(trans);
1134 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1135
1136 /* cause */
1137 if (rel->fields & MNCC_F_CAUSE)
1138 gsm48_encode_cause(msg, 0, &rel->cause);
1139 /* facility */
1140 if (rel->fields & MNCC_F_FACILITY)
1141 gsm48_encode_facility(msg, 0, &rel->facility);
1142 /* user-user */
1143 if (rel->fields & MNCC_F_USERUSER)
1144 gsm48_encode_useruser(msg, 0, &rel->useruser);
1145
1146 trans->cc.T308_second = 0;
1147 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1148
1149 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1150 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1151
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001152 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001153}
1154
1155static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1156{
1157 struct gsm48_hdr *gh = msgb_l3(msg);
1158 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1159 struct tlv_parsed tp;
1160 struct gsm_mncc rel;
1161 int rc = 0;
1162
1163 gsm48_stop_cc_timer(trans);
1164
1165 memset(&rel, 0, sizeof(struct gsm_mncc));
1166 rel.callref = trans->callref;
1167 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1168 /* cause */
1169 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1170 rel.fields |= MNCC_F_CAUSE;
1171 gsm48_decode_cause(&rel.cause,
1172 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1173 }
1174 /* facility */
1175 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1176 rel.fields |= MNCC_F_FACILITY;
1177 gsm48_decode_facility(&rel.facility,
1178 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1179 }
1180 /* user-user */
1181 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1182 rel.fields |= MNCC_F_USERUSER;
1183 gsm48_decode_useruser(&rel.useruser,
1184 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1185 }
1186 /* ss-version */
1187 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1188 rel.fields |= MNCC_F_SSVERSION;
1189 gsm48_decode_ssversion(&rel.ssversion,
1190 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1191 }
1192
1193 if (trans->callref) {
1194 switch (trans->cc.state) {
1195 case GSM_CSTATE_CALL_PRESENT:
1196 rc = mncc_recvmsg(trans->net, trans,
1197 MNCC_REJ_IND, &rel);
1198 break;
1199 case GSM_CSTATE_RELEASE_REQ:
1200 rc = mncc_recvmsg(trans->net, trans,
1201 MNCC_REL_CNF, &rel);
1202 break;
1203 default:
1204 rc = mncc_recvmsg(trans->net, trans,
1205 MNCC_REL_IND, &rel);
1206 }
1207 }
1208
1209 trans->callref = 0;
1210 trans_free(trans);
1211
1212 return rc;
1213}
1214
1215static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1216{
1217 struct gsm_mncc *rel = arg;
1218 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1220 int ret;
1221
1222 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1223
1224 trans->callref = 0;
1225
1226 gsm48_stop_cc_timer(trans);
1227
1228 /* cause */
1229 if (rel->fields & MNCC_F_CAUSE)
1230 gsm48_encode_cause(msg, 0, &rel->cause);
1231 /* facility */
1232 if (rel->fields & MNCC_F_FACILITY)
1233 gsm48_encode_facility(msg, 0, &rel->facility);
1234 /* user-user */
1235 if (rel->fields & MNCC_F_USERUSER)
1236 gsm48_encode_useruser(msg, 0, &rel->useruser);
1237
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001238 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001239
1240 trans_free(trans);
1241
1242 return ret;
1243}
1244
1245static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1246{
1247 struct gsm48_hdr *gh = msgb_l3(msg);
1248 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1249 struct tlv_parsed tp;
1250 struct gsm_mncc fac;
1251
1252 memset(&fac, 0, sizeof(struct gsm_mncc));
1253 fac.callref = trans->callref;
1254 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1255 /* facility */
1256 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1257 fac.fields |= MNCC_F_FACILITY;
1258 gsm48_decode_facility(&fac.facility,
1259 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1260 }
1261 /* ss-version */
1262 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1263 fac.fields |= MNCC_F_SSVERSION;
1264 gsm48_decode_ssversion(&fac.ssversion,
1265 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1266 }
1267
1268 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1269}
1270
1271static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1272{
1273 struct gsm_mncc *fac = arg;
1274 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1275 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1276
1277 gh->msg_type = GSM48_MT_CC_FACILITY;
1278
1279 /* facility */
1280 gsm48_encode_facility(msg, 1, &fac->facility);
1281
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001282 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001283}
1284
1285static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1286{
1287 struct gsm_mncc hold;
1288
1289 memset(&hold, 0, sizeof(struct gsm_mncc));
1290 hold.callref = trans->callref;
1291 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1292}
1293
1294static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1295{
1296 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1298
1299 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1300
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001301 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001302}
1303
1304static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1305{
1306 struct gsm_mncc *hold_rej = arg;
1307 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1308 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1309
1310 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1311
1312 /* cause */
1313 if (hold_rej->fields & MNCC_F_CAUSE)
1314 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1315 else
1316 gsm48_encode_cause(msg, 1, &default_cause);
1317
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001318 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001319}
1320
1321static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1322{
1323 struct gsm_mncc retrieve;
1324
1325 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1326 retrieve.callref = trans->callref;
1327 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1328 &retrieve);
1329}
1330
1331static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1332{
1333 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1334 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1335
1336 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1337
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001338 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001339}
1340
1341static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1342{
1343 struct gsm_mncc *retrieve_rej = arg;
1344 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1345 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1346
1347 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1348
1349 /* cause */
1350 if (retrieve_rej->fields & MNCC_F_CAUSE)
1351 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1352 else
1353 gsm48_encode_cause(msg, 1, &default_cause);
1354
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001355 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001356}
1357
1358static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1359{
1360 struct gsm48_hdr *gh = msgb_l3(msg);
1361 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1362 struct tlv_parsed tp;
1363 struct gsm_mncc dtmf;
1364
1365 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1366 dtmf.callref = trans->callref;
1367 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1368 /* keypad facility */
1369 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1370 dtmf.fields |= MNCC_F_KEYPAD;
1371 gsm48_decode_keypad(&dtmf.keypad,
1372 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1373 }
1374
1375 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1376}
1377
1378static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1379{
1380 struct gsm_mncc *dtmf = arg;
1381 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1383
1384 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1385
1386 /* keypad */
1387 if (dtmf->fields & MNCC_F_KEYPAD)
1388 gsm48_encode_keypad(msg, dtmf->keypad);
1389
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001390 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001391}
1392
1393static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1394{
1395 struct gsm_mncc *dtmf = arg;
1396 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1397 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1398
1399 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1400
1401 /* cause */
1402 if (dtmf->fields & MNCC_F_CAUSE)
1403 gsm48_encode_cause(msg, 1, &dtmf->cause);
1404 else
1405 gsm48_encode_cause(msg, 1, &default_cause);
1406
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001407 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001408}
1409
1410static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1411{
1412 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1413 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1414
1415 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1416
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001417 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001418}
1419
1420static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1421{
1422 struct gsm_mncc dtmf;
1423
1424 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1425 dtmf.callref = trans->callref;
1426
1427 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1428}
1429
1430static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1431{
1432 struct gsm48_hdr *gh = msgb_l3(msg);
1433 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1434 struct tlv_parsed tp;
1435 struct gsm_mncc modify;
1436
1437 memset(&modify, 0, sizeof(struct gsm_mncc));
1438 modify.callref = trans->callref;
1439 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1440 /* bearer capability */
1441 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1442 modify.fields |= MNCC_F_BEARER_CAP;
1443 gsm48_decode_bearer_cap(&modify.bearer_cap,
1444 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1445
1446 /* Create a copy of the bearer capability
1447 * in the transaction struct, so we can use
1448 * this information later */
1449 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1450 sizeof(trans->bearer_cap));
1451 }
1452
1453 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1454
1455 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1456}
1457
1458static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1459{
1460 struct gsm_mncc *modify = arg;
1461 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1462 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1463
1464 gh->msg_type = GSM48_MT_CC_MODIFY;
1465
1466 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1467
1468 /* bearer capability */
1469 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1470 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1471
1472 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1473
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001474 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001475}
1476
1477static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1478{
1479 struct gsm48_hdr *gh = msgb_l3(msg);
1480 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1481 struct tlv_parsed tp;
1482 struct gsm_mncc modify;
1483
1484 gsm48_stop_cc_timer(trans);
1485
1486 memset(&modify, 0, sizeof(struct gsm_mncc));
1487 modify.callref = trans->callref;
1488 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1489 /* bearer capability */
1490 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1491 modify.fields |= MNCC_F_BEARER_CAP;
1492 gsm48_decode_bearer_cap(&modify.bearer_cap,
1493 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1494
1495 /* Create a copy of the bearer capability
1496 * in the transaction struct, so we can use
1497 * this information later */
1498 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1499 sizeof(trans->bearer_cap));
1500 }
1501
1502 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1503
1504 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1505}
1506
1507static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1508{
1509 struct gsm_mncc *modify = arg;
1510 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1511 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1512
1513 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1514
1515 /* bearer capability */
1516 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1517 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1518
1519 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1520
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001521 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001522}
1523
1524static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1525{
1526 struct gsm48_hdr *gh = msgb_l3(msg);
1527 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1528 struct tlv_parsed tp;
1529 struct gsm_mncc modify;
1530
1531 gsm48_stop_cc_timer(trans);
1532
1533 memset(&modify, 0, sizeof(struct gsm_mncc));
1534 modify.callref = trans->callref;
1535 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1536 /* bearer capability */
1537 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1538 modify.fields |= GSM48_IE_BEARER_CAP;
1539 gsm48_decode_bearer_cap(&modify.bearer_cap,
1540 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1541
1542 /* Create a copy of the bearer capability
1543 * in the transaction struct, so we can use
1544 * this information later */
1545 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1546 sizeof(trans->bearer_cap));
1547 }
1548 /* cause */
1549 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1550 modify.fields |= MNCC_F_CAUSE;
1551 gsm48_decode_cause(&modify.cause,
1552 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1553 }
1554
1555 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1556
1557 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1558}
1559
1560static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1561{
1562 struct gsm_mncc *modify = arg;
1563 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1564 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1565
1566 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1567
1568 /* bearer capability */
1569 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1570 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1571 /* cause */
1572 gsm48_encode_cause(msg, 1, &modify->cause);
1573
1574 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1575
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001576 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001577}
1578
1579static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1580{
1581 struct gsm_mncc *notify = arg;
1582 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1584
1585 gh->msg_type = GSM48_MT_CC_NOTIFY;
1586
1587 /* notify */
1588 gsm48_encode_notify(msg, notify->notify);
1589
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001590 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001591}
1592
1593static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1594{
1595 struct gsm48_hdr *gh = msgb_l3(msg);
1596 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1597// struct tlv_parsed tp;
1598 struct gsm_mncc notify;
1599
1600 memset(&notify, 0, sizeof(struct gsm_mncc));
1601 notify.callref = trans->callref;
1602// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1603 if (payload_len >= 1)
1604 gsm48_decode_notify(&notify.notify, gh->data);
1605
1606 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1607}
1608
1609static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1610{
1611 struct gsm_mncc *user = arg;
1612 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1613 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1614
1615 gh->msg_type = GSM48_MT_CC_USER_INFO;
1616
1617 /* user-user */
1618 if (user->fields & MNCC_F_USERUSER)
1619 gsm48_encode_useruser(msg, 1, &user->useruser);
1620 /* more data */
1621 if (user->more)
1622 gsm48_encode_more(msg);
1623
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001624 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001625}
1626
1627static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1628{
1629 struct gsm48_hdr *gh = msgb_l3(msg);
1630 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1631 struct tlv_parsed tp;
1632 struct gsm_mncc user;
1633
1634 memset(&user, 0, sizeof(struct gsm_mncc));
1635 user.callref = trans->callref;
1636 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1637 /* user-user */
1638 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1639 user.fields |= MNCC_F_USERUSER;
1640 gsm48_decode_useruser(&user.useruser,
1641 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1642 }
1643 /* more data */
1644 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1645 user.more = 1;
1646
1647 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1648}
1649
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001650static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1651 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1652 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001653{
1654 uint8_t data[sizeof(struct gsm_mncc)];
1655 struct gsm_mncc_rtp *rtp;
1656
1657 memset(&data, 0, sizeof(data));
1658 rtp = (struct gsm_mncc_rtp *) &data[0];
1659
1660 rtp->callref = callref;
1661 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001662 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001663 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1664 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001665 }
Harald Welte27989d42018-06-21 20:39:20 +02001666 rtp->payload_type = payload_type;
1667 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001668 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001669}
1670
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001671static 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 +02001672{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001673 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001674}
1675
1676static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1677{
1678 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001679
1680 /* Find callref */
1681 trans = trans_find_by_callref(net, callref);
1682 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001683 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001684 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001685 return -EIO;
1686 }
1687 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001688 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001689 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001690 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001691 return 0;
1692 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001693 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001694
Harald Welte27989d42018-06-21 20:39:20 +02001695 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001696 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001697}
1698
1699/* Trigger TCH_RTP_CREATE acknowledgement */
1700int gsm48_tch_rtp_create(struct gsm_trans *trans)
1701{
1702 /* This function is called as soon as the port, on which the
1703 * mgcp-gw expects the incoming RTP stream from the remote
1704 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001705 struct msc_a *msc_a = trans->msc_a;
1706 struct gsm_network *net = msc_a_net(msc_a);
1707 struct call_leg *cl = msc_a->cc.call_leg;
1708 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001709 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1710 uint32_t payload_type;
1711 int payload_msg_type;
1712 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001713
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001714 if (!rtp_cn) {
1715 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1716 return -EINVAL;
1717 }
1718
1719 if (!rtp_cn->codec_known) {
1720 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1721 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1722 return -EINVAL;
1723 }
1724
1725 /* Codec */
1726 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1727
1728 /* Payload Type number */
1729 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001730 if (mgcp_info && mgcp_info->ptmap_len)
1731 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1732 else
1733 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001734
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001735 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1736 if (!rtp_cn_local) {
1737 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1738 return -EINVAL;
1739 }
1740
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001741 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 +02001742}
1743
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001744static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001745{
1746 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001747 struct call_leg *cl;
1748 struct rtp_stream *rtps;
1749 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001750 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001751
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001752 /* FIXME: in *rtp we should get the codec information of the remote
1753 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1754 * meaningful value based on this information but unfortunately we
1755 * can't do that yet because the mncc API can not signal dynamic
1756 * payload types yet. This must be fixed first. Also there may be
1757 * additional members necessary in trans->conn->rtp because we
1758 * somehow need to deal with dynamic payload types that do not
1759 * comply to 3gpp's assumptions of payload type numbers on the A
1760 * interface. See also related tickets: OS#3399 and OS1683 */
1761
Harald Welte27989d42018-06-21 20:39:20 +02001762 /* Find callref */
1763 trans = trans_find_by_callref(net, rtp->callref);
1764 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001765 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001766 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001767 return -EIO;
1768 }
1769 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001770 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001771 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001772 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001773 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001774 }
1775
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001776 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(MNCC_RTP_CONNECT),
1777 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1778 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001779
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001780 cl = trans->msc_a->cc.call_leg;
1781 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1782
1783 if (!rtps) {
1784 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1785 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1786 return -EINVAL;
1787 }
1788
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001789 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1790 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1791 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1792 return -EINVAL;
1793 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001794 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1795 rtp_stream_commit(rtps);
1796 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001797}
1798
1799static struct downstate {
1800 uint32_t states;
1801 int type;
1802 int (*rout) (struct gsm_trans *trans, void *arg);
1803} downstatelist[] = {
1804 /* mobile originating call establishment */
1805 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1806 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1807 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1808 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1809 {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 */
1810 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1811 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1812 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1813 /* mobile terminating call establishment */
1814 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1815 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1816 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1817 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1818 /* signalling during call */
1819 {SBIT(GSM_CSTATE_ACTIVE),
1820 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1821 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1822 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1823 {ALL_STATES,
1824 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1825 {ALL_STATES,
1826 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1827 {ALL_STATES,
1828 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1829 {SBIT(GSM_CSTATE_ACTIVE),
1830 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1831 {SBIT(GSM_CSTATE_ACTIVE),
1832 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1833 {SBIT(GSM_CSTATE_ACTIVE),
1834 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1835 {SBIT(GSM_CSTATE_ACTIVE),
1836 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1837 {SBIT(GSM_CSTATE_ACTIVE),
1838 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1839 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1840 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1841 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1842 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1843 {SBIT(GSM_CSTATE_ACTIVE),
1844 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1845 /* clearing */
1846 {SBIT(GSM_CSTATE_INITIATED),
1847 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1848 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1849 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1850 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1851 MNCC_REL_REQ, gsm48_cc_tx_release},
1852};
1853
1854#define DOWNSLLEN \
1855 (sizeof(downstatelist) / sizeof(struct downstate))
1856
1857
Philipp Maiercd64af72019-08-01 09:46:40 +02001858static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001859{
1860 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001861 struct msc_a *msc_a = NULL;
1862 struct gsm_trans *trans = NULL;
1863 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001864
Harald Welte27989d42018-06-21 20:39:20 +02001865 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001866 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001867 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001868 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001869 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001870 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001871 return rc;
1872 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001873 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001874 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001875 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001876 case MNCC_RTP_FREE:
1877 /* unused right now */
1878 return -EIO;
1879
1880 case MNCC_FRAME_DROP:
1881 case MNCC_FRAME_RECV:
1882 case GSM_TCHF_FRAME:
1883 case GSM_TCHF_FRAME_EFR:
1884 case GSM_TCHH_FRAME:
1885 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001886 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001887 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001888 return -ENOTSUP;
1889 }
1890
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001891 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001892
1893 /* Find callref */
1894 trans = trans_find_by_callref(net, data->callref);
1895
1896 /* Callref unknown */
1897 if (!trans) {
1898 struct vlr_subscr *vsub;
1899
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001900 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001901 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001903 /* Invalid call reference */
1904 return mncc_release_ind(net, NULL, data->callref,
1905 GSM48_CAUSE_LOC_PRN_S_LU,
1906 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1907 }
1908 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001909 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001910 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001911 /* Invalid number */
1912 return mncc_release_ind(net, NULL, data->callref,
1913 GSM48_CAUSE_LOC_PRN_S_LU,
1914 GSM48_CC_CAUSE_INV_NR_FORMAT);
1915 }
1916 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001917 if (data->called.number[0]) {
1918 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1919 if (!vsub)
1920 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001921 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001922 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001923 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001924 if (!vsub)
1925 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001926 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001927 }
1928 if (!vsub)
1929 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001930 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001931 /* update the subscriber we deal with */
1932 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1933
Harald Welte27989d42018-06-21 20:39:20 +02001934 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001935 if (!vsub->lu_complete) {
1936 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001937 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001938 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001939 /* Temporarily out of order */
1940 return mncc_release_ind(net, NULL, data->callref,
1941 GSM48_CAUSE_LOC_PRN_S_LU,
1942 GSM48_CC_CAUSE_DEST_OOO);
1943 }
Keith Whyte991bb422019-08-08 15:43:40 +02001944
1945 /* Find valid conn */
1946 msc_a = msc_a_for_vsub(vsub, true);
1947
1948 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1949 if (!net->call_waiting && msc_a) {
1950 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1951 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1952 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1953 "rx '%s' for subscriber %s with trans state (%s)"
1954 " rejecting with USER_BUSY\n",
1955 get_mncc_name(msg->msg_type), data->called.number,
1956 gsm48_cc_state_name(existing_cc_trans->cc.state));
1957 return mncc_release_ind(net, NULL, data->callref,
1958 GSM48_CAUSE_LOC_PRN_S_LU,
1959 GSM48_CC_CAUSE_USER_BUSY);
1960 }
1961 }
1962
Harald Welte27989d42018-06-21 20:39:20 +02001963 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001964 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001965 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001966 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001967 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001968 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001969 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001970 mncc_release_ind(net, NULL, data->callref,
1971 GSM48_CAUSE_LOC_PRN_S_LU,
1972 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1973 return -ENOMEM;
1974 }
1975
Harald Welte27989d42018-06-21 20:39:20 +02001976 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001977 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001978 /* This condition will return before the common logging of the received MNCC message below, so
1979 * log it now. */
1980 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001981
Harald Welte27989d42018-06-21 20:39:20 +02001982 /* store setup information until paging succeeds */
1983 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1984
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001985 /* Request a channel. If Paging already started, paging_request_start() will append the new
1986 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001987 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1988 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001989 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001990 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001991 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001992 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001993 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001994 return 0;
1995 }
1996
1997 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001998 trans->msc_a = msc_a;
1999 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002000 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002001 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002002 } else {
2003 /* update the subscriber we deal with */
2004 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2005 }
2006
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002007 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002008
Philipp Maier9ca7b312018-10-10 17:00:49 +02002009 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002010 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002011
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002012 if (trans->msc_a)
2013 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002014
2015 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016 if (!msc_a) {
2017 struct gsm_mncc rel = {
2018 .callref = data->callref,
2019 };
2020 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002021 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2022 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002023 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002024 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2025 else
2026 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2027 trans->callref = 0;
2028 trans_free(trans);
2029 return rc;
2030 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002031 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002032 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002033 }
2034
2035 /* Find function for current state and message */
2036 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002037 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002038 && ((1 << trans->cc.state) & downstatelist[i].states))
2039 break;
2040 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002041 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002042 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002043 return 0;
2044 }
2045
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002046 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002047
2048 return rc;
2049}
2050
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002051struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2052{
2053 uint32_t callref;
2054
2055 switch (msg->msg_type) {
2056 case MNCC_BRIDGE:
2057 callref = msg->bridge.callref[0];
2058 break;
2059 case MNCC_RTP_CREATE:
2060 case MNCC_RTP_CONNECT:
2061 callref = msg->rtp.callref;
2062 break;
2063
2064 case MNCC_RTP_FREE:
2065 case MNCC_FRAME_DROP:
2066 case MNCC_FRAME_RECV:
2067 case GSM_TCHF_FRAME:
2068 case GSM_TCHF_FRAME_EFR:
2069 case GSM_TCHH_FRAME:
2070 case GSM_TCH_FRAME_AMR:
2071 return NULL;
2072
2073 default:
2074 callref = msg->signal.callref;
2075 break;
2076 }
2077
2078 return mncc_call_find_by_callref(callref);
2079}
2080
2081/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002082int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002083{
2084 const union mncc_msg *msg = arg;
2085 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002086
2087 if (msg->msg_type == MNCC_SETUP_REQ) {
2088 /* Incoming call to forward for inter-MSC Handover? */
2089 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2090 if (mncc_call)
2091 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2092 "Incoming call matches pending inter-MSC Handover Number\n");
2093 }
2094 if (!mncc_call) {
2095 /* Find already active MNCC FSM for this callref.
2096 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2097 * MNCC<->GSM-CC call handling. */
2098 mncc_call = mncc_find_by_callref_from_msg(msg);
2099 }
2100 if (mncc_call) {
2101 mncc_call_rx(mncc_call, msg);
2102 return 0;
2103 }
2104
2105 /* None of the above? Then it must be a normal GSM CC call related message. */
2106 return mncc_tx_to_gsm_cc(net, msg);
2107}
Harald Welte27989d42018-06-21 20:39:20 +02002108
2109static struct datastate {
2110 uint32_t states;
2111 int type;
2112 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2113} datastatelist[] = {
2114 /* mobile originating call establishment */
2115 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2116 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2117 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2118 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2119 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2120 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2121 /* mobile terminating call establishment */
2122 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2123 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2124 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2125 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2126 {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 */
2127 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2128 /* signalling during call */
2129 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2130 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2131 {SBIT(GSM_CSTATE_ACTIVE),
2132 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2133 {ALL_STATES,
2134 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2135 {ALL_STATES,
2136 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2137 {ALL_STATES,
2138 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2139 {SBIT(GSM_CSTATE_ACTIVE),
2140 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2141 {SBIT(GSM_CSTATE_ACTIVE),
2142 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2143 {SBIT(GSM_CSTATE_ACTIVE),
2144 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2145 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2146 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2147 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2148 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2149 {SBIT(GSM_CSTATE_ACTIVE),
2150 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2151 /* clearing */
2152 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2153 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2154 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2155 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2156 {ALL_STATES, /* 5.4.3.4 */
2157 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2158};
2159
2160#define DATASLLEN \
2161 (sizeof(datastatelist) / sizeof(struct datastate))
2162
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002163int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002164{
2165 struct gsm48_hdr *gh = msgb_l3(msg);
2166 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2167 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2168 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002169 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2170 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002171 int i, rc = 0;
2172
2173 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002174 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002175 return -EINVAL;
2176 }
2177
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002178 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002179 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002180 return -EINVAL;
2181 }
2182
2183 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002184 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002185
Harald Welte27989d42018-06-21 20:39:20 +02002186 /* Create transaction */
2187 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002188 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002189 trans = trans_alloc(net, vsub,
2190 TRANS_CC,
2191 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002192 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002193 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002194 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002195 GSM48_PDISC_CC | (transaction_id << 4),
2196 GSM48_MT_CC_RELEASE_COMPL);
2197 return -ENOMEM;
2198 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002199 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2200 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2201 trans_free(trans);
2202 return -EINVAL;
2203 }
2204
Harald Welte27989d42018-06-21 20:39:20 +02002205 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002206 msc_a_get(msc_a, MSC_A_USE_CC);
2207 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002208 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002209
2210 /* An earlier CM Service Request for this CC message now has concluded */
2211 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2212 LOG_MSC_A(msc_a, LOGL_ERROR,
2213 "Creating new CC transaction without prior CM Service Request\n");
2214 else
2215 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002216 }
2217
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002218 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2219 gsm48_cc_state_name(trans->cc.state));
2220
Harald Welte27989d42018-06-21 20:39:20 +02002221 /* find function for current state and message */
2222 for (i = 0; i < DATASLLEN; i++)
2223 if ((msg_type == datastatelist[i].type)
2224 && ((1 << trans->cc.state) & datastatelist[i].states))
2225 break;
2226 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002227 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002228
2229 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2230 * transaction right away. */
2231 if (trans->cc.state == GSM_CSTATE_NULL) {
2232 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2233 " -- disarding new CC transaction right away\n");
2234 trans_free(trans);
2235 }
Harald Welte27989d42018-06-21 20:39:20 +02002236 return 0;
2237 }
2238
2239 assert(trans->vsub);
2240
2241 rc = datastatelist[i].rout(trans, msg);
2242
Harald Welte27989d42018-06-21 20:39:20 +02002243 return rc;
2244}