blob: 281a386accd189f26d9463fefdcfe02a4540ab3c [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 */
730 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
731 sizeof(trans->bearer_cap));
732 }
733 /* cause */
734 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
735 call_conf.fields |= MNCC_F_CAUSE;
736 gsm48_decode_cause(&call_conf.cause,
737 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
738 }
739 /* cc cap */
740 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
741 call_conf.fields |= MNCC_F_CCCAP;
742 gsm48_decode_cccap(&call_conf.cccap,
743 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
744 }
745
746 /* IMSI of called subscriber */
747 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
748
749 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
750
751 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100752 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200753
754 /* don't continue, if there were problems with
755 * the call assignment. */
756 if (rc)
757 return rc;
758
759 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
760 &call_conf);
761}
762
763static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
764{
765 struct gsm_mncc *proceeding = arg;
766 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
767 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
768 int rc;
769
770 gh->msg_type = GSM48_MT_CC_CALL_PROC;
771
772 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
773
774 /* bearer capability */
775 if (proceeding->fields & MNCC_F_BEARER_CAP) {
776 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
777 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
778 }
779 /* facility */
780 if (proceeding->fields & MNCC_F_FACILITY)
781 gsm48_encode_facility(msg, 0, &proceeding->facility);
782 /* progress */
783 if (proceeding->fields & MNCC_F_PROGRESS)
784 gsm48_encode_progress(msg, 0, &proceeding->progress);
785
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100786 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200787 if (rc)
788 return rc;
789
790 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100791 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200792}
793
794static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
795{
796 struct gsm48_hdr *gh = msgb_l3(msg);
797 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
798 struct tlv_parsed tp;
799 struct gsm_mncc alerting;
800
801 gsm48_stop_cc_timer(trans);
802 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
803
804 memset(&alerting, 0, sizeof(struct gsm_mncc));
805 alerting.callref = trans->callref;
806 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
807 /* facility */
808 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
809 alerting.fields |= MNCC_F_FACILITY;
810 gsm48_decode_facility(&alerting.facility,
811 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
812 }
813
814 /* progress */
815 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
816 alerting.fields |= MNCC_F_PROGRESS;
817 gsm48_decode_progress(&alerting.progress,
818 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
819 }
820 /* ss-version */
821 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
822 alerting.fields |= MNCC_F_SSVERSION;
823 gsm48_decode_ssversion(&alerting.ssversion,
824 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
825 }
826
827 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
828
829 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
830 &alerting);
831}
832
833static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
834{
835 struct gsm_mncc *alerting = arg;
836 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
837 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
838
839 gh->msg_type = GSM48_MT_CC_ALERTING;
840
841 /* facility */
842 if (alerting->fields & MNCC_F_FACILITY)
843 gsm48_encode_facility(msg, 0, &alerting->facility);
844 /* progress */
845 if (alerting->fields & MNCC_F_PROGRESS)
846 gsm48_encode_progress(msg, 0, &alerting->progress);
847 /* user-user */
848 if (alerting->fields & MNCC_F_USERUSER)
849 gsm48_encode_useruser(msg, 0, &alerting->useruser);
850
851 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
852
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100853 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200854}
855
856static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
857{
858 struct gsm_mncc *progress = arg;
859 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
860 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
861
862 gh->msg_type = GSM48_MT_CC_PROGRESS;
863
864 /* progress */
865 gsm48_encode_progress(msg, 1, &progress->progress);
866 /* user-user */
867 if (progress->fields & MNCC_F_USERUSER)
868 gsm48_encode_useruser(msg, 0, &progress->useruser);
869
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100870 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200871}
872
873static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
874{
875 struct gsm_mncc *connect = arg;
876 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
877 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
878
879 gh->msg_type = GSM48_MT_CC_CONNECT;
880
881 gsm48_stop_cc_timer(trans);
882 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
883
884 /* facility */
885 if (connect->fields & MNCC_F_FACILITY)
886 gsm48_encode_facility(msg, 0, &connect->facility);
887 /* progress */
888 if (connect->fields & MNCC_F_PROGRESS)
889 gsm48_encode_progress(msg, 0, &connect->progress);
890 /* connected number */
891 if (connect->fields & MNCC_F_CONNECTED)
892 gsm48_encode_connected(msg, &connect->connected);
893 /* user-user */
894 if (connect->fields & MNCC_F_USERUSER)
895 gsm48_encode_useruser(msg, 0, &connect->useruser);
896
897 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
898
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100899 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200900}
901
902static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
903{
904 struct gsm48_hdr *gh = msgb_l3(msg);
905 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
906 struct tlv_parsed tp;
907 struct gsm_mncc connect;
908
909 gsm48_stop_cc_timer(trans);
910
911 memset(&connect, 0, sizeof(struct gsm_mncc));
912 connect.callref = trans->callref;
913 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
914 /* use subscriber as connected party number */
915 connect.fields |= MNCC_F_CONNECTED;
916 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
917 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
918
919 /* facility */
920 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
921 connect.fields |= MNCC_F_FACILITY;
922 gsm48_decode_facility(&connect.facility,
923 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
924 }
925 /* user-user */
926 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
927 connect.fields |= MNCC_F_USERUSER;
928 gsm48_decode_useruser(&connect.useruser,
929 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
930 }
931 /* ss-version */
932 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
933 connect.fields |= MNCC_F_SSVERSION;
934 gsm48_decode_ssversion(&connect.ssversion,
935 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
936 }
937
938 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200939 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200940
941 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
942}
943
944
945static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
946{
947 struct gsm_mncc connect_ack;
948
949 gsm48_stop_cc_timer(trans);
950
951 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200952 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 +0200953
954 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
955 connect_ack.callref = trans->callref;
956
957 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
958 &connect_ack);
959}
960
961static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
962{
963 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
964 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
965
966 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
967
968 new_cc_state(trans, GSM_CSTATE_ACTIVE);
969
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100970 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200971}
972
973static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
974{
975 struct gsm48_hdr *gh = msgb_l3(msg);
976 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
977 struct tlv_parsed tp;
978 struct gsm_mncc disc;
979
980 gsm48_stop_cc_timer(trans);
981
982 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
983
984 memset(&disc, 0, sizeof(struct gsm_mncc));
985 disc.callref = trans->callref;
986 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
987 /* cause */
988 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
989 disc.fields |= MNCC_F_CAUSE;
990 gsm48_decode_cause(&disc.cause,
991 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
992 }
993 /* facility */
994 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
995 disc.fields |= MNCC_F_FACILITY;
996 gsm48_decode_facility(&disc.facility,
997 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
998 }
999 /* user-user */
1000 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1001 disc.fields |= MNCC_F_USERUSER;
1002 gsm48_decode_useruser(&disc.useruser,
1003 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1004 }
1005 /* ss-version */
1006 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1007 disc.fields |= MNCC_F_SSVERSION;
1008 gsm48_decode_ssversion(&disc.ssversion,
1009 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1010 }
1011
1012 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001013}
1014
1015static struct gsm_mncc_cause default_cause = {
1016 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1017 .coding = 0,
1018 .rec = 0,
1019 .rec_val = 0,
1020 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1021 .diag_len = 0,
1022 .diag = { 0 },
1023};
1024
1025static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1026{
1027 struct gsm_mncc *disc = arg;
1028 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1029 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1030
1031 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1032
1033 gsm48_stop_cc_timer(trans);
1034 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1035
1036 /* cause */
1037 if (disc->fields & MNCC_F_CAUSE)
1038 gsm48_encode_cause(msg, 1, &disc->cause);
1039 else
1040 gsm48_encode_cause(msg, 1, &default_cause);
1041
1042 /* facility */
1043 if (disc->fields & MNCC_F_FACILITY)
1044 gsm48_encode_facility(msg, 0, &disc->facility);
1045 /* progress */
1046 if (disc->fields & MNCC_F_PROGRESS)
1047 gsm48_encode_progress(msg, 0, &disc->progress);
1048 /* user-user */
1049 if (disc->fields & MNCC_F_USERUSER)
1050 gsm48_encode_useruser(msg, 0, &disc->useruser);
1051
1052 /* store disconnect cause for T306 expiry */
1053 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1054
1055 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1056
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001057 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001058}
1059
1060static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1061{
1062 struct gsm48_hdr *gh = msgb_l3(msg);
1063 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1064 struct tlv_parsed tp;
1065 struct gsm_mncc rel;
1066 int rc;
1067
1068 gsm48_stop_cc_timer(trans);
1069
1070 memset(&rel, 0, sizeof(struct gsm_mncc));
1071 rel.callref = trans->callref;
1072 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1073 /* cause */
1074 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1075 rel.fields |= MNCC_F_CAUSE;
1076 gsm48_decode_cause(&rel.cause,
1077 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1078 }
1079 /* facility */
1080 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1081 rel.fields |= MNCC_F_FACILITY;
1082 gsm48_decode_facility(&rel.facility,
1083 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1084 }
1085 /* user-user */
1086 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1087 rel.fields |= MNCC_F_USERUSER;
1088 gsm48_decode_useruser(&rel.useruser,
1089 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1090 }
1091 /* ss-version */
1092 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1093 rel.fields |= MNCC_F_SSVERSION;
1094 gsm48_decode_ssversion(&rel.ssversion,
1095 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1096 }
1097
1098 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1099 /* release collision 5.4.5 */
1100 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1101 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001102 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001103 GSM48_PDISC_CC | (trans->transaction_id << 4),
1104 GSM48_MT_CC_RELEASE_COMPL);
1105 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1106 }
1107
1108 new_cc_state(trans, GSM_CSTATE_NULL);
1109
1110 trans->callref = 0;
1111 trans_free(trans);
1112
1113 return rc;
1114}
1115
1116static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1117{
1118 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001119 struct msgb *msg;
1120 struct gsm48_hdr *gh;
1121
1122 if (!trans->msc_a) {
1123 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1124 return -EINVAL;
1125 }
1126
1127 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1128 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001129
1130 gh->msg_type = GSM48_MT_CC_RELEASE;
1131
1132 gsm48_stop_cc_timer(trans);
1133 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1134
1135 /* cause */
1136 if (rel->fields & MNCC_F_CAUSE)
1137 gsm48_encode_cause(msg, 0, &rel->cause);
1138 /* facility */
1139 if (rel->fields & MNCC_F_FACILITY)
1140 gsm48_encode_facility(msg, 0, &rel->facility);
1141 /* user-user */
1142 if (rel->fields & MNCC_F_USERUSER)
1143 gsm48_encode_useruser(msg, 0, &rel->useruser);
1144
1145 trans->cc.T308_second = 0;
1146 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1147
1148 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1149 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1150
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001151 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001152}
1153
1154static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1155{
1156 struct gsm48_hdr *gh = msgb_l3(msg);
1157 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1158 struct tlv_parsed tp;
1159 struct gsm_mncc rel;
1160 int rc = 0;
1161
1162 gsm48_stop_cc_timer(trans);
1163
1164 memset(&rel, 0, sizeof(struct gsm_mncc));
1165 rel.callref = trans->callref;
1166 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1167 /* cause */
1168 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1169 rel.fields |= MNCC_F_CAUSE;
1170 gsm48_decode_cause(&rel.cause,
1171 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1172 }
1173 /* facility */
1174 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1175 rel.fields |= MNCC_F_FACILITY;
1176 gsm48_decode_facility(&rel.facility,
1177 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1178 }
1179 /* user-user */
1180 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1181 rel.fields |= MNCC_F_USERUSER;
1182 gsm48_decode_useruser(&rel.useruser,
1183 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1184 }
1185 /* ss-version */
1186 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1187 rel.fields |= MNCC_F_SSVERSION;
1188 gsm48_decode_ssversion(&rel.ssversion,
1189 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1190 }
1191
1192 if (trans->callref) {
1193 switch (trans->cc.state) {
1194 case GSM_CSTATE_CALL_PRESENT:
1195 rc = mncc_recvmsg(trans->net, trans,
1196 MNCC_REJ_IND, &rel);
1197 break;
1198 case GSM_CSTATE_RELEASE_REQ:
1199 rc = mncc_recvmsg(trans->net, trans,
1200 MNCC_REL_CNF, &rel);
1201 break;
1202 default:
1203 rc = mncc_recvmsg(trans->net, trans,
1204 MNCC_REL_IND, &rel);
1205 }
1206 }
1207
1208 trans->callref = 0;
1209 trans_free(trans);
1210
1211 return rc;
1212}
1213
1214static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1215{
1216 struct gsm_mncc *rel = arg;
1217 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1219 int ret;
1220
1221 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1222
1223 trans->callref = 0;
1224
1225 gsm48_stop_cc_timer(trans);
1226
1227 /* cause */
1228 if (rel->fields & MNCC_F_CAUSE)
1229 gsm48_encode_cause(msg, 0, &rel->cause);
1230 /* facility */
1231 if (rel->fields & MNCC_F_FACILITY)
1232 gsm48_encode_facility(msg, 0, &rel->facility);
1233 /* user-user */
1234 if (rel->fields & MNCC_F_USERUSER)
1235 gsm48_encode_useruser(msg, 0, &rel->useruser);
1236
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001237 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001238
1239 trans_free(trans);
1240
1241 return ret;
1242}
1243
1244static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1245{
1246 struct gsm48_hdr *gh = msgb_l3(msg);
1247 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1248 struct tlv_parsed tp;
1249 struct gsm_mncc fac;
1250
1251 memset(&fac, 0, sizeof(struct gsm_mncc));
1252 fac.callref = trans->callref;
1253 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1254 /* facility */
1255 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1256 fac.fields |= MNCC_F_FACILITY;
1257 gsm48_decode_facility(&fac.facility,
1258 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1259 }
1260 /* ss-version */
1261 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1262 fac.fields |= MNCC_F_SSVERSION;
1263 gsm48_decode_ssversion(&fac.ssversion,
1264 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1265 }
1266
1267 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1268}
1269
1270static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1271{
1272 struct gsm_mncc *fac = arg;
1273 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1274 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1275
1276 gh->msg_type = GSM48_MT_CC_FACILITY;
1277
1278 /* facility */
1279 gsm48_encode_facility(msg, 1, &fac->facility);
1280
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001281 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001282}
1283
1284static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1285{
1286 struct gsm_mncc hold;
1287
1288 memset(&hold, 0, sizeof(struct gsm_mncc));
1289 hold.callref = trans->callref;
1290 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1291}
1292
1293static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1294{
1295 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1296 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1297
1298 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1299
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001300 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001301}
1302
1303static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1304{
1305 struct gsm_mncc *hold_rej = arg;
1306 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1307 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1308
1309 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1310
1311 /* cause */
1312 if (hold_rej->fields & MNCC_F_CAUSE)
1313 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1314 else
1315 gsm48_encode_cause(msg, 1, &default_cause);
1316
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001317 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001318}
1319
1320static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1321{
1322 struct gsm_mncc retrieve;
1323
1324 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1325 retrieve.callref = trans->callref;
1326 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1327 &retrieve);
1328}
1329
1330static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1331{
1332 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1333 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1334
1335 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1336
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001337 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001338}
1339
1340static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1341{
1342 struct gsm_mncc *retrieve_rej = arg;
1343 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1344 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1345
1346 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1347
1348 /* cause */
1349 if (retrieve_rej->fields & MNCC_F_CAUSE)
1350 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1351 else
1352 gsm48_encode_cause(msg, 1, &default_cause);
1353
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001354 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001355}
1356
1357static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1358{
1359 struct gsm48_hdr *gh = msgb_l3(msg);
1360 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1361 struct tlv_parsed tp;
1362 struct gsm_mncc dtmf;
1363
1364 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1365 dtmf.callref = trans->callref;
1366 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1367 /* keypad facility */
1368 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1369 dtmf.fields |= MNCC_F_KEYPAD;
1370 gsm48_decode_keypad(&dtmf.keypad,
1371 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1372 }
1373
1374 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1375}
1376
1377static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1378{
1379 struct gsm_mncc *dtmf = arg;
1380 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1381 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1382
1383 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1384
1385 /* keypad */
1386 if (dtmf->fields & MNCC_F_KEYPAD)
1387 gsm48_encode_keypad(msg, dtmf->keypad);
1388
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001389 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001390}
1391
1392static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1393{
1394 struct gsm_mncc *dtmf = arg;
1395 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1396 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1397
1398 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1399
1400 /* cause */
1401 if (dtmf->fields & MNCC_F_CAUSE)
1402 gsm48_encode_cause(msg, 1, &dtmf->cause);
1403 else
1404 gsm48_encode_cause(msg, 1, &default_cause);
1405
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001406 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001407}
1408
1409static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1410{
1411 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1413
1414 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1415
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001416 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001417}
1418
1419static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1420{
1421 struct gsm_mncc dtmf;
1422
1423 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1424 dtmf.callref = trans->callref;
1425
1426 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1427}
1428
1429static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1430{
1431 struct gsm48_hdr *gh = msgb_l3(msg);
1432 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1433 struct tlv_parsed tp;
1434 struct gsm_mncc modify;
1435
1436 memset(&modify, 0, sizeof(struct gsm_mncc));
1437 modify.callref = trans->callref;
1438 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1439 /* bearer capability */
1440 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1441 modify.fields |= MNCC_F_BEARER_CAP;
1442 gsm48_decode_bearer_cap(&modify.bearer_cap,
1443 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1444
1445 /* Create a copy of the bearer capability
1446 * in the transaction struct, so we can use
1447 * this information later */
1448 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1449 sizeof(trans->bearer_cap));
1450 }
1451
1452 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1453
1454 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1455}
1456
1457static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1458{
1459 struct gsm_mncc *modify = arg;
1460 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1462
1463 gh->msg_type = GSM48_MT_CC_MODIFY;
1464
1465 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1466
1467 /* bearer capability */
1468 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1469 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1470
1471 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1472
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001473 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001474}
1475
1476static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1477{
1478 struct gsm48_hdr *gh = msgb_l3(msg);
1479 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1480 struct tlv_parsed tp;
1481 struct gsm_mncc modify;
1482
1483 gsm48_stop_cc_timer(trans);
1484
1485 memset(&modify, 0, sizeof(struct gsm_mncc));
1486 modify.callref = trans->callref;
1487 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1488 /* bearer capability */
1489 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1490 modify.fields |= MNCC_F_BEARER_CAP;
1491 gsm48_decode_bearer_cap(&modify.bearer_cap,
1492 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1493
1494 /* Create a copy of the bearer capability
1495 * in the transaction struct, so we can use
1496 * this information later */
1497 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1498 sizeof(trans->bearer_cap));
1499 }
1500
1501 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1502
1503 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1504}
1505
1506static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1507{
1508 struct gsm_mncc *modify = arg;
1509 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1510 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1511
1512 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1513
1514 /* bearer capability */
1515 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1516 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1517
1518 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1519
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001520 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001521}
1522
1523static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1524{
1525 struct gsm48_hdr *gh = msgb_l3(msg);
1526 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1527 struct tlv_parsed tp;
1528 struct gsm_mncc modify;
1529
1530 gsm48_stop_cc_timer(trans);
1531
1532 memset(&modify, 0, sizeof(struct gsm_mncc));
1533 modify.callref = trans->callref;
1534 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1535 /* bearer capability */
1536 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1537 modify.fields |= GSM48_IE_BEARER_CAP;
1538 gsm48_decode_bearer_cap(&modify.bearer_cap,
1539 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1540
1541 /* Create a copy of the bearer capability
1542 * in the transaction struct, so we can use
1543 * this information later */
1544 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1545 sizeof(trans->bearer_cap));
1546 }
1547 /* cause */
1548 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1549 modify.fields |= MNCC_F_CAUSE;
1550 gsm48_decode_cause(&modify.cause,
1551 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1552 }
1553
1554 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1555
1556 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1557}
1558
1559static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1560{
1561 struct gsm_mncc *modify = arg;
1562 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1563 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1564
1565 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1566
1567 /* bearer capability */
1568 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1569 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1570 /* cause */
1571 gsm48_encode_cause(msg, 1, &modify->cause);
1572
1573 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1574
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001575 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001576}
1577
1578static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1579{
1580 struct gsm_mncc *notify = arg;
1581 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1582 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1583
1584 gh->msg_type = GSM48_MT_CC_NOTIFY;
1585
1586 /* notify */
1587 gsm48_encode_notify(msg, notify->notify);
1588
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001589 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001590}
1591
1592static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1593{
1594 struct gsm48_hdr *gh = msgb_l3(msg);
1595 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1596// struct tlv_parsed tp;
1597 struct gsm_mncc notify;
1598
1599 memset(&notify, 0, sizeof(struct gsm_mncc));
1600 notify.callref = trans->callref;
1601// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1602 if (payload_len >= 1)
1603 gsm48_decode_notify(&notify.notify, gh->data);
1604
1605 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1606}
1607
1608static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1609{
1610 struct gsm_mncc *user = arg;
1611 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1612 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1613
1614 gh->msg_type = GSM48_MT_CC_USER_INFO;
1615
1616 /* user-user */
1617 if (user->fields & MNCC_F_USERUSER)
1618 gsm48_encode_useruser(msg, 1, &user->useruser);
1619 /* more data */
1620 if (user->more)
1621 gsm48_encode_more(msg);
1622
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001623 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001624}
1625
1626static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1627{
1628 struct gsm48_hdr *gh = msgb_l3(msg);
1629 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1630 struct tlv_parsed tp;
1631 struct gsm_mncc user;
1632
1633 memset(&user, 0, sizeof(struct gsm_mncc));
1634 user.callref = trans->callref;
1635 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1636 /* user-user */
1637 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1638 user.fields |= MNCC_F_USERUSER;
1639 gsm48_decode_useruser(&user.useruser,
1640 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1641 }
1642 /* more data */
1643 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1644 user.more = 1;
1645
1646 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1647}
1648
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001649static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1650 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1651 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001652{
1653 uint8_t data[sizeof(struct gsm_mncc)];
1654 struct gsm_mncc_rtp *rtp;
1655
1656 memset(&data, 0, sizeof(data));
1657 rtp = (struct gsm_mncc_rtp *) &data[0];
1658
1659 rtp->callref = callref;
1660 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001661 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001662 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1663 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001664 }
Harald Welte27989d42018-06-21 20:39:20 +02001665 rtp->payload_type = payload_type;
1666 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001667 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001668}
1669
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001670static 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 +02001671{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001672 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001673}
1674
1675static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1676{
1677 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001678
1679 /* Find callref */
1680 trans = trans_find_by_callref(net, callref);
1681 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001682 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001683 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001684 return -EIO;
1685 }
1686 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001687 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001688 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001689 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001690 return 0;
1691 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001692 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001693
Harald Welte27989d42018-06-21 20:39:20 +02001694 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001695 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001696}
1697
1698/* Trigger TCH_RTP_CREATE acknowledgement */
1699int gsm48_tch_rtp_create(struct gsm_trans *trans)
1700{
1701 /* This function is called as soon as the port, on which the
1702 * mgcp-gw expects the incoming RTP stream from the remote
1703 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001704 struct msc_a *msc_a = trans->msc_a;
1705 struct gsm_network *net = msc_a_net(msc_a);
1706 struct call_leg *cl = msc_a->cc.call_leg;
1707 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001708 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1709 uint32_t payload_type;
1710 int payload_msg_type;
1711 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001712
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001713 if (!rtp_cn) {
1714 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1715 return -EINVAL;
1716 }
1717
1718 if (!rtp_cn->codec_known) {
1719 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1720 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1721 return -EINVAL;
1722 }
1723
1724 /* Codec */
1725 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1726
1727 /* Payload Type number */
1728 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001729 if (mgcp_info && mgcp_info->ptmap_len)
1730 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1731 else
1732 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001733
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001734 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1735 if (!rtp_cn_local) {
1736 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1737 return -EINVAL;
1738 }
1739
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001740 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 +02001741}
1742
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001743static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001744{
1745 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001746 struct call_leg *cl;
1747 struct rtp_stream *rtps;
1748 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001749 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001750
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001751 /* FIXME: in *rtp we should get the codec information of the remote
1752 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1753 * meaningful value based on this information but unfortunately we
1754 * can't do that yet because the mncc API can not signal dynamic
1755 * payload types yet. This must be fixed first. Also there may be
1756 * additional members necessary in trans->conn->rtp because we
1757 * somehow need to deal with dynamic payload types that do not
1758 * comply to 3gpp's assumptions of payload type numbers on the A
1759 * interface. See also related tickets: OS#3399 and OS1683 */
1760
Harald Welte27989d42018-06-21 20:39:20 +02001761 /* Find callref */
1762 trans = trans_find_by_callref(net, rtp->callref);
1763 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001764 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001765 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001766 return -EIO;
1767 }
1768 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001769 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001770 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001771 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001772 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001773 }
1774
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001775 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(MNCC_RTP_CONNECT),
1776 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1777 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001778
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001779 cl = trans->msc_a->cc.call_leg;
1780 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1781
1782 if (!rtps) {
1783 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1784 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1785 return -EINVAL;
1786 }
1787
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001788 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1789 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1790 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1791 return -EINVAL;
1792 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001793 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1794 rtp_stream_commit(rtps);
1795 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001796}
1797
1798static struct downstate {
1799 uint32_t states;
1800 int type;
1801 int (*rout) (struct gsm_trans *trans, void *arg);
1802} downstatelist[] = {
1803 /* mobile originating call establishment */
1804 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1805 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1806 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1807 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1808 {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 */
1809 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1810 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1811 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1812 /* mobile terminating call establishment */
1813 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1814 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1815 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1816 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1817 /* signalling during call */
1818 {SBIT(GSM_CSTATE_ACTIVE),
1819 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1820 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1821 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1822 {ALL_STATES,
1823 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1824 {ALL_STATES,
1825 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1826 {ALL_STATES,
1827 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1828 {SBIT(GSM_CSTATE_ACTIVE),
1829 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1830 {SBIT(GSM_CSTATE_ACTIVE),
1831 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1832 {SBIT(GSM_CSTATE_ACTIVE),
1833 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1834 {SBIT(GSM_CSTATE_ACTIVE),
1835 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1836 {SBIT(GSM_CSTATE_ACTIVE),
1837 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1838 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1839 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1840 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1841 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1842 {SBIT(GSM_CSTATE_ACTIVE),
1843 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1844 /* clearing */
1845 {SBIT(GSM_CSTATE_INITIATED),
1846 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1847 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1848 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1849 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1850 MNCC_REL_REQ, gsm48_cc_tx_release},
1851};
1852
1853#define DOWNSLLEN \
1854 (sizeof(downstatelist) / sizeof(struct downstate))
1855
1856
Philipp Maiercd64af72019-08-01 09:46:40 +02001857static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001858{
1859 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001860 struct msc_a *msc_a = NULL;
1861 struct gsm_trans *trans = NULL;
1862 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001863
Harald Welte27989d42018-06-21 20:39:20 +02001864 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001865 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001866 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001867 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001868 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001869 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001870 return rc;
1871 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001872 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001873 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001874 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001875 case MNCC_RTP_FREE:
1876 /* unused right now */
1877 return -EIO;
1878
1879 case MNCC_FRAME_DROP:
1880 case MNCC_FRAME_RECV:
1881 case GSM_TCHF_FRAME:
1882 case GSM_TCHF_FRAME_EFR:
1883 case GSM_TCHH_FRAME:
1884 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001885 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001886 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001887 return -ENOTSUP;
1888 }
1889
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001890 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001891
1892 /* Find callref */
1893 trans = trans_find_by_callref(net, data->callref);
1894
1895 /* Callref unknown */
1896 if (!trans) {
1897 struct vlr_subscr *vsub;
1898
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001899 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001900 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001901 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001902 /* Invalid call reference */
1903 return mncc_release_ind(net, NULL, data->callref,
1904 GSM48_CAUSE_LOC_PRN_S_LU,
1905 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1906 }
1907 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001908 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001909 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001910 /* Invalid number */
1911 return mncc_release_ind(net, NULL, data->callref,
1912 GSM48_CAUSE_LOC_PRN_S_LU,
1913 GSM48_CC_CAUSE_INV_NR_FORMAT);
1914 }
1915 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001916 if (data->called.number[0]) {
1917 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1918 if (!vsub)
1919 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001920 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001921 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001922 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001923 if (!vsub)
1924 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001925 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001926 }
1927 if (!vsub)
1928 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001929 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001930 /* update the subscriber we deal with */
1931 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1932
Harald Welte27989d42018-06-21 20:39:20 +02001933 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001934 if (!vsub->lu_complete) {
1935 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001936 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001937 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001938 /* Temporarily out of order */
1939 return mncc_release_ind(net, NULL, data->callref,
1940 GSM48_CAUSE_LOC_PRN_S_LU,
1941 GSM48_CC_CAUSE_DEST_OOO);
1942 }
Keith Whyte991bb422019-08-08 15:43:40 +02001943
1944 /* Find valid conn */
1945 msc_a = msc_a_for_vsub(vsub, true);
1946
1947 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1948 if (!net->call_waiting && msc_a) {
1949 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1950 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1951 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1952 "rx '%s' for subscriber %s with trans state (%s)"
1953 " rejecting with USER_BUSY\n",
1954 get_mncc_name(msg->msg_type), data->called.number,
1955 gsm48_cc_state_name(existing_cc_trans->cc.state));
1956 return mncc_release_ind(net, NULL, data->callref,
1957 GSM48_CAUSE_LOC_PRN_S_LU,
1958 GSM48_CC_CAUSE_USER_BUSY);
1959 }
1960 }
1961
Harald Welte27989d42018-06-21 20:39:20 +02001962 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001963 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001964 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001965 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001966 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001967 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001968 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001969 mncc_release_ind(net, NULL, data->callref,
1970 GSM48_CAUSE_LOC_PRN_S_LU,
1971 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1972 return -ENOMEM;
1973 }
1974
Harald Welte27989d42018-06-21 20:39:20 +02001975 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001976 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001977 /* This condition will return before the common logging of the received MNCC message below, so
1978 * log it now. */
1979 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001980
Harald Welte27989d42018-06-21 20:39:20 +02001981 /* store setup information until paging succeeds */
1982 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1983
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001984 /* Request a channel. If Paging already started, paging_request_start() will append the new
1985 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001986 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1987 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001988 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001989 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001990 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001991 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001992 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001993 return 0;
1994 }
1995
1996 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997 trans->msc_a = msc_a;
1998 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001999 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002000 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002001 } else {
2002 /* update the subscriber we deal with */
2003 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2004 }
2005
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002006 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002007
Philipp Maier9ca7b312018-10-10 17:00:49 +02002008 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002009 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002010
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002011 if (trans->msc_a)
2012 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002013
2014 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002015 if (!msc_a) {
2016 struct gsm_mncc rel = {
2017 .callref = data->callref,
2018 };
2019 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002020 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2021 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002022 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002023 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2024 else
2025 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2026 trans->callref = 0;
2027 trans_free(trans);
2028 return rc;
2029 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002030 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002031 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002032 }
2033
2034 /* Find function for current state and message */
2035 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002036 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002037 && ((1 << trans->cc.state) & downstatelist[i].states))
2038 break;
2039 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002040 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002041 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002042 return 0;
2043 }
2044
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002045 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002046
2047 return rc;
2048}
2049
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002050struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2051{
2052 uint32_t callref;
2053
2054 switch (msg->msg_type) {
2055 case MNCC_BRIDGE:
2056 callref = msg->bridge.callref[0];
2057 break;
2058 case MNCC_RTP_CREATE:
2059 case MNCC_RTP_CONNECT:
2060 callref = msg->rtp.callref;
2061 break;
2062
2063 case MNCC_RTP_FREE:
2064 case MNCC_FRAME_DROP:
2065 case MNCC_FRAME_RECV:
2066 case GSM_TCHF_FRAME:
2067 case GSM_TCHF_FRAME_EFR:
2068 case GSM_TCHH_FRAME:
2069 case GSM_TCH_FRAME_AMR:
2070 return NULL;
2071
2072 default:
2073 callref = msg->signal.callref;
2074 break;
2075 }
2076
2077 return mncc_call_find_by_callref(callref);
2078}
2079
2080/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002081int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002082{
2083 const union mncc_msg *msg = arg;
2084 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002085
2086 if (msg->msg_type == MNCC_SETUP_REQ) {
2087 /* Incoming call to forward for inter-MSC Handover? */
2088 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2089 if (mncc_call)
2090 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2091 "Incoming call matches pending inter-MSC Handover Number\n");
2092 }
2093 if (!mncc_call) {
2094 /* Find already active MNCC FSM for this callref.
2095 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2096 * MNCC<->GSM-CC call handling. */
2097 mncc_call = mncc_find_by_callref_from_msg(msg);
2098 }
2099 if (mncc_call) {
2100 mncc_call_rx(mncc_call, msg);
2101 return 0;
2102 }
2103
2104 /* None of the above? Then it must be a normal GSM CC call related message. */
2105 return mncc_tx_to_gsm_cc(net, msg);
2106}
Harald Welte27989d42018-06-21 20:39:20 +02002107
2108static struct datastate {
2109 uint32_t states;
2110 int type;
2111 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2112} datastatelist[] = {
2113 /* mobile originating call establishment */
2114 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2115 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2116 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2117 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2118 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2119 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2120 /* mobile terminating call establishment */
2121 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2122 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2123 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2124 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2125 {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 */
2126 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2127 /* signalling during call */
2128 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2129 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2130 {SBIT(GSM_CSTATE_ACTIVE),
2131 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2132 {ALL_STATES,
2133 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2134 {ALL_STATES,
2135 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2136 {ALL_STATES,
2137 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2138 {SBIT(GSM_CSTATE_ACTIVE),
2139 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2140 {SBIT(GSM_CSTATE_ACTIVE),
2141 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2142 {SBIT(GSM_CSTATE_ACTIVE),
2143 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2144 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2145 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2146 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2147 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2148 {SBIT(GSM_CSTATE_ACTIVE),
2149 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2150 /* clearing */
2151 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2152 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2153 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2154 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2155 {ALL_STATES, /* 5.4.3.4 */
2156 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2157};
2158
2159#define DATASLLEN \
2160 (sizeof(datastatelist) / sizeof(struct datastate))
2161
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002162int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002163{
2164 struct gsm48_hdr *gh = msgb_l3(msg);
2165 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2166 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2167 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002168 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2169 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002170 int i, rc = 0;
2171
2172 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002173 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002174 return -EINVAL;
2175 }
2176
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002177 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002178 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002179 return -EINVAL;
2180 }
2181
2182 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002183 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002184
Harald Welte27989d42018-06-21 20:39:20 +02002185 /* Create transaction */
2186 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002187 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002188 trans = trans_alloc(net, vsub,
2189 TRANS_CC,
2190 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002191 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002192 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002193 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002194 GSM48_PDISC_CC | (transaction_id << 4),
2195 GSM48_MT_CC_RELEASE_COMPL);
2196 return -ENOMEM;
2197 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002198 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2199 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2200 trans_free(trans);
2201 return -EINVAL;
2202 }
2203
Harald Welte27989d42018-06-21 20:39:20 +02002204 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002205 msc_a_get(msc_a, MSC_A_USE_CC);
2206 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002207 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002208
2209 /* An earlier CM Service Request for this CC message now has concluded */
2210 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2211 LOG_MSC_A(msc_a, LOGL_ERROR,
2212 "Creating new CC transaction without prior CM Service Request\n");
2213 else
2214 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002215 }
2216
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002217 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2218 gsm48_cc_state_name(trans->cc.state));
2219
Harald Welte27989d42018-06-21 20:39:20 +02002220 /* find function for current state and message */
2221 for (i = 0; i < DATASLLEN; i++)
2222 if ((msg_type == datastatelist[i].type)
2223 && ((1 << trans->cc.state) & datastatelist[i].states))
2224 break;
2225 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002226 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002227
2228 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2229 * transaction right away. */
2230 if (trans->cc.state == GSM_CSTATE_NULL) {
2231 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2232 " -- disarding new CC transaction right away\n");
2233 trans_free(trans);
2234 }
Harald Welte27989d42018-06-21 20:39:20 +02002235 return 0;
2236 }
2237
2238 assert(trans->vsub);
2239
2240 rc = datastatelist[i].rout(trans, msg);
2241
Harald Welte27989d42018-06-21 20:39:20 +02002242 return rc;
2243}