blob: 99174de1a2f216e06e60f1522b3a11951323917c [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,
Keith Whyteba4d6822022-07-03 04:12:58 +0100281 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
282 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100283 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
284 }
285
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100286 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
287 * CC Release to the MS if it gets freed here. Hack it to do so. */
288 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
289 struct gsm_mncc rel = {};
290 rel.callref = trans->callref;
291 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
292 gsm48_cc_tx_release(trans, &rel);
293 }
Harald Welte27989d42018-06-21 20:39:20 +0200294 /* This is a final freeing of the transaction. The MNCC release may have triggered the
295 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
296 gsm48_stop_cc_timer(trans);
297 }
298 if (trans->cc.state != GSM_CSTATE_NULL)
299 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200300
301 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100302
303 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
304 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200305}
306
Harald Welte27989d42018-06-21 20:39:20 +0200307/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100308static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200309{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100310 if (trans->msc_a) {
311 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
312 "Handle paging error: transaction already associated with subscriber,"
313 " apparently it was already handled. Skip.\n");
314 return;
Harald Welte27989d42018-06-21 20:39:20 +0200315 }
316
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100317 if (msc_a) {
318 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
319 /* Assign conn */
320 msc_a_get(msc_a, MSC_A_USE_CC);
321 trans->msc_a = msc_a;
322 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200323
324 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300325 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200326 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300327 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
328 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
329 &trans->cc.msg.gcr[0],
330 sizeof(trans->cc.msg.gcr));
331 if (rc < 0)
332 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
333 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200334 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200335 }
336
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100337 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
338 /* send SETUP request to called party */
339 gsm48_cc_tx_setup(trans, &trans->cc.msg);
340 } else {
341 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
342 /* Temporarily out of order */
343 mncc_release_ind(trans->net, trans,
344 trans->callref,
345 GSM48_CAUSE_LOC_PRN_S_LU,
346 GSM48_CC_CAUSE_DEST_OOO);
347 trans->callref = 0;
348 trans->paging_request = NULL;
349 trans_free(trans);
350 }
Harald Welte27989d42018-06-21 20:39:20 +0200351}
352
353/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100354static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200355{
356 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
357 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100358 struct call_leg *cl1;
359 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200360
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100361 if (!trans1 || !trans2) {
362 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200363 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100364 }
Harald Welte27989d42018-06-21 20:39:20 +0200365
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100366 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100367 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
368 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 +0200369 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100370 }
371
372 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
373 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200374
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100375 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
376 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
377 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
378 * If we can't, then we must give up. */
379 cl1 = trans1->msc_a->cc.call_leg;
380 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200381
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100382 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200383}
384
385static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
386{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100387 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200388 return gsm48_cc_tx_status(trans, msg);
389}
390
Harald Welte27989d42018-06-21 20:39:20 +0200391static void gsm48_cc_timeout(void *arg)
392{
393 struct gsm_trans *trans = arg;
394 int disconnect = 0, release = 0;
395 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
396 int mo_location = GSM48_CAUSE_LOC_USER;
397 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
398 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
399 struct gsm_mncc mo_rel, l4_rel;
400
401 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
402 mo_rel.callref = trans->callref;
403 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
404 l4_rel.callref = trans->callref;
405
406 switch(trans->cc.Tcurrent) {
407 case 0x303:
408 release = 1;
409 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
410 break;
411 case 0x310:
412 disconnect = 1;
413 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
414 break;
415 case 0x313:
416 disconnect = 1;
417 /* unknown, did not find it in the specs */
418 break;
419 case 0x301:
420 disconnect = 1;
421 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
422 break;
423 case 0x308:
424 if (!trans->cc.T308_second) {
425 /* restart T308 a second time */
426 gsm48_cc_tx_release(trans, &trans->cc.msg);
427 trans->cc.T308_second = 1;
428 break; /* stay in release state */
429 }
430 trans_free(trans);
431 return;
432 case 0x306:
433 release = 1;
434 mo_cause = trans->cc.msg.cause.value;
435 mo_location = trans->cc.msg.cause.location;
436 break;
437 case 0x323:
438 disconnect = 1;
439 break;
440 default:
441 release = 1;
442 }
443
444 if (release && trans->callref) {
445 /* process release towards layer 4 */
446 mncc_release_ind(trans->net, trans, trans->callref,
447 l4_location, l4_cause);
448 trans->callref = 0;
449 }
450
451 if (disconnect && trans->callref) {
452 /* process disconnect towards layer 4 */
453 mncc_set_cause(&l4_rel, l4_location, l4_cause);
454 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
455 }
456
457 /* process disconnect towards mobile station */
458 if (disconnect || release) {
459 mncc_set_cause(&mo_rel, mo_location, mo_cause);
460 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
461 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
462 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
463 mo_rel.cause.diag_len = 3;
464
465 if (disconnect)
466 gsm48_cc_tx_disconnect(trans, &mo_rel);
467 if (release)
468 gsm48_cc_tx_release(trans, &mo_rel);
469 }
470
471}
472
473/* disconnect both calls from the bridge */
474static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100475 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200476{
477 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
478 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
479 struct gsm_mncc mx_rel;
480 if (!trans0 || !trans1)
481 return;
482
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100483 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
484 trans0->callref, trans1->callref, strerror(err));
485 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200486 trans0->callref, trans1->callref, strerror(err));
487
488 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
489 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
490 GSM48_CC_CAUSE_CHAN_UNACCEPT);
491
492 mx_rel.callref = trans0->callref;
493 gsm48_cc_tx_disconnect(trans0, &mx_rel);
494
495 mx_rel.callref = trans1->callref;
496 gsm48_cc_tx_disconnect(trans1, &mx_rel);
497}
498
499static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
500 int sec, int micro)
501{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100502 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200503 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
504 osmo_timer_schedule(&trans->cc.timer, sec, micro);
505 trans->cc.Tcurrent = current;
506}
507
508static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
509{
510 struct gsm48_hdr *gh = msgb_l3(msg);
511 uint8_t msg_type = gsm48_hdr_msg_type(gh);
512 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
513 struct tlv_parsed tp;
514 struct gsm_mncc setup;
515
Philipp Maier9ca7b312018-10-10 17:00:49 +0200516 gsm48_start_guard_timer(trans);
517
Harald Welte27989d42018-06-21 20:39:20 +0200518 memset(&setup, 0, sizeof(struct gsm_mncc));
519 setup.callref = trans->callref;
520
Keith Whytea1a70be2021-05-16 02:59:52 +0200521 /* New Global Call Reference */
522 if (!trans->cc.lcls)
523 trans->cc.lcls = trans_lcls_compose(trans, true);
524
525 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300526 if (trans->cc.lcls) {
527 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
528 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
529 int rc;
530
531 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
532 memcpy(&setup.gcr[0], gcr_msg->data, rc);
533 setup.fields |= MNCC_F_GCR;
534 } else
535 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
536 msgb_free(gcr_msg);
537 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200538
Harald Welte27989d42018-06-21 20:39:20 +0200539 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
540 /* emergency setup is identified by msg_type */
541 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
542 setup.fields |= MNCC_F_EMERGENCY;
543 setup.emergency = 1;
544 /* use destination number as configured by user (if any) */
545 if (trans->net->emergency.route_to_msisdn) {
546 setup.fields |= MNCC_F_CALLED;
547 setup.called.type = 0; /* unknown */
548 setup.called.plan = 0; /* unknown */
549 OSMO_STRLCPY_ARRAY(setup.called.number,
550 trans->net->emergency.route_to_msisdn);
551 }
552 }
553
554 /* use subscriber as calling party number */
555 setup.fields |= MNCC_F_CALLING;
556 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
557 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
558
559 /* bearer capability */
560 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
561 setup.fields |= MNCC_F_BEARER_CAP;
562 gsm48_decode_bearer_cap(&setup.bearer_cap,
563 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
564
565 /* Create a copy of the bearer capability
566 * in the transaction struct, so we can use
567 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100568 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200569 sizeof(trans->bearer_cap));
570 }
571 /* facility */
572 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
573 setup.fields |= MNCC_F_FACILITY;
574 gsm48_decode_facility(&setup.facility,
575 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
576 }
577 /* called party bcd number */
578 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
579 setup.fields |= MNCC_F_CALLED;
580 gsm48_decode_called(&setup.called,
581 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
582 }
583 /* user-user */
584 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
585 setup.fields |= MNCC_F_USERUSER;
586 gsm48_decode_useruser(&setup.useruser,
587 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
588 }
589 /* ss-version */
590 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
591 setup.fields |= MNCC_F_SSVERSION;
592 gsm48_decode_ssversion(&setup.ssversion,
593 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
594 }
595 /* CLIR suppression */
596 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
597 setup.clir.sup = 1;
598 /* CLIR invocation */
599 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
600 setup.clir.inv = 1;
601 /* cc cap */
602 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
603 setup.fields |= MNCC_F_CCCAP;
604 gsm48_decode_cccap(&setup.cccap,
605 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
606 }
607
608 new_cc_state(trans, GSM_CSTATE_INITIATED);
609
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100610 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
611 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200612
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200613 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200614
615 /* indicate setup to MNCC */
616 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
617
618 /* MNCC code will modify the channel asynchronously, we should
619 * ipaccess-bind only after the modification has been made to the
620 * lchan->tch_mode */
621 return 0;
622}
623
624static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
625{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100626 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200627 struct gsm48_hdr *gh;
628 struct gsm_mncc *setup = arg;
629 int rc, trans_id;
630
631 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
632
633 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700634 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100635 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200636 "This is not allowed!\n");
637 /* Temporarily out of order */
638 rc = mncc_release_ind(trans->net, trans, trans->callref,
639 GSM48_CAUSE_LOC_PRN_S_LU,
640 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
641 trans->callref = 0;
642 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200643 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200644 return rc;
645 }
646
647 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100648 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200649 if (trans_id < 0) {
650 /* no free transaction ID */
651 rc = mncc_release_ind(trans->net, trans, trans->callref,
652 GSM48_CAUSE_LOC_PRN_S_LU,
653 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
654 trans->callref = 0;
655 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200656 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200657 return rc;
658 }
659 trans->transaction_id = trans_id;
660
661 gh->msg_type = GSM48_MT_CC_SETUP;
662
663 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
664
665 /* bearer capability */
666 if (setup->fields & MNCC_F_BEARER_CAP) {
667 /* Create a copy of the bearer capability in the transaction struct, so we
668 * can use this information later */
669 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
670 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
671 }
672 /* facility */
673 if (setup->fields & MNCC_F_FACILITY)
674 gsm48_encode_facility(msg, 0, &setup->facility);
675 /* progress */
676 if (setup->fields & MNCC_F_PROGRESS)
677 gsm48_encode_progress(msg, 0, &setup->progress);
678 /* calling party BCD number */
679 if (setup->fields & MNCC_F_CALLING)
680 gsm48_encode_calling(msg, &setup->calling);
681 /* called party BCD number */
682 if (setup->fields & MNCC_F_CALLED)
683 gsm48_encode_called(msg, &setup->called);
684 /* user-user */
685 if (setup->fields & MNCC_F_USERUSER)
686 gsm48_encode_useruser(msg, 0, &setup->useruser);
687 /* redirecting party BCD number */
688 if (setup->fields & MNCC_F_REDIRECTING)
689 gsm48_encode_redirecting(msg, &setup->redirecting);
690 /* signal */
691 if (setup->fields & MNCC_F_SIGNAL)
692 gsm48_encode_signal(msg, setup->signal);
693
694 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
695
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200696 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200697
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100698 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200699}
700
701static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
702{
703 struct gsm48_hdr *gh = msgb_l3(msg);
704 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
705 struct tlv_parsed tp;
706 struct gsm_mncc call_conf;
707 int rc;
708
709 gsm48_stop_cc_timer(trans);
710 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
711
712 memset(&call_conf, 0, sizeof(struct gsm_mncc));
713 call_conf.callref = trans->callref;
714
715 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
716#if 0
717 /* repeat */
718 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
719 call_conf.repeat = 1;
720 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
721 call_conf.repeat = 2;
722#endif
723 /* bearer capability */
724 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
725 call_conf.fields |= MNCC_F_BEARER_CAP;
726 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
727 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
728
729 /* Create a copy of the bearer capability
730 * in the transaction struct, so we can use
731 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100732 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200733 sizeof(trans->bearer_cap));
734 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100735
Harald Welte27989d42018-06-21 20:39:20 +0200736 /* cause */
737 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
738 call_conf.fields |= MNCC_F_CAUSE;
739 gsm48_decode_cause(&call_conf.cause,
740 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
741 }
742 /* cc cap */
743 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
744 call_conf.fields |= MNCC_F_CCCAP;
745 gsm48_decode_cccap(&call_conf.cccap,
746 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
747 }
748
749 /* IMSI of called subscriber */
750 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
751
752 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
753
754 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100755 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200756
757 /* don't continue, if there were problems with
758 * the call assignment. */
759 if (rc)
760 return rc;
761
762 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
763 &call_conf);
764}
765
766static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
767{
768 struct gsm_mncc *proceeding = arg;
769 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
770 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
771 int rc;
772
773 gh->msg_type = GSM48_MT_CC_CALL_PROC;
774
775 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
776
777 /* bearer capability */
778 if (proceeding->fields & MNCC_F_BEARER_CAP) {
779 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
780 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
781 }
782 /* facility */
783 if (proceeding->fields & MNCC_F_FACILITY)
784 gsm48_encode_facility(msg, 0, &proceeding->facility);
785 /* progress */
786 if (proceeding->fields & MNCC_F_PROGRESS)
787 gsm48_encode_progress(msg, 0, &proceeding->progress);
788
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100789 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200790 if (rc)
791 return rc;
792
793 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100794 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200795}
796
797static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
798{
799 struct gsm48_hdr *gh = msgb_l3(msg);
800 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
801 struct tlv_parsed tp;
802 struct gsm_mncc alerting;
803
804 gsm48_stop_cc_timer(trans);
805 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
806
807 memset(&alerting, 0, sizeof(struct gsm_mncc));
808 alerting.callref = trans->callref;
809 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
810 /* facility */
811 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
812 alerting.fields |= MNCC_F_FACILITY;
813 gsm48_decode_facility(&alerting.facility,
814 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
815 }
816
817 /* progress */
818 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
819 alerting.fields |= MNCC_F_PROGRESS;
820 gsm48_decode_progress(&alerting.progress,
821 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
822 }
823 /* ss-version */
824 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
825 alerting.fields |= MNCC_F_SSVERSION;
826 gsm48_decode_ssversion(&alerting.ssversion,
827 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
828 }
829
830 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
831
832 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
833 &alerting);
834}
835
836static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
837{
838 struct gsm_mncc *alerting = arg;
839 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
841
842 gh->msg_type = GSM48_MT_CC_ALERTING;
843
844 /* facility */
845 if (alerting->fields & MNCC_F_FACILITY)
846 gsm48_encode_facility(msg, 0, &alerting->facility);
847 /* progress */
848 if (alerting->fields & MNCC_F_PROGRESS)
849 gsm48_encode_progress(msg, 0, &alerting->progress);
850 /* user-user */
851 if (alerting->fields & MNCC_F_USERUSER)
852 gsm48_encode_useruser(msg, 0, &alerting->useruser);
853
854 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
855
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100856 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200857}
858
859static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
860{
861 struct gsm_mncc *progress = arg;
862 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
864
865 gh->msg_type = GSM48_MT_CC_PROGRESS;
866
867 /* progress */
868 gsm48_encode_progress(msg, 1, &progress->progress);
869 /* user-user */
870 if (progress->fields & MNCC_F_USERUSER)
871 gsm48_encode_useruser(msg, 0, &progress->useruser);
872
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100873 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200874}
875
876static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
877{
878 struct gsm_mncc *connect = arg;
879 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
880 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
881
882 gh->msg_type = GSM48_MT_CC_CONNECT;
883
884 gsm48_stop_cc_timer(trans);
885 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
886
887 /* facility */
888 if (connect->fields & MNCC_F_FACILITY)
889 gsm48_encode_facility(msg, 0, &connect->facility);
890 /* progress */
891 if (connect->fields & MNCC_F_PROGRESS)
892 gsm48_encode_progress(msg, 0, &connect->progress);
893 /* connected number */
894 if (connect->fields & MNCC_F_CONNECTED)
895 gsm48_encode_connected(msg, &connect->connected);
896 /* user-user */
897 if (connect->fields & MNCC_F_USERUSER)
898 gsm48_encode_useruser(msg, 0, &connect->useruser);
899
900 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
901
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100902 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200903}
904
905static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
906{
907 struct gsm48_hdr *gh = msgb_l3(msg);
908 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
909 struct tlv_parsed tp;
910 struct gsm_mncc connect;
911
912 gsm48_stop_cc_timer(trans);
913
914 memset(&connect, 0, sizeof(struct gsm_mncc));
915 connect.callref = trans->callref;
916 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
917 /* use subscriber as connected party number */
918 connect.fields |= MNCC_F_CONNECTED;
919 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
920 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
921
922 /* facility */
923 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
924 connect.fields |= MNCC_F_FACILITY;
925 gsm48_decode_facility(&connect.facility,
926 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
927 }
928 /* user-user */
929 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
930 connect.fields |= MNCC_F_USERUSER;
931 gsm48_decode_useruser(&connect.useruser,
932 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
933 }
934 /* ss-version */
935 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
936 connect.fields |= MNCC_F_SSVERSION;
937 gsm48_decode_ssversion(&connect.ssversion,
938 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
939 }
940
941 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200942 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200943
944 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
945}
946
947
948static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
949{
950 struct gsm_mncc connect_ack;
951
952 gsm48_stop_cc_timer(trans);
953
954 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200955 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 +0200956
957 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
958 connect_ack.callref = trans->callref;
959
960 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
961 &connect_ack);
962}
963
964static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
965{
966 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
967 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
968
969 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
970
971 new_cc_state(trans, GSM_CSTATE_ACTIVE);
972
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100973 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200974}
975
976static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
977{
978 struct gsm48_hdr *gh = msgb_l3(msg);
979 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
980 struct tlv_parsed tp;
981 struct gsm_mncc disc;
982
983 gsm48_stop_cc_timer(trans);
984
985 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
986
987 memset(&disc, 0, sizeof(struct gsm_mncc));
988 disc.callref = trans->callref;
989 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
990 /* cause */
991 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
992 disc.fields |= MNCC_F_CAUSE;
993 gsm48_decode_cause(&disc.cause,
994 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
995 }
996 /* facility */
997 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
998 disc.fields |= MNCC_F_FACILITY;
999 gsm48_decode_facility(&disc.facility,
1000 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1001 }
1002 /* user-user */
1003 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1004 disc.fields |= MNCC_F_USERUSER;
1005 gsm48_decode_useruser(&disc.useruser,
1006 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1007 }
1008 /* ss-version */
1009 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1010 disc.fields |= MNCC_F_SSVERSION;
1011 gsm48_decode_ssversion(&disc.ssversion,
1012 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1013 }
1014
1015 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001016}
1017
1018static struct gsm_mncc_cause default_cause = {
1019 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1020 .coding = 0,
1021 .rec = 0,
1022 .rec_val = 0,
1023 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1024 .diag_len = 0,
1025 .diag = { 0 },
1026};
1027
1028static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1029{
1030 struct gsm_mncc *disc = arg;
1031 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1032 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1033
1034 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1035
1036 gsm48_stop_cc_timer(trans);
1037 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1038
1039 /* cause */
1040 if (disc->fields & MNCC_F_CAUSE)
1041 gsm48_encode_cause(msg, 1, &disc->cause);
1042 else
1043 gsm48_encode_cause(msg, 1, &default_cause);
1044
1045 /* facility */
1046 if (disc->fields & MNCC_F_FACILITY)
1047 gsm48_encode_facility(msg, 0, &disc->facility);
1048 /* progress */
1049 if (disc->fields & MNCC_F_PROGRESS)
1050 gsm48_encode_progress(msg, 0, &disc->progress);
1051 /* user-user */
1052 if (disc->fields & MNCC_F_USERUSER)
1053 gsm48_encode_useruser(msg, 0, &disc->useruser);
1054
1055 /* store disconnect cause for T306 expiry */
1056 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1057
1058 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1059
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001060 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001061}
1062
1063static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1064{
1065 struct gsm48_hdr *gh = msgb_l3(msg);
1066 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1067 struct tlv_parsed tp;
1068 struct gsm_mncc rel;
1069 int rc;
1070
1071 gsm48_stop_cc_timer(trans);
1072
1073 memset(&rel, 0, sizeof(struct gsm_mncc));
1074 rel.callref = trans->callref;
1075 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1076 /* cause */
1077 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1078 rel.fields |= MNCC_F_CAUSE;
1079 gsm48_decode_cause(&rel.cause,
1080 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1081 }
1082 /* facility */
1083 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1084 rel.fields |= MNCC_F_FACILITY;
1085 gsm48_decode_facility(&rel.facility,
1086 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1087 }
1088 /* user-user */
1089 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1090 rel.fields |= MNCC_F_USERUSER;
1091 gsm48_decode_useruser(&rel.useruser,
1092 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1093 }
1094 /* ss-version */
1095 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1096 rel.fields |= MNCC_F_SSVERSION;
1097 gsm48_decode_ssversion(&rel.ssversion,
1098 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1099 }
1100
1101 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1102 /* release collision 5.4.5 */
1103 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1104 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001105 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001106 GSM48_PDISC_CC | (trans->transaction_id << 4),
1107 GSM48_MT_CC_RELEASE_COMPL);
1108 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1109 }
1110
1111 new_cc_state(trans, GSM_CSTATE_NULL);
1112
1113 trans->callref = 0;
1114 trans_free(trans);
1115
1116 return rc;
1117}
1118
1119static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1120{
1121 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001122 struct msgb *msg;
1123 struct gsm48_hdr *gh;
1124
1125 if (!trans->msc_a) {
1126 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1127 return -EINVAL;
1128 }
1129
1130 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1131 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001132
1133 gh->msg_type = GSM48_MT_CC_RELEASE;
1134
1135 gsm48_stop_cc_timer(trans);
1136 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1137
1138 /* cause */
1139 if (rel->fields & MNCC_F_CAUSE)
1140 gsm48_encode_cause(msg, 0, &rel->cause);
1141 /* facility */
1142 if (rel->fields & MNCC_F_FACILITY)
1143 gsm48_encode_facility(msg, 0, &rel->facility);
1144 /* user-user */
1145 if (rel->fields & MNCC_F_USERUSER)
1146 gsm48_encode_useruser(msg, 0, &rel->useruser);
1147
1148 trans->cc.T308_second = 0;
1149 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1150
1151 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1152 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1153
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001154 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001155}
1156
1157static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1158{
1159 struct gsm48_hdr *gh = msgb_l3(msg);
1160 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1161 struct tlv_parsed tp;
1162 struct gsm_mncc rel;
1163 int rc = 0;
1164
1165 gsm48_stop_cc_timer(trans);
1166
1167 memset(&rel, 0, sizeof(struct gsm_mncc));
1168 rel.callref = trans->callref;
1169 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1170 /* cause */
1171 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1172 rel.fields |= MNCC_F_CAUSE;
1173 gsm48_decode_cause(&rel.cause,
1174 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1175 }
1176 /* facility */
1177 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1178 rel.fields |= MNCC_F_FACILITY;
1179 gsm48_decode_facility(&rel.facility,
1180 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1181 }
1182 /* user-user */
1183 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1184 rel.fields |= MNCC_F_USERUSER;
1185 gsm48_decode_useruser(&rel.useruser,
1186 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1187 }
1188 /* ss-version */
1189 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1190 rel.fields |= MNCC_F_SSVERSION;
1191 gsm48_decode_ssversion(&rel.ssversion,
1192 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1193 }
1194
1195 if (trans->callref) {
1196 switch (trans->cc.state) {
1197 case GSM_CSTATE_CALL_PRESENT:
1198 rc = mncc_recvmsg(trans->net, trans,
1199 MNCC_REJ_IND, &rel);
1200 break;
1201 case GSM_CSTATE_RELEASE_REQ:
1202 rc = mncc_recvmsg(trans->net, trans,
1203 MNCC_REL_CNF, &rel);
1204 break;
1205 default:
1206 rc = mncc_recvmsg(trans->net, trans,
1207 MNCC_REL_IND, &rel);
1208 }
1209 }
1210
1211 trans->callref = 0;
1212 trans_free(trans);
1213
1214 return rc;
1215}
1216
1217static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1218{
1219 struct gsm_mncc *rel = arg;
1220 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1221 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1222 int ret;
1223
1224 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1225
1226 trans->callref = 0;
1227
1228 gsm48_stop_cc_timer(trans);
1229
1230 /* cause */
1231 if (rel->fields & MNCC_F_CAUSE)
1232 gsm48_encode_cause(msg, 0, &rel->cause);
1233 /* facility */
1234 if (rel->fields & MNCC_F_FACILITY)
1235 gsm48_encode_facility(msg, 0, &rel->facility);
1236 /* user-user */
1237 if (rel->fields & MNCC_F_USERUSER)
1238 gsm48_encode_useruser(msg, 0, &rel->useruser);
1239
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001240 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001241
1242 trans_free(trans);
1243
1244 return ret;
1245}
1246
1247static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1248{
1249 struct gsm48_hdr *gh = msgb_l3(msg);
1250 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1251 struct tlv_parsed tp;
1252 struct gsm_mncc fac;
1253
1254 memset(&fac, 0, sizeof(struct gsm_mncc));
1255 fac.callref = trans->callref;
1256 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1257 /* facility */
1258 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1259 fac.fields |= MNCC_F_FACILITY;
1260 gsm48_decode_facility(&fac.facility,
1261 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1262 }
1263 /* ss-version */
1264 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1265 fac.fields |= MNCC_F_SSVERSION;
1266 gsm48_decode_ssversion(&fac.ssversion,
1267 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1268 }
1269
1270 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1271}
1272
1273static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1274{
1275 struct gsm_mncc *fac = arg;
1276 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1277 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1278
1279 gh->msg_type = GSM48_MT_CC_FACILITY;
1280
1281 /* facility */
1282 gsm48_encode_facility(msg, 1, &fac->facility);
1283
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001284 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001285}
1286
1287static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1288{
1289 struct gsm_mncc hold;
1290
1291 memset(&hold, 0, sizeof(struct gsm_mncc));
1292 hold.callref = trans->callref;
1293 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1294}
1295
1296static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1297{
1298 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1299 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1300
1301 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1302
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001303 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001304}
1305
1306static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1307{
1308 struct gsm_mncc *hold_rej = arg;
1309 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1310 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1311
1312 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1313
1314 /* cause */
1315 if (hold_rej->fields & MNCC_F_CAUSE)
1316 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1317 else
1318 gsm48_encode_cause(msg, 1, &default_cause);
1319
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001320 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001321}
1322
1323static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1324{
1325 struct gsm_mncc retrieve;
1326
1327 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1328 retrieve.callref = trans->callref;
1329 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1330 &retrieve);
1331}
1332
1333static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1334{
1335 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1336 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1337
1338 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1339
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001340 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001341}
1342
1343static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1344{
1345 struct gsm_mncc *retrieve_rej = arg;
1346 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1347 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1348
1349 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1350
1351 /* cause */
1352 if (retrieve_rej->fields & MNCC_F_CAUSE)
1353 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1354 else
1355 gsm48_encode_cause(msg, 1, &default_cause);
1356
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001357 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001358}
1359
1360static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1361{
1362 struct gsm48_hdr *gh = msgb_l3(msg);
1363 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1364 struct tlv_parsed tp;
1365 struct gsm_mncc dtmf;
1366
1367 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1368 dtmf.callref = trans->callref;
1369 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1370 /* keypad facility */
1371 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1372 dtmf.fields |= MNCC_F_KEYPAD;
1373 gsm48_decode_keypad(&dtmf.keypad,
1374 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1375 }
1376
1377 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1378}
1379
1380static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1381{
1382 struct gsm_mncc *dtmf = arg;
1383 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1384 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1385
1386 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1387
1388 /* keypad */
1389 if (dtmf->fields & MNCC_F_KEYPAD)
1390 gsm48_encode_keypad(msg, dtmf->keypad);
1391
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001392 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001393}
1394
1395static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1396{
1397 struct gsm_mncc *dtmf = arg;
1398 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1399 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1400
1401 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1402
1403 /* cause */
1404 if (dtmf->fields & MNCC_F_CAUSE)
1405 gsm48_encode_cause(msg, 1, &dtmf->cause);
1406 else
1407 gsm48_encode_cause(msg, 1, &default_cause);
1408
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001409 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001410}
1411
1412static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1413{
1414 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1416
1417 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1418
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001419 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001420}
1421
1422static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1423{
1424 struct gsm_mncc dtmf;
1425
1426 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1427 dtmf.callref = trans->callref;
1428
1429 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1430}
1431
1432static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1433{
1434 struct gsm48_hdr *gh = msgb_l3(msg);
1435 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1436 struct tlv_parsed tp;
1437 struct gsm_mncc modify;
1438
1439 memset(&modify, 0, sizeof(struct gsm_mncc));
1440 modify.callref = trans->callref;
1441 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1442 /* bearer capability */
1443 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1444 modify.fields |= MNCC_F_BEARER_CAP;
1445 gsm48_decode_bearer_cap(&modify.bearer_cap,
1446 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1447
1448 /* Create a copy of the bearer capability
1449 * in the transaction struct, so we can use
1450 * this information later */
1451 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1452 sizeof(trans->bearer_cap));
1453 }
1454
1455 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1456
1457 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1458}
1459
1460static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1461{
1462 struct gsm_mncc *modify = arg;
1463 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1465
1466 gh->msg_type = GSM48_MT_CC_MODIFY;
1467
1468 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1469
1470 /* bearer capability */
1471 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1472 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1473
1474 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1475
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001476 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001477}
1478
1479static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1480{
1481 struct gsm48_hdr *gh = msgb_l3(msg);
1482 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1483 struct tlv_parsed tp;
1484 struct gsm_mncc modify;
1485
1486 gsm48_stop_cc_timer(trans);
1487
1488 memset(&modify, 0, sizeof(struct gsm_mncc));
1489 modify.callref = trans->callref;
1490 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1491 /* bearer capability */
1492 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1493 modify.fields |= MNCC_F_BEARER_CAP;
1494 gsm48_decode_bearer_cap(&modify.bearer_cap,
1495 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1496
1497 /* Create a copy of the bearer capability
1498 * in the transaction struct, so we can use
1499 * this information later */
1500 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1501 sizeof(trans->bearer_cap));
1502 }
1503
1504 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1505
1506 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1507}
1508
1509static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1510{
1511 struct gsm_mncc *modify = arg;
1512 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1513 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1514
1515 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1516
1517 /* bearer capability */
1518 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1519 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1520
1521 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1522
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001523 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001524}
1525
1526static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1527{
1528 struct gsm48_hdr *gh = msgb_l3(msg);
1529 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1530 struct tlv_parsed tp;
1531 struct gsm_mncc modify;
1532
1533 gsm48_stop_cc_timer(trans);
1534
1535 memset(&modify, 0, sizeof(struct gsm_mncc));
1536 modify.callref = trans->callref;
1537 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1538 /* bearer capability */
1539 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1540 modify.fields |= GSM48_IE_BEARER_CAP;
1541 gsm48_decode_bearer_cap(&modify.bearer_cap,
1542 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1543
1544 /* Create a copy of the bearer capability
1545 * in the transaction struct, so we can use
1546 * this information later */
1547 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1548 sizeof(trans->bearer_cap));
1549 }
1550 /* cause */
1551 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1552 modify.fields |= MNCC_F_CAUSE;
1553 gsm48_decode_cause(&modify.cause,
1554 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1555 }
1556
1557 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1558
1559 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1560}
1561
1562static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1563{
1564 struct gsm_mncc *modify = arg;
1565 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1566 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1567
1568 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1569
1570 /* bearer capability */
1571 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1572 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1573 /* cause */
1574 gsm48_encode_cause(msg, 1, &modify->cause);
1575
1576 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1577
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001578 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001579}
1580
1581static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1582{
1583 struct gsm_mncc *notify = arg;
1584 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1585 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1586
1587 gh->msg_type = GSM48_MT_CC_NOTIFY;
1588
1589 /* notify */
1590 gsm48_encode_notify(msg, notify->notify);
1591
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001592 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001593}
1594
1595static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1596{
1597 struct gsm48_hdr *gh = msgb_l3(msg);
1598 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1599// struct tlv_parsed tp;
1600 struct gsm_mncc notify;
1601
1602 memset(&notify, 0, sizeof(struct gsm_mncc));
1603 notify.callref = trans->callref;
1604// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1605 if (payload_len >= 1)
1606 gsm48_decode_notify(&notify.notify, gh->data);
1607
1608 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1609}
1610
1611static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1612{
1613 struct gsm_mncc *user = arg;
1614 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1615 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1616
1617 gh->msg_type = GSM48_MT_CC_USER_INFO;
1618
1619 /* user-user */
1620 if (user->fields & MNCC_F_USERUSER)
1621 gsm48_encode_useruser(msg, 1, &user->useruser);
1622 /* more data */
1623 if (user->more)
1624 gsm48_encode_more(msg);
1625
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001626 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001627}
1628
1629static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1630{
1631 struct gsm48_hdr *gh = msgb_l3(msg);
1632 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1633 struct tlv_parsed tp;
1634 struct gsm_mncc user;
1635
1636 memset(&user, 0, sizeof(struct gsm_mncc));
1637 user.callref = trans->callref;
1638 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1639 /* user-user */
1640 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1641 user.fields |= MNCC_F_USERUSER;
1642 gsm48_decode_useruser(&user.useruser,
1643 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1644 }
1645 /* more data */
1646 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1647 user.more = 1;
1648
1649 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1650}
1651
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001652static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1653 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1654 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001655{
1656 uint8_t data[sizeof(struct gsm_mncc)];
1657 struct gsm_mncc_rtp *rtp;
1658
1659 memset(&data, 0, sizeof(data));
1660 rtp = (struct gsm_mncc_rtp *) &data[0];
1661
1662 rtp->callref = callref;
1663 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001664 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001665 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1666 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001667 }
Harald Welte27989d42018-06-21 20:39:20 +02001668 rtp->payload_type = payload_type;
1669 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001670 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001671}
1672
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001673static 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 +02001674{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001675 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001676}
1677
1678static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1679{
1680 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001681
1682 /* Find callref */
1683 trans = trans_find_by_callref(net, callref);
1684 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001685 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001686 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001687 return -EIO;
1688 }
1689 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001690 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001691 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001692 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001693 return 0;
1694 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001695 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001696
Harald Welte27989d42018-06-21 20:39:20 +02001697 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001698 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001699}
1700
1701/* Trigger TCH_RTP_CREATE acknowledgement */
1702int gsm48_tch_rtp_create(struct gsm_trans *trans)
1703{
1704 /* This function is called as soon as the port, on which the
1705 * mgcp-gw expects the incoming RTP stream from the remote
1706 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001707 struct msc_a *msc_a = trans->msc_a;
1708 struct gsm_network *net = msc_a_net(msc_a);
1709 struct call_leg *cl = msc_a->cc.call_leg;
1710 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001711 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1712 uint32_t payload_type;
1713 int payload_msg_type;
1714 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001715
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001716 if (!rtp_cn) {
1717 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1718 return -EINVAL;
1719 }
1720
1721 if (!rtp_cn->codec_known) {
1722 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1723 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1724 return -EINVAL;
1725 }
1726
1727 /* Codec */
1728 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1729
1730 /* Payload Type number */
1731 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001732 if (mgcp_info && mgcp_info->ptmap_len)
1733 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1734 else
1735 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001736
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001737 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1738 if (!rtp_cn_local) {
1739 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1740 return -EINVAL;
1741 }
1742
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001743 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 +02001744}
1745
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001746static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001747{
1748 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001749 struct call_leg *cl;
1750 struct rtp_stream *rtps;
1751 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001752 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001753
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001754 /* FIXME: in *rtp we should get the codec information of the remote
1755 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1756 * meaningful value based on this information but unfortunately we
1757 * can't do that yet because the mncc API can not signal dynamic
1758 * payload types yet. This must be fixed first. Also there may be
1759 * additional members necessary in trans->conn->rtp because we
1760 * somehow need to deal with dynamic payload types that do not
1761 * comply to 3gpp's assumptions of payload type numbers on the A
1762 * interface. See also related tickets: OS#3399 and OS1683 */
1763
Harald Welte27989d42018-06-21 20:39:20 +02001764 /* Find callref */
1765 trans = trans_find_by_callref(net, rtp->callref);
1766 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001767 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001768 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001769 return -EIO;
1770 }
1771 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001772 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001773 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001774 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001775 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001776 }
1777
Neels Hofmeyr90933d42022-01-13 20:10:52 +01001778 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(rtp->msg_type),
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001779 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1780 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001781
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001782 cl = trans->msc_a->cc.call_leg;
1783 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1784
1785 if (!rtps) {
1786 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1787 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1788 return -EINVAL;
1789 }
1790
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001791 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1792 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1793 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1794 return -EINVAL;
1795 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001796 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1797 rtp_stream_commit(rtps);
1798 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001799}
1800
1801static struct downstate {
1802 uint32_t states;
1803 int type;
1804 int (*rout) (struct gsm_trans *trans, void *arg);
1805} downstatelist[] = {
1806 /* mobile originating call establishment */
1807 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1808 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1809 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1810 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1811 {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 */
1812 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1813 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1814 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1815 /* mobile terminating call establishment */
1816 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1817 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1818 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1819 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1820 /* signalling during call */
1821 {SBIT(GSM_CSTATE_ACTIVE),
1822 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1823 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1824 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1825 {ALL_STATES,
1826 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1827 {ALL_STATES,
1828 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1829 {ALL_STATES,
1830 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1831 {SBIT(GSM_CSTATE_ACTIVE),
1832 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1833 {SBIT(GSM_CSTATE_ACTIVE),
1834 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1835 {SBIT(GSM_CSTATE_ACTIVE),
1836 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1837 {SBIT(GSM_CSTATE_ACTIVE),
1838 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1839 {SBIT(GSM_CSTATE_ACTIVE),
1840 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1841 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1842 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1843 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1844 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1845 {SBIT(GSM_CSTATE_ACTIVE),
1846 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1847 /* clearing */
1848 {SBIT(GSM_CSTATE_INITIATED),
1849 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1850 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1851 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1852 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1853 MNCC_REL_REQ, gsm48_cc_tx_release},
1854};
1855
1856#define DOWNSLLEN \
1857 (sizeof(downstatelist) / sizeof(struct downstate))
1858
1859
Philipp Maiercd64af72019-08-01 09:46:40 +02001860static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001861{
1862 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001863 struct msc_a *msc_a = NULL;
1864 struct gsm_trans *trans = NULL;
1865 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001866
Harald Welte27989d42018-06-21 20:39:20 +02001867 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001868 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001869 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001870 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001871 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001872 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001873 return rc;
1874 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001875 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001876 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001877 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001878 case MNCC_RTP_FREE:
1879 /* unused right now */
1880 return -EIO;
1881
1882 case MNCC_FRAME_DROP:
1883 case MNCC_FRAME_RECV:
1884 case GSM_TCHF_FRAME:
1885 case GSM_TCHF_FRAME_EFR:
1886 case GSM_TCHH_FRAME:
1887 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001888 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001889 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001890 return -ENOTSUP;
1891 }
1892
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001893 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001894
1895 /* Find callref */
1896 trans = trans_find_by_callref(net, data->callref);
1897
1898 /* Callref unknown */
1899 if (!trans) {
1900 struct vlr_subscr *vsub;
1901
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001903 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001904 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001905 /* Invalid call reference */
1906 return mncc_release_ind(net, NULL, data->callref,
1907 GSM48_CAUSE_LOC_PRN_S_LU,
1908 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1909 }
1910 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001911 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001912 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001913 /* Invalid number */
1914 return mncc_release_ind(net, NULL, data->callref,
1915 GSM48_CAUSE_LOC_PRN_S_LU,
1916 GSM48_CC_CAUSE_INV_NR_FORMAT);
1917 }
1918 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001919 if (data->called.number[0]) {
1920 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1921 if (!vsub)
1922 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001923 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001924 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001925 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001926 if (!vsub)
1927 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001928 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001929 }
1930 if (!vsub)
1931 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001932 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001933 /* update the subscriber we deal with */
1934 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1935
Harald Welte27989d42018-06-21 20:39:20 +02001936 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001937 if (!vsub->lu_complete) {
1938 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001939 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001940 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001941 /* Temporarily out of order */
1942 return mncc_release_ind(net, NULL, data->callref,
1943 GSM48_CAUSE_LOC_PRN_S_LU,
1944 GSM48_CC_CAUSE_DEST_OOO);
1945 }
Keith Whyte991bb422019-08-08 15:43:40 +02001946
1947 /* Find valid conn */
1948 msc_a = msc_a_for_vsub(vsub, true);
1949
1950 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1951 if (!net->call_waiting && msc_a) {
1952 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1953 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1954 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1955 "rx '%s' for subscriber %s with trans state (%s)"
1956 " rejecting with USER_BUSY\n",
1957 get_mncc_name(msg->msg_type), data->called.number,
1958 gsm48_cc_state_name(existing_cc_trans->cc.state));
1959 return mncc_release_ind(net, NULL, data->callref,
1960 GSM48_CAUSE_LOC_PRN_S_LU,
1961 GSM48_CC_CAUSE_USER_BUSY);
1962 }
1963 }
1964
Harald Welte27989d42018-06-21 20:39:20 +02001965 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001966 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001967 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001968 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001969 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001970 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001971 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001972 mncc_release_ind(net, NULL, data->callref,
1973 GSM48_CAUSE_LOC_PRN_S_LU,
1974 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1975 return -ENOMEM;
1976 }
1977
Harald Welte27989d42018-06-21 20:39:20 +02001978 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001979 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001980 /* This condition will return before the common logging of the received MNCC message below, so
1981 * log it now. */
1982 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001983
Harald Welte27989d42018-06-21 20:39:20 +02001984 /* store setup information until paging succeeds */
1985 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1986
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001987 /* Request a channel. If Paging already started, paging_request_start() will append the new
1988 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001989 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1990 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001991 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001992 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001993 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001994 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001995 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001996 return 0;
1997 }
1998
1999 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002000 trans->msc_a = msc_a;
2001 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002002 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002003 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002004 } else {
2005 /* update the subscriber we deal with */
2006 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2007 }
2008
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002009 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002010
Philipp Maier9ca7b312018-10-10 17:00:49 +02002011 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002012 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002013
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002014 if (trans->msc_a)
2015 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002016
2017 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002018 if (!msc_a) {
2019 struct gsm_mncc rel = {
2020 .callref = data->callref,
2021 };
2022 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002023 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2024 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002025 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002026 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2027 else
2028 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2029 trans->callref = 0;
2030 trans_free(trans);
2031 return rc;
2032 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002033 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002034 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002035 }
2036
2037 /* Find function for current state and message */
2038 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002039 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002040 && ((1 << trans->cc.state) & downstatelist[i].states))
2041 break;
2042 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002043 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002044 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002045 return 0;
2046 }
2047
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002048 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002049
2050 return rc;
2051}
2052
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002053struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2054{
2055 uint32_t callref;
2056
2057 switch (msg->msg_type) {
2058 case MNCC_BRIDGE:
2059 callref = msg->bridge.callref[0];
2060 break;
2061 case MNCC_RTP_CREATE:
2062 case MNCC_RTP_CONNECT:
2063 callref = msg->rtp.callref;
2064 break;
2065
2066 case MNCC_RTP_FREE:
2067 case MNCC_FRAME_DROP:
2068 case MNCC_FRAME_RECV:
2069 case GSM_TCHF_FRAME:
2070 case GSM_TCHF_FRAME_EFR:
2071 case GSM_TCHH_FRAME:
2072 case GSM_TCH_FRAME_AMR:
2073 return NULL;
2074
2075 default:
2076 callref = msg->signal.callref;
2077 break;
2078 }
2079
2080 return mncc_call_find_by_callref(callref);
2081}
2082
2083/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002084int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002085{
2086 const union mncc_msg *msg = arg;
2087 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002088
2089 if (msg->msg_type == MNCC_SETUP_REQ) {
2090 /* Incoming call to forward for inter-MSC Handover? */
2091 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2092 if (mncc_call)
2093 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2094 "Incoming call matches pending inter-MSC Handover Number\n");
2095 }
2096 if (!mncc_call) {
2097 /* Find already active MNCC FSM for this callref.
2098 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2099 * MNCC<->GSM-CC call handling. */
2100 mncc_call = mncc_find_by_callref_from_msg(msg);
2101 }
2102 if (mncc_call) {
2103 mncc_call_rx(mncc_call, msg);
2104 return 0;
2105 }
2106
2107 /* None of the above? Then it must be a normal GSM CC call related message. */
2108 return mncc_tx_to_gsm_cc(net, msg);
2109}
Harald Welte27989d42018-06-21 20:39:20 +02002110
2111static struct datastate {
2112 uint32_t states;
2113 int type;
2114 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2115} datastatelist[] = {
2116 /* mobile originating call establishment */
2117 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2118 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2119 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2120 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2121 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2122 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2123 /* mobile terminating call establishment */
2124 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2125 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2126 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2127 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2128 {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 */
2129 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2130 /* signalling during call */
2131 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2132 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2133 {SBIT(GSM_CSTATE_ACTIVE),
2134 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2135 {ALL_STATES,
2136 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2137 {ALL_STATES,
2138 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2139 {ALL_STATES,
2140 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2141 {SBIT(GSM_CSTATE_ACTIVE),
2142 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2143 {SBIT(GSM_CSTATE_ACTIVE),
2144 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2145 {SBIT(GSM_CSTATE_ACTIVE),
2146 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2147 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2148 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2149 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2150 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2151 {SBIT(GSM_CSTATE_ACTIVE),
2152 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2153 /* clearing */
2154 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2155 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2156 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2157 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2158 {ALL_STATES, /* 5.4.3.4 */
2159 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2160};
2161
2162#define DATASLLEN \
2163 (sizeof(datastatelist) / sizeof(struct datastate))
2164
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002165int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002166{
2167 struct gsm48_hdr *gh = msgb_l3(msg);
2168 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2169 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2170 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002171 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2172 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002173 int i, rc = 0;
2174
2175 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002176 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002177 return -EINVAL;
2178 }
2179
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002180 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002181 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002182 return -EINVAL;
2183 }
2184
2185 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002186 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002187
Harald Welte27989d42018-06-21 20:39:20 +02002188 /* Create transaction */
2189 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002190 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002191 trans = trans_alloc(net, vsub,
2192 TRANS_CC,
2193 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002194 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002195 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002196 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002197 GSM48_PDISC_CC | (transaction_id << 4),
2198 GSM48_MT_CC_RELEASE_COMPL);
2199 return -ENOMEM;
2200 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002201 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2202 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2203 trans_free(trans);
2204 return -EINVAL;
2205 }
2206
Harald Welte27989d42018-06-21 20:39:20 +02002207 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002208 msc_a_get(msc_a, MSC_A_USE_CC);
2209 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002210 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002211
2212 /* An earlier CM Service Request for this CC message now has concluded */
2213 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2214 LOG_MSC_A(msc_a, LOGL_ERROR,
2215 "Creating new CC transaction without prior CM Service Request\n");
2216 else
2217 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002218 }
2219
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002220 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2221 gsm48_cc_state_name(trans->cc.state));
2222
Harald Welte27989d42018-06-21 20:39:20 +02002223 /* find function for current state and message */
2224 for (i = 0; i < DATASLLEN; i++)
2225 if ((msg_type == datastatelist[i].type)
2226 && ((1 << trans->cc.state) & datastatelist[i].states))
2227 break;
2228 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002229 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002230
2231 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2232 * transaction right away. */
2233 if (trans->cc.state == GSM_CSTATE_NULL) {
2234 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2235 " -- disarding new CC transaction right away\n");
2236 trans_free(trans);
2237 }
Harald Welte27989d42018-06-21 20:39:20 +02002238 return 0;
2239 }
2240
2241 assert(trans->vsub);
2242
2243 rc = datastatelist[i].rout(trans, msg);
2244
Harald Welte27989d42018-06-21 20:39:20 +02002245 return rc;
2246}