blob: 771ddbc1baef69352d1e207e0fd03fe7f5a2649d [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
Neels Hofmeyrac7d9a62022-08-06 14:16:55 +0200401 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
402
Harald Welte27989d42018-06-21 20:39:20 +0200403 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
404 mo_rel.callref = trans->callref;
405 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
406 l4_rel.callref = trans->callref;
407
408 switch(trans->cc.Tcurrent) {
409 case 0x303:
410 release = 1;
411 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
412 break;
413 case 0x310:
414 disconnect = 1;
415 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
416 break;
417 case 0x313:
418 disconnect = 1;
419 /* unknown, did not find it in the specs */
420 break;
421 case 0x301:
422 disconnect = 1;
423 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
424 break;
425 case 0x308:
426 if (!trans->cc.T308_second) {
427 /* restart T308 a second time */
428 gsm48_cc_tx_release(trans, &trans->cc.msg);
429 trans->cc.T308_second = 1;
430 break; /* stay in release state */
431 }
432 trans_free(trans);
433 return;
434 case 0x306:
435 release = 1;
436 mo_cause = trans->cc.msg.cause.value;
437 mo_location = trans->cc.msg.cause.location;
438 break;
439 case 0x323:
440 disconnect = 1;
441 break;
442 default:
443 release = 1;
444 }
445
446 if (release && trans->callref) {
447 /* process release towards layer 4 */
448 mncc_release_ind(trans->net, trans, trans->callref,
449 l4_location, l4_cause);
450 trans->callref = 0;
451 }
452
453 if (disconnect && trans->callref) {
454 /* process disconnect towards layer 4 */
455 mncc_set_cause(&l4_rel, l4_location, l4_cause);
456 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
457 }
458
459 /* process disconnect towards mobile station */
460 if (disconnect || release) {
461 mncc_set_cause(&mo_rel, mo_location, mo_cause);
462 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
463 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
464 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
465 mo_rel.cause.diag_len = 3;
466
467 if (disconnect)
468 gsm48_cc_tx_disconnect(trans, &mo_rel);
469 if (release)
470 gsm48_cc_tx_release(trans, &mo_rel);
471 }
472
473}
474
475/* disconnect both calls from the bridge */
476static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100477 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200478{
479 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
480 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
481 struct gsm_mncc mx_rel;
482 if (!trans0 || !trans1)
483 return;
484
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100485 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
486 trans0->callref, trans1->callref, strerror(err));
487 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200488 trans0->callref, trans1->callref, strerror(err));
489
490 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
491 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
492 GSM48_CC_CAUSE_CHAN_UNACCEPT);
493
494 mx_rel.callref = trans0->callref;
495 gsm48_cc_tx_disconnect(trans0, &mx_rel);
496
497 mx_rel.callref = trans1->callref;
498 gsm48_cc_tx_disconnect(trans1, &mx_rel);
499}
500
501static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
502 int sec, int micro)
503{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100504 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200505 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
506 osmo_timer_schedule(&trans->cc.timer, sec, micro);
507 trans->cc.Tcurrent = current;
508}
509
510static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
511{
512 struct gsm48_hdr *gh = msgb_l3(msg);
513 uint8_t msg_type = gsm48_hdr_msg_type(gh);
514 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
515 struct tlv_parsed tp;
516 struct gsm_mncc setup;
517
Philipp Maier9ca7b312018-10-10 17:00:49 +0200518 gsm48_start_guard_timer(trans);
519
Harald Welte27989d42018-06-21 20:39:20 +0200520 memset(&setup, 0, sizeof(struct gsm_mncc));
521 setup.callref = trans->callref;
522
Keith Whytea1a70be2021-05-16 02:59:52 +0200523 /* New Global Call Reference */
524 if (!trans->cc.lcls)
525 trans->cc.lcls = trans_lcls_compose(trans, true);
526
527 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300528 if (trans->cc.lcls) {
529 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
530 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
531 int rc;
532
533 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
534 memcpy(&setup.gcr[0], gcr_msg->data, rc);
535 setup.fields |= MNCC_F_GCR;
536 } else
537 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
538 msgb_free(gcr_msg);
539 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200540
Harald Welte27989d42018-06-21 20:39:20 +0200541 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
542 /* emergency setup is identified by msg_type */
543 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
544 setup.fields |= MNCC_F_EMERGENCY;
545 setup.emergency = 1;
546 /* use destination number as configured by user (if any) */
547 if (trans->net->emergency.route_to_msisdn) {
548 setup.fields |= MNCC_F_CALLED;
549 setup.called.type = 0; /* unknown */
550 setup.called.plan = 0; /* unknown */
551 OSMO_STRLCPY_ARRAY(setup.called.number,
552 trans->net->emergency.route_to_msisdn);
553 }
554 }
555
556 /* use subscriber as calling party number */
557 setup.fields |= MNCC_F_CALLING;
558 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
559 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
560
561 /* bearer capability */
562 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
563 setup.fields |= MNCC_F_BEARER_CAP;
564 gsm48_decode_bearer_cap(&setup.bearer_cap,
565 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
566
567 /* Create a copy of the bearer capability
568 * in the transaction struct, so we can use
569 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100570 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200571 sizeof(trans->bearer_cap));
572 }
573 /* facility */
574 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
575 setup.fields |= MNCC_F_FACILITY;
576 gsm48_decode_facility(&setup.facility,
577 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
578 }
579 /* called party bcd number */
580 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
581 setup.fields |= MNCC_F_CALLED;
582 gsm48_decode_called(&setup.called,
583 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
584 }
585 /* user-user */
586 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
587 setup.fields |= MNCC_F_USERUSER;
588 gsm48_decode_useruser(&setup.useruser,
589 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
590 }
591 /* ss-version */
592 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
593 setup.fields |= MNCC_F_SSVERSION;
594 gsm48_decode_ssversion(&setup.ssversion,
595 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
596 }
597 /* CLIR suppression */
598 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
599 setup.clir.sup = 1;
600 /* CLIR invocation */
601 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
602 setup.clir.inv = 1;
603 /* cc cap */
604 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
605 setup.fields |= MNCC_F_CCCAP;
606 gsm48_decode_cccap(&setup.cccap,
607 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
608 }
609
610 new_cc_state(trans, GSM_CSTATE_INITIATED);
611
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100612 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
613 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200614
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200615 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200616
617 /* indicate setup to MNCC */
618 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
619
620 /* MNCC code will modify the channel asynchronously, we should
621 * ipaccess-bind only after the modification has been made to the
622 * lchan->tch_mode */
623 return 0;
624}
625
626static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
627{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100628 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200629 struct gsm48_hdr *gh;
630 struct gsm_mncc *setup = arg;
631 int rc, trans_id;
632
633 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
634
635 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700636 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100637 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200638 "This is not allowed!\n");
639 /* Temporarily out of order */
640 rc = mncc_release_ind(trans->net, trans, trans->callref,
641 GSM48_CAUSE_LOC_PRN_S_LU,
642 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
643 trans->callref = 0;
644 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200645 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200646 return rc;
647 }
648
649 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100650 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200651 if (trans_id < 0) {
652 /* no free transaction ID */
653 rc = mncc_release_ind(trans->net, trans, trans->callref,
654 GSM48_CAUSE_LOC_PRN_S_LU,
655 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
656 trans->callref = 0;
657 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200658 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200659 return rc;
660 }
661 trans->transaction_id = trans_id;
662
663 gh->msg_type = GSM48_MT_CC_SETUP;
664
665 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
666
667 /* bearer capability */
668 if (setup->fields & MNCC_F_BEARER_CAP) {
669 /* Create a copy of the bearer capability in the transaction struct, so we
670 * can use this information later */
671 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
672 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
673 }
674 /* facility */
675 if (setup->fields & MNCC_F_FACILITY)
676 gsm48_encode_facility(msg, 0, &setup->facility);
677 /* progress */
678 if (setup->fields & MNCC_F_PROGRESS)
679 gsm48_encode_progress(msg, 0, &setup->progress);
680 /* calling party BCD number */
681 if (setup->fields & MNCC_F_CALLING)
682 gsm48_encode_calling(msg, &setup->calling);
683 /* called party BCD number */
684 if (setup->fields & MNCC_F_CALLED)
685 gsm48_encode_called(msg, &setup->called);
686 /* user-user */
687 if (setup->fields & MNCC_F_USERUSER)
688 gsm48_encode_useruser(msg, 0, &setup->useruser);
689 /* redirecting party BCD number */
690 if (setup->fields & MNCC_F_REDIRECTING)
691 gsm48_encode_redirecting(msg, &setup->redirecting);
692 /* signal */
693 if (setup->fields & MNCC_F_SIGNAL)
694 gsm48_encode_signal(msg, setup->signal);
695
696 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
697
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200698 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200699
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100700 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200701}
702
703static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
704{
705 struct gsm48_hdr *gh = msgb_l3(msg);
706 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
707 struct tlv_parsed tp;
708 struct gsm_mncc call_conf;
709 int rc;
710
711 gsm48_stop_cc_timer(trans);
712 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
713
714 memset(&call_conf, 0, sizeof(struct gsm_mncc));
715 call_conf.callref = trans->callref;
716
717 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
718#if 0
719 /* repeat */
720 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
721 call_conf.repeat = 1;
722 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
723 call_conf.repeat = 2;
724#endif
725 /* bearer capability */
726 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
727 call_conf.fields |= MNCC_F_BEARER_CAP;
728 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
729 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
730
731 /* Create a copy of the bearer capability
732 * in the transaction struct, so we can use
733 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100734 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200735 sizeof(trans->bearer_cap));
736 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100737
Harald Welte27989d42018-06-21 20:39:20 +0200738 /* cause */
739 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
740 call_conf.fields |= MNCC_F_CAUSE;
741 gsm48_decode_cause(&call_conf.cause,
742 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
743 }
744 /* cc cap */
745 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
746 call_conf.fields |= MNCC_F_CCCAP;
747 gsm48_decode_cccap(&call_conf.cccap,
748 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
749 }
750
751 /* IMSI of called subscriber */
752 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
753
754 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
755
756 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100757 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200758
759 /* don't continue, if there were problems with
760 * the call assignment. */
761 if (rc)
762 return rc;
763
764 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
765 &call_conf);
766}
767
768static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
769{
770 struct gsm_mncc *proceeding = arg;
771 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
773 int rc;
774
775 gh->msg_type = GSM48_MT_CC_CALL_PROC;
776
777 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
778
779 /* bearer capability */
780 if (proceeding->fields & MNCC_F_BEARER_CAP) {
781 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
782 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
783 }
784 /* facility */
785 if (proceeding->fields & MNCC_F_FACILITY)
786 gsm48_encode_facility(msg, 0, &proceeding->facility);
787 /* progress */
788 if (proceeding->fields & MNCC_F_PROGRESS)
789 gsm48_encode_progress(msg, 0, &proceeding->progress);
790
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100791 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200792 if (rc)
793 return rc;
794
795 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100796 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200797}
798
799static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
800{
801 struct gsm48_hdr *gh = msgb_l3(msg);
802 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
803 struct tlv_parsed tp;
804 struct gsm_mncc alerting;
805
806 gsm48_stop_cc_timer(trans);
807 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
808
809 memset(&alerting, 0, sizeof(struct gsm_mncc));
810 alerting.callref = trans->callref;
811 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
812 /* facility */
813 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
814 alerting.fields |= MNCC_F_FACILITY;
815 gsm48_decode_facility(&alerting.facility,
816 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
817 }
818
819 /* progress */
820 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
821 alerting.fields |= MNCC_F_PROGRESS;
822 gsm48_decode_progress(&alerting.progress,
823 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
824 }
825 /* ss-version */
826 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
827 alerting.fields |= MNCC_F_SSVERSION;
828 gsm48_decode_ssversion(&alerting.ssversion,
829 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
830 }
831
832 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
833
834 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
835 &alerting);
836}
837
838static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
839{
840 struct gsm_mncc *alerting = arg;
841 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
842 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
843
844 gh->msg_type = GSM48_MT_CC_ALERTING;
845
846 /* facility */
847 if (alerting->fields & MNCC_F_FACILITY)
848 gsm48_encode_facility(msg, 0, &alerting->facility);
849 /* progress */
850 if (alerting->fields & MNCC_F_PROGRESS)
851 gsm48_encode_progress(msg, 0, &alerting->progress);
852 /* user-user */
853 if (alerting->fields & MNCC_F_USERUSER)
854 gsm48_encode_useruser(msg, 0, &alerting->useruser);
855
856 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
857
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100858 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200859}
860
861static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
862{
863 struct gsm_mncc *progress = arg;
864 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
865 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
866
867 gh->msg_type = GSM48_MT_CC_PROGRESS;
868
869 /* progress */
870 gsm48_encode_progress(msg, 1, &progress->progress);
871 /* user-user */
872 if (progress->fields & MNCC_F_USERUSER)
873 gsm48_encode_useruser(msg, 0, &progress->useruser);
874
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100875 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200876}
877
878static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
879{
880 struct gsm_mncc *connect = arg;
881 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
882 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
883
884 gh->msg_type = GSM48_MT_CC_CONNECT;
885
886 gsm48_stop_cc_timer(trans);
887 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
888
889 /* facility */
890 if (connect->fields & MNCC_F_FACILITY)
891 gsm48_encode_facility(msg, 0, &connect->facility);
892 /* progress */
893 if (connect->fields & MNCC_F_PROGRESS)
894 gsm48_encode_progress(msg, 0, &connect->progress);
895 /* connected number */
896 if (connect->fields & MNCC_F_CONNECTED)
897 gsm48_encode_connected(msg, &connect->connected);
898 /* user-user */
899 if (connect->fields & MNCC_F_USERUSER)
900 gsm48_encode_useruser(msg, 0, &connect->useruser);
901
902 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
903
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100904 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200905}
906
907static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
908{
909 struct gsm48_hdr *gh = msgb_l3(msg);
910 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
911 struct tlv_parsed tp;
912 struct gsm_mncc connect;
913
914 gsm48_stop_cc_timer(trans);
915
916 memset(&connect, 0, sizeof(struct gsm_mncc));
917 connect.callref = trans->callref;
918 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
919 /* use subscriber as connected party number */
920 connect.fields |= MNCC_F_CONNECTED;
921 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
922 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
923
924 /* facility */
925 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
926 connect.fields |= MNCC_F_FACILITY;
927 gsm48_decode_facility(&connect.facility,
928 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
929 }
930 /* user-user */
931 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
932 connect.fields |= MNCC_F_USERUSER;
933 gsm48_decode_useruser(&connect.useruser,
934 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
935 }
936 /* ss-version */
937 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
938 connect.fields |= MNCC_F_SSVERSION;
939 gsm48_decode_ssversion(&connect.ssversion,
940 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
941 }
942
943 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200944 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200945
946 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
947}
948
949
950static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
951{
952 struct gsm_mncc connect_ack;
953
954 gsm48_stop_cc_timer(trans);
955
956 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200957 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 +0200958
959 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
960 connect_ack.callref = trans->callref;
961
962 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
963 &connect_ack);
964}
965
966static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
967{
968 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
970
971 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
972
973 new_cc_state(trans, GSM_CSTATE_ACTIVE);
974
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100975 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200976}
977
978static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
979{
980 struct gsm48_hdr *gh = msgb_l3(msg);
981 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
982 struct tlv_parsed tp;
983 struct gsm_mncc disc;
984
985 gsm48_stop_cc_timer(trans);
986
987 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
988
989 memset(&disc, 0, sizeof(struct gsm_mncc));
990 disc.callref = trans->callref;
991 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
992 /* cause */
993 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
994 disc.fields |= MNCC_F_CAUSE;
995 gsm48_decode_cause(&disc.cause,
996 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
997 }
998 /* facility */
999 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1000 disc.fields |= MNCC_F_FACILITY;
1001 gsm48_decode_facility(&disc.facility,
1002 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1003 }
1004 /* user-user */
1005 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1006 disc.fields |= MNCC_F_USERUSER;
1007 gsm48_decode_useruser(&disc.useruser,
1008 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1009 }
1010 /* ss-version */
1011 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1012 disc.fields |= MNCC_F_SSVERSION;
1013 gsm48_decode_ssversion(&disc.ssversion,
1014 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1015 }
1016
1017 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001018}
1019
1020static struct gsm_mncc_cause default_cause = {
1021 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1022 .coding = 0,
1023 .rec = 0,
1024 .rec_val = 0,
1025 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1026 .diag_len = 0,
1027 .diag = { 0 },
1028};
1029
1030static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1031{
1032 struct gsm_mncc *disc = arg;
1033 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1034 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1035
1036 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1037
1038 gsm48_stop_cc_timer(trans);
1039 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1040
1041 /* cause */
1042 if (disc->fields & MNCC_F_CAUSE)
1043 gsm48_encode_cause(msg, 1, &disc->cause);
1044 else
1045 gsm48_encode_cause(msg, 1, &default_cause);
1046
1047 /* facility */
1048 if (disc->fields & MNCC_F_FACILITY)
1049 gsm48_encode_facility(msg, 0, &disc->facility);
1050 /* progress */
1051 if (disc->fields & MNCC_F_PROGRESS)
1052 gsm48_encode_progress(msg, 0, &disc->progress);
1053 /* user-user */
1054 if (disc->fields & MNCC_F_USERUSER)
1055 gsm48_encode_useruser(msg, 0, &disc->useruser);
1056
1057 /* store disconnect cause for T306 expiry */
1058 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1059
1060 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1061
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001062 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001063}
1064
1065static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1066{
1067 struct gsm48_hdr *gh = msgb_l3(msg);
1068 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1069 struct tlv_parsed tp;
1070 struct gsm_mncc rel;
1071 int rc;
1072
1073 gsm48_stop_cc_timer(trans);
1074
1075 memset(&rel, 0, sizeof(struct gsm_mncc));
1076 rel.callref = trans->callref;
1077 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1078 /* cause */
1079 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1080 rel.fields |= MNCC_F_CAUSE;
1081 gsm48_decode_cause(&rel.cause,
1082 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1083 }
1084 /* facility */
1085 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1086 rel.fields |= MNCC_F_FACILITY;
1087 gsm48_decode_facility(&rel.facility,
1088 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1089 }
1090 /* user-user */
1091 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1092 rel.fields |= MNCC_F_USERUSER;
1093 gsm48_decode_useruser(&rel.useruser,
1094 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1095 }
1096 /* ss-version */
1097 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1098 rel.fields |= MNCC_F_SSVERSION;
1099 gsm48_decode_ssversion(&rel.ssversion,
1100 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1101 }
1102
1103 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1104 /* release collision 5.4.5 */
1105 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1106 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001107 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001108 GSM48_PDISC_CC | (trans->transaction_id << 4),
1109 GSM48_MT_CC_RELEASE_COMPL);
1110 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1111 }
1112
1113 new_cc_state(trans, GSM_CSTATE_NULL);
1114
1115 trans->callref = 0;
1116 trans_free(trans);
1117
1118 return rc;
1119}
1120
1121static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1122{
1123 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001124 struct msgb *msg;
1125 struct gsm48_hdr *gh;
1126
1127 if (!trans->msc_a) {
1128 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1129 return -EINVAL;
1130 }
1131
1132 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1133 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001134
1135 gh->msg_type = GSM48_MT_CC_RELEASE;
1136
1137 gsm48_stop_cc_timer(trans);
1138 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1139
1140 /* cause */
1141 if (rel->fields & MNCC_F_CAUSE)
1142 gsm48_encode_cause(msg, 0, &rel->cause);
1143 /* facility */
1144 if (rel->fields & MNCC_F_FACILITY)
1145 gsm48_encode_facility(msg, 0, &rel->facility);
1146 /* user-user */
1147 if (rel->fields & MNCC_F_USERUSER)
1148 gsm48_encode_useruser(msg, 0, &rel->useruser);
1149
1150 trans->cc.T308_second = 0;
1151 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1152
1153 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1154 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1155
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001156 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001157}
1158
1159static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1160{
1161 struct gsm48_hdr *gh = msgb_l3(msg);
1162 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1163 struct tlv_parsed tp;
1164 struct gsm_mncc rel;
1165 int rc = 0;
1166
1167 gsm48_stop_cc_timer(trans);
1168
1169 memset(&rel, 0, sizeof(struct gsm_mncc));
1170 rel.callref = trans->callref;
1171 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1172 /* cause */
1173 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1174 rel.fields |= MNCC_F_CAUSE;
1175 gsm48_decode_cause(&rel.cause,
1176 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1177 }
1178 /* facility */
1179 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1180 rel.fields |= MNCC_F_FACILITY;
1181 gsm48_decode_facility(&rel.facility,
1182 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1183 }
1184 /* user-user */
1185 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1186 rel.fields |= MNCC_F_USERUSER;
1187 gsm48_decode_useruser(&rel.useruser,
1188 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1189 }
1190 /* ss-version */
1191 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1192 rel.fields |= MNCC_F_SSVERSION;
1193 gsm48_decode_ssversion(&rel.ssversion,
1194 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1195 }
1196
1197 if (trans->callref) {
1198 switch (trans->cc.state) {
1199 case GSM_CSTATE_CALL_PRESENT:
1200 rc = mncc_recvmsg(trans->net, trans,
1201 MNCC_REJ_IND, &rel);
1202 break;
1203 case GSM_CSTATE_RELEASE_REQ:
1204 rc = mncc_recvmsg(trans->net, trans,
1205 MNCC_REL_CNF, &rel);
1206 break;
1207 default:
1208 rc = mncc_recvmsg(trans->net, trans,
1209 MNCC_REL_IND, &rel);
1210 }
1211 }
1212
1213 trans->callref = 0;
1214 trans_free(trans);
1215
1216 return rc;
1217}
1218
1219static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1220{
1221 struct gsm_mncc *rel = arg;
1222 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1223 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1224 int ret;
1225
1226 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1227
1228 trans->callref = 0;
1229
1230 gsm48_stop_cc_timer(trans);
1231
1232 /* cause */
1233 if (rel->fields & MNCC_F_CAUSE)
1234 gsm48_encode_cause(msg, 0, &rel->cause);
1235 /* facility */
1236 if (rel->fields & MNCC_F_FACILITY)
1237 gsm48_encode_facility(msg, 0, &rel->facility);
1238 /* user-user */
1239 if (rel->fields & MNCC_F_USERUSER)
1240 gsm48_encode_useruser(msg, 0, &rel->useruser);
1241
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001242 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001243
1244 trans_free(trans);
1245
1246 return ret;
1247}
1248
1249static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1250{
1251 struct gsm48_hdr *gh = msgb_l3(msg);
1252 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1253 struct tlv_parsed tp;
1254 struct gsm_mncc fac;
1255
1256 memset(&fac, 0, sizeof(struct gsm_mncc));
1257 fac.callref = trans->callref;
1258 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1259 /* facility */
1260 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1261 fac.fields |= MNCC_F_FACILITY;
1262 gsm48_decode_facility(&fac.facility,
1263 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1264 }
1265 /* ss-version */
1266 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1267 fac.fields |= MNCC_F_SSVERSION;
1268 gsm48_decode_ssversion(&fac.ssversion,
1269 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1270 }
1271
1272 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1273}
1274
1275static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1276{
1277 struct gsm_mncc *fac = arg;
1278 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1279 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1280
1281 gh->msg_type = GSM48_MT_CC_FACILITY;
1282
1283 /* facility */
1284 gsm48_encode_facility(msg, 1, &fac->facility);
1285
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001286 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001287}
1288
1289static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1290{
1291 struct gsm_mncc hold;
1292
1293 memset(&hold, 0, sizeof(struct gsm_mncc));
1294 hold.callref = trans->callref;
1295 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1296}
1297
1298static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1299{
1300 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1302
1303 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001305 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001306}
1307
1308static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1309{
1310 struct gsm_mncc *hold_rej = arg;
1311 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1312 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1313
1314 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1315
1316 /* cause */
1317 if (hold_rej->fields & MNCC_F_CAUSE)
1318 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1319 else
1320 gsm48_encode_cause(msg, 1, &default_cause);
1321
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001322 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001323}
1324
1325static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1326{
1327 struct gsm_mncc retrieve;
1328
1329 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1330 retrieve.callref = trans->callref;
1331 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1332 &retrieve);
1333}
1334
1335static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1336{
1337 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1338 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1339
1340 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001342 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001343}
1344
1345static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1346{
1347 struct gsm_mncc *retrieve_rej = arg;
1348 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1349 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1350
1351 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1352
1353 /* cause */
1354 if (retrieve_rej->fields & MNCC_F_CAUSE)
1355 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1356 else
1357 gsm48_encode_cause(msg, 1, &default_cause);
1358
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001359 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001360}
1361
1362static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1363{
1364 struct gsm48_hdr *gh = msgb_l3(msg);
1365 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1366 struct tlv_parsed tp;
1367 struct gsm_mncc dtmf;
1368
1369 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1370 dtmf.callref = trans->callref;
1371 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1372 /* keypad facility */
1373 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1374 dtmf.fields |= MNCC_F_KEYPAD;
1375 gsm48_decode_keypad(&dtmf.keypad,
1376 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1377 }
1378
1379 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1380}
1381
1382static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1383{
1384 struct gsm_mncc *dtmf = arg;
1385 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1386 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1387
1388 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1389
1390 /* keypad */
1391 if (dtmf->fields & MNCC_F_KEYPAD)
1392 gsm48_encode_keypad(msg, dtmf->keypad);
1393
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001394 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001395}
1396
1397static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1398{
1399 struct gsm_mncc *dtmf = arg;
1400 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1401 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1402
1403 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1404
1405 /* cause */
1406 if (dtmf->fields & MNCC_F_CAUSE)
1407 gsm48_encode_cause(msg, 1, &dtmf->cause);
1408 else
1409 gsm48_encode_cause(msg, 1, &default_cause);
1410
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001411 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001412}
1413
1414static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1415{
1416 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1417 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1418
1419 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1420
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001421 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001422}
1423
1424static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1425{
1426 struct gsm_mncc dtmf;
1427
1428 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1429 dtmf.callref = trans->callref;
1430
1431 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1432}
1433
1434static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1435{
1436 struct gsm48_hdr *gh = msgb_l3(msg);
1437 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1438 struct tlv_parsed tp;
1439 struct gsm_mncc modify;
1440
1441 memset(&modify, 0, sizeof(struct gsm_mncc));
1442 modify.callref = trans->callref;
1443 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1444 /* bearer capability */
1445 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1446 modify.fields |= MNCC_F_BEARER_CAP;
1447 gsm48_decode_bearer_cap(&modify.bearer_cap,
1448 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1449
1450 /* Create a copy of the bearer capability
1451 * in the transaction struct, so we can use
1452 * this information later */
1453 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1454 sizeof(trans->bearer_cap));
1455 }
1456
1457 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1458
1459 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1460}
1461
1462static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1463{
1464 struct gsm_mncc *modify = arg;
1465 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1466 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1467
1468 gh->msg_type = GSM48_MT_CC_MODIFY;
1469
1470 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1471
1472 /* bearer capability */
1473 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1474 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1475
1476 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1477
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001478 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001479}
1480
1481static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1482{
1483 struct gsm48_hdr *gh = msgb_l3(msg);
1484 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1485 struct tlv_parsed tp;
1486 struct gsm_mncc modify;
1487
1488 gsm48_stop_cc_timer(trans);
1489
1490 memset(&modify, 0, sizeof(struct gsm_mncc));
1491 modify.callref = trans->callref;
1492 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1493 /* bearer capability */
1494 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1495 modify.fields |= MNCC_F_BEARER_CAP;
1496 gsm48_decode_bearer_cap(&modify.bearer_cap,
1497 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1498
1499 /* Create a copy of the bearer capability
1500 * in the transaction struct, so we can use
1501 * this information later */
1502 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1503 sizeof(trans->bearer_cap));
1504 }
1505
1506 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1507
1508 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1509}
1510
1511static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1512{
1513 struct gsm_mncc *modify = arg;
1514 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1515 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1516
1517 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1518
1519 /* bearer capability */
1520 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1521 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1522
1523 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1524
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001525 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001526}
1527
1528static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1529{
1530 struct gsm48_hdr *gh = msgb_l3(msg);
1531 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1532 struct tlv_parsed tp;
1533 struct gsm_mncc modify;
1534
1535 gsm48_stop_cc_timer(trans);
1536
1537 memset(&modify, 0, sizeof(struct gsm_mncc));
1538 modify.callref = trans->callref;
1539 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1540 /* bearer capability */
1541 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1542 modify.fields |= GSM48_IE_BEARER_CAP;
1543 gsm48_decode_bearer_cap(&modify.bearer_cap,
1544 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1545
1546 /* Create a copy of the bearer capability
1547 * in the transaction struct, so we can use
1548 * this information later */
1549 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1550 sizeof(trans->bearer_cap));
1551 }
1552 /* cause */
1553 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1554 modify.fields |= MNCC_F_CAUSE;
1555 gsm48_decode_cause(&modify.cause,
1556 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1557 }
1558
1559 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1560
1561 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1562}
1563
1564static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1565{
1566 struct gsm_mncc *modify = arg;
1567 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1568 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1569
1570 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1571
1572 /* bearer capability */
1573 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1574 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1575 /* cause */
1576 gsm48_encode_cause(msg, 1, &modify->cause);
1577
1578 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1579
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001580 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001581}
1582
1583static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1584{
1585 struct gsm_mncc *notify = arg;
1586 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1587 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1588
1589 gh->msg_type = GSM48_MT_CC_NOTIFY;
1590
1591 /* notify */
1592 gsm48_encode_notify(msg, notify->notify);
1593
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001594 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001595}
1596
1597static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1598{
1599 struct gsm48_hdr *gh = msgb_l3(msg);
1600 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1601// struct tlv_parsed tp;
1602 struct gsm_mncc notify;
1603
1604 memset(&notify, 0, sizeof(struct gsm_mncc));
1605 notify.callref = trans->callref;
1606// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1607 if (payload_len >= 1)
1608 gsm48_decode_notify(&notify.notify, gh->data);
1609
1610 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1611}
1612
1613static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1614{
1615 struct gsm_mncc *user = arg;
1616 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1618
1619 gh->msg_type = GSM48_MT_CC_USER_INFO;
1620
1621 /* user-user */
1622 if (user->fields & MNCC_F_USERUSER)
1623 gsm48_encode_useruser(msg, 1, &user->useruser);
1624 /* more data */
1625 if (user->more)
1626 gsm48_encode_more(msg);
1627
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001628 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001629}
1630
1631static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1632{
1633 struct gsm48_hdr *gh = msgb_l3(msg);
1634 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1635 struct tlv_parsed tp;
1636 struct gsm_mncc user;
1637
1638 memset(&user, 0, sizeof(struct gsm_mncc));
1639 user.callref = trans->callref;
1640 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1641 /* user-user */
1642 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1643 user.fields |= MNCC_F_USERUSER;
1644 gsm48_decode_useruser(&user.useruser,
1645 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1646 }
1647 /* more data */
1648 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1649 user.more = 1;
1650
1651 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1652}
1653
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001654static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1655 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1656 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001657{
1658 uint8_t data[sizeof(struct gsm_mncc)];
1659 struct gsm_mncc_rtp *rtp;
1660
1661 memset(&data, 0, sizeof(data));
1662 rtp = (struct gsm_mncc_rtp *) &data[0];
1663
1664 rtp->callref = callref;
1665 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001666 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001667 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1668 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001669 }
Harald Welte27989d42018-06-21 20:39:20 +02001670 rtp->payload_type = payload_type;
1671 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001672 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001673}
1674
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001675static 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 +02001676{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001677 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001678}
1679
1680static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1681{
1682 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001683
1684 /* Find callref */
1685 trans = trans_find_by_callref(net, callref);
1686 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001687 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001688 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001689 return -EIO;
1690 }
1691 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001692 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001693 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001694 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001695 return 0;
1696 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001697 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001698
Harald Welte27989d42018-06-21 20:39:20 +02001699 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001700 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001701}
1702
1703/* Trigger TCH_RTP_CREATE acknowledgement */
1704int gsm48_tch_rtp_create(struct gsm_trans *trans)
1705{
1706 /* This function is called as soon as the port, on which the
1707 * mgcp-gw expects the incoming RTP stream from the remote
1708 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001709 struct msc_a *msc_a = trans->msc_a;
1710 struct gsm_network *net = msc_a_net(msc_a);
1711 struct call_leg *cl = msc_a->cc.call_leg;
1712 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001713 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1714 uint32_t payload_type;
1715 int payload_msg_type;
1716 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001717
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001718 if (!rtp_cn) {
1719 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1720 return -EINVAL;
1721 }
1722
1723 if (!rtp_cn->codec_known) {
1724 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1725 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1726 return -EINVAL;
1727 }
1728
1729 /* Codec */
1730 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1731
1732 /* Payload Type number */
1733 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001734 if (mgcp_info && mgcp_info->ptmap_len)
1735 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1736 else
1737 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001738
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001739 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1740 if (!rtp_cn_local) {
1741 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1742 return -EINVAL;
1743 }
1744
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001745 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 +02001746}
1747
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001748static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001749{
1750 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001751 struct call_leg *cl;
1752 struct rtp_stream *rtps;
1753 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001754 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001755
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001756 /* FIXME: in *rtp we should get the codec information of the remote
1757 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1758 * meaningful value based on this information but unfortunately we
1759 * can't do that yet because the mncc API can not signal dynamic
1760 * payload types yet. This must be fixed first. Also there may be
1761 * additional members necessary in trans->conn->rtp because we
1762 * somehow need to deal with dynamic payload types that do not
1763 * comply to 3gpp's assumptions of payload type numbers on the A
1764 * interface. See also related tickets: OS#3399 and OS1683 */
1765
Harald Welte27989d42018-06-21 20:39:20 +02001766 /* Find callref */
1767 trans = trans_find_by_callref(net, rtp->callref);
1768 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001769 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001770 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001771 return -EIO;
1772 }
1773 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001774 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001775 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001776 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001777 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001778 }
1779
Neels Hofmeyr90933d42022-01-13 20:10:52 +01001780 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 +02001781 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1782 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001783
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001784 cl = trans->msc_a->cc.call_leg;
1785 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1786
1787 if (!rtps) {
1788 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1789 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1790 return -EINVAL;
1791 }
1792
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001793 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1794 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1795 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1796 return -EINVAL;
1797 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001798 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1799 rtp_stream_commit(rtps);
1800 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001801}
1802
1803static struct downstate {
1804 uint32_t states;
1805 int type;
1806 int (*rout) (struct gsm_trans *trans, void *arg);
1807} downstatelist[] = {
1808 /* mobile originating call establishment */
1809 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1810 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1811 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1812 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1813 {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 */
1814 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1815 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1816 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1817 /* mobile terminating call establishment */
1818 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1819 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1820 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1821 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1822 /* signalling during call */
1823 {SBIT(GSM_CSTATE_ACTIVE),
1824 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1825 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1826 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1827 {ALL_STATES,
1828 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1829 {ALL_STATES,
1830 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1831 {ALL_STATES,
1832 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1833 {SBIT(GSM_CSTATE_ACTIVE),
1834 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1835 {SBIT(GSM_CSTATE_ACTIVE),
1836 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1837 {SBIT(GSM_CSTATE_ACTIVE),
1838 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1839 {SBIT(GSM_CSTATE_ACTIVE),
1840 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1841 {SBIT(GSM_CSTATE_ACTIVE),
1842 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1843 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1844 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1845 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1846 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1847 {SBIT(GSM_CSTATE_ACTIVE),
1848 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1849 /* clearing */
1850 {SBIT(GSM_CSTATE_INITIATED),
1851 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1852 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1853 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1854 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1855 MNCC_REL_REQ, gsm48_cc_tx_release},
1856};
1857
1858#define DOWNSLLEN \
1859 (sizeof(downstatelist) / sizeof(struct downstate))
1860
1861
Philipp Maiercd64af72019-08-01 09:46:40 +02001862static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001863{
1864 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001865 struct msc_a *msc_a = NULL;
1866 struct gsm_trans *trans = NULL;
1867 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001868
Harald Welte27989d42018-06-21 20:39:20 +02001869 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001870 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001871 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001872 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001873 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001874 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001875 return rc;
1876 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001877 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001878 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001879 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001880 case MNCC_RTP_FREE:
1881 /* unused right now */
1882 return -EIO;
1883
1884 case MNCC_FRAME_DROP:
1885 case MNCC_FRAME_RECV:
1886 case GSM_TCHF_FRAME:
1887 case GSM_TCHF_FRAME_EFR:
1888 case GSM_TCHH_FRAME:
1889 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001890 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001891 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001892 return -ENOTSUP;
1893 }
1894
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001895 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001896
1897 /* Find callref */
1898 trans = trans_find_by_callref(net, data->callref);
1899
1900 /* Callref unknown */
1901 if (!trans) {
1902 struct vlr_subscr *vsub;
1903
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001904 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001905 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001906 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001907 /* Invalid call reference */
1908 return mncc_release_ind(net, NULL, data->callref,
1909 GSM48_CAUSE_LOC_PRN_S_LU,
1910 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1911 }
1912 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001913 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001914 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001915 /* Invalid number */
1916 return mncc_release_ind(net, NULL, data->callref,
1917 GSM48_CAUSE_LOC_PRN_S_LU,
1918 GSM48_CC_CAUSE_INV_NR_FORMAT);
1919 }
1920 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001921 if (data->called.number[0]) {
1922 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1923 if (!vsub)
1924 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001925 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001926 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001927 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001928 if (!vsub)
1929 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001930 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001931 }
1932 if (!vsub)
1933 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001934 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001935 /* update the subscriber we deal with */
1936 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1937
Harald Welte27989d42018-06-21 20:39:20 +02001938 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001939 if (!vsub->lu_complete) {
1940 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001941 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001942 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001943 /* Temporarily out of order */
1944 return mncc_release_ind(net, NULL, data->callref,
1945 GSM48_CAUSE_LOC_PRN_S_LU,
1946 GSM48_CC_CAUSE_DEST_OOO);
1947 }
Keith Whyte991bb422019-08-08 15:43:40 +02001948
1949 /* Find valid conn */
1950 msc_a = msc_a_for_vsub(vsub, true);
1951
1952 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1953 if (!net->call_waiting && msc_a) {
1954 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1955 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1956 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1957 "rx '%s' for subscriber %s with trans state (%s)"
1958 " rejecting with USER_BUSY\n",
1959 get_mncc_name(msg->msg_type), data->called.number,
1960 gsm48_cc_state_name(existing_cc_trans->cc.state));
1961 return mncc_release_ind(net, NULL, data->callref,
1962 GSM48_CAUSE_LOC_PRN_S_LU,
1963 GSM48_CC_CAUSE_USER_BUSY);
1964 }
1965 }
1966
Harald Welte27989d42018-06-21 20:39:20 +02001967 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001968 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001969 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001970 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001971 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001972 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001973 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001974 mncc_release_ind(net, NULL, data->callref,
1975 GSM48_CAUSE_LOC_PRN_S_LU,
1976 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1977 return -ENOMEM;
1978 }
1979
Harald Welte27989d42018-06-21 20:39:20 +02001980 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001981 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001982 /* This condition will return before the common logging of the received MNCC message below, so
1983 * log it now. */
1984 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001985
Harald Welte27989d42018-06-21 20:39:20 +02001986 /* store setup information until paging succeeds */
1987 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1988
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001989 /* Request a channel. If Paging already started, paging_request_start() will append the new
1990 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001991 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1992 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001993 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001994 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001995 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001996 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001997 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001998 return 0;
1999 }
2000
2001 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002002 trans->msc_a = msc_a;
2003 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002004 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002005 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002006 } else {
2007 /* update the subscriber we deal with */
2008 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2009 }
2010
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002011 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002012
Philipp Maier9ca7b312018-10-10 17:00:49 +02002013 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002014 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002015
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016 if (trans->msc_a)
2017 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002018
2019 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002020 if (!msc_a) {
2021 struct gsm_mncc rel = {
2022 .callref = data->callref,
2023 };
2024 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002025 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2026 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002027 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002028 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2029 else
2030 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2031 trans->callref = 0;
2032 trans_free(trans);
2033 return rc;
2034 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002035 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002036 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002037 }
2038
2039 /* Find function for current state and message */
2040 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002041 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002042 && ((1 << trans->cc.state) & downstatelist[i].states))
2043 break;
2044 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002045 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002046 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002047 return 0;
2048 }
2049
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002050 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002051
2052 return rc;
2053}
2054
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002055struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2056{
2057 uint32_t callref;
2058
2059 switch (msg->msg_type) {
2060 case MNCC_BRIDGE:
2061 callref = msg->bridge.callref[0];
2062 break;
2063 case MNCC_RTP_CREATE:
2064 case MNCC_RTP_CONNECT:
2065 callref = msg->rtp.callref;
2066 break;
2067
2068 case MNCC_RTP_FREE:
2069 case MNCC_FRAME_DROP:
2070 case MNCC_FRAME_RECV:
2071 case GSM_TCHF_FRAME:
2072 case GSM_TCHF_FRAME_EFR:
2073 case GSM_TCHH_FRAME:
2074 case GSM_TCH_FRAME_AMR:
2075 return NULL;
2076
2077 default:
2078 callref = msg->signal.callref;
2079 break;
2080 }
2081
2082 return mncc_call_find_by_callref(callref);
2083}
2084
2085/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002086int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002087{
2088 const union mncc_msg *msg = arg;
2089 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002090
2091 if (msg->msg_type == MNCC_SETUP_REQ) {
2092 /* Incoming call to forward for inter-MSC Handover? */
2093 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2094 if (mncc_call)
2095 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2096 "Incoming call matches pending inter-MSC Handover Number\n");
2097 }
2098 if (!mncc_call) {
2099 /* Find already active MNCC FSM for this callref.
2100 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2101 * MNCC<->GSM-CC call handling. */
2102 mncc_call = mncc_find_by_callref_from_msg(msg);
2103 }
2104 if (mncc_call) {
2105 mncc_call_rx(mncc_call, msg);
2106 return 0;
2107 }
2108
2109 /* None of the above? Then it must be a normal GSM CC call related message. */
2110 return mncc_tx_to_gsm_cc(net, msg);
2111}
Harald Welte27989d42018-06-21 20:39:20 +02002112
2113static struct datastate {
2114 uint32_t states;
2115 int type;
2116 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2117} datastatelist[] = {
2118 /* mobile originating call establishment */
2119 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2120 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2121 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2122 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2123 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2124 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2125 /* mobile terminating call establishment */
2126 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2127 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2128 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2129 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2130 {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 */
2131 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2132 /* signalling during call */
2133 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2134 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2135 {SBIT(GSM_CSTATE_ACTIVE),
2136 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2137 {ALL_STATES,
2138 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2139 {ALL_STATES,
2140 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2141 {ALL_STATES,
2142 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2143 {SBIT(GSM_CSTATE_ACTIVE),
2144 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2145 {SBIT(GSM_CSTATE_ACTIVE),
2146 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2147 {SBIT(GSM_CSTATE_ACTIVE),
2148 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2149 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2150 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2151 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2152 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2153 {SBIT(GSM_CSTATE_ACTIVE),
2154 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2155 /* clearing */
2156 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2157 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2158 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2159 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2160 {ALL_STATES, /* 5.4.3.4 */
2161 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2162};
2163
2164#define DATASLLEN \
2165 (sizeof(datastatelist) / sizeof(struct datastate))
2166
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002167int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002168{
2169 struct gsm48_hdr *gh = msgb_l3(msg);
2170 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2171 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2172 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002173 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2174 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002175 int i, rc = 0;
2176
2177 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002178 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002179 return -EINVAL;
2180 }
2181
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002182 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002183 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002184 return -EINVAL;
2185 }
2186
2187 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002188 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002189
Harald Welte27989d42018-06-21 20:39:20 +02002190 /* Create transaction */
2191 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002192 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002193 trans = trans_alloc(net, vsub,
2194 TRANS_CC,
2195 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002196 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002197 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002198 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002199 GSM48_PDISC_CC | (transaction_id << 4),
2200 GSM48_MT_CC_RELEASE_COMPL);
2201 return -ENOMEM;
2202 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002203 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2204 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2205 trans_free(trans);
2206 return -EINVAL;
2207 }
2208
Harald Welte27989d42018-06-21 20:39:20 +02002209 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002210 msc_a_get(msc_a, MSC_A_USE_CC);
2211 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002212 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002213
2214 /* An earlier CM Service Request for this CC message now has concluded */
2215 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2216 LOG_MSC_A(msc_a, LOGL_ERROR,
2217 "Creating new CC transaction without prior CM Service Request\n");
2218 else
2219 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002220 }
2221
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002222 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2223 gsm48_cc_state_name(trans->cc.state));
2224
Harald Welte27989d42018-06-21 20:39:20 +02002225 /* find function for current state and message */
2226 for (i = 0; i < DATASLLEN; i++)
2227 if ((msg_type == datastatelist[i].type)
2228 && ((1 << trans->cc.state) & datastatelist[i].states))
2229 break;
2230 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002231 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002232
2233 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2234 * transaction right away. */
2235 if (trans->cc.state == GSM_CSTATE_NULL) {
2236 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2237 " -- disarding new CC transaction right away\n");
2238 trans_free(trans);
2239 }
Harald Welte27989d42018-06-21 20:39:20 +02002240 return 0;
2241 }
2242
2243 assert(trans->vsub);
2244
2245 rc = datastatelist[i].rout(trans, msg);
2246
Harald Welte27989d42018-06-21 20:39:20 +02002247 return rc;
2248}