blob: a8b4665e61cb24fb35ef24a0099efb774968fa45 [file] [log] [blame]
Harald Welte27989d42018-06-21 20:39:20 +02001/* GSM Mobile Radio Interface Layer 3 Call Control */
2
3/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdbool.h>
27#include <errno.h>
28#include <time.h>
29#include <netinet/in.h>
30#include <regex.h>
31#include <sys/types.h>
32
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +020033#include <osmocom/mgcp_client/mgcp_client_endpoint_fsm.h>
34
Harald Welte27989d42018-06-21 20:39:20 +020035#include <osmocom/msc/db.h>
36#include <osmocom/msc/debug.h>
37#include <osmocom/msc/gsm_data.h>
38#include <osmocom/msc/gsm_subscriber.h>
39#include <osmocom/msc/gsm_04_11.h>
40#include <osmocom/msc/gsm_04_08.h>
41#include <osmocom/msc/gsm_04_80.h>
42#include <osmocom/msc/gsm_04_14.h>
43#include <osmocom/msc/gsm_09_11.h>
44#include <osmocom/msc/signal.h>
45#include <osmocom/msc/transaction.h>
46#include <osmocom/msc/silent_call.h>
Harald Welte27989d42018-06-21 20:39:20 +020047#include <osmocom/msc/mncc_int.h>
48#include <osmocom/abis/e1_input.h>
49#include <osmocom/core/bitvec.h>
50#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010051#include <osmocom/msc/msub.h>
52#include <osmocom/msc/msc_a.h>
53#include <osmocom/msc/paging.h>
54#include <osmocom/msc/call_leg.h>
55#include <osmocom/msc/rtp_stream.h>
56#include <osmocom/msc/mncc_call.h>
57#include <osmocom/msc/msc_t.h>
Harald Welte27989d42018-06-21 20:39:20 +020058
59#include <osmocom/gsm/gsm48.h>
60#include <osmocom/gsm/gsm0480.h>
61#include <osmocom/gsm/gsm_utils.h>
62#include <osmocom/gsm/protocol/gsm_04_08.h>
63#include <osmocom/core/msgb.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/core/utils.h>
66#include <osmocom/core/byteswap.h>
67#include <osmocom/gsm/tlv.h>
68#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020069
70#include <assert.h>
71
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010072static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
73static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
75
76static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
77{
78 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
79 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
80 OMSC_LINKID_CB(msg) = trans->dlci;
81
82 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
83}
84
85uint32_t msc_cc_next_outgoing_callref() {
86 static uint32_t last_callref = 0x80000000;
87 last_callref++;
88 if (last_callref < 0x80000001)
89 last_callref = 0x80000001;
90 return last_callref;
91}
Harald Welte27989d42018-06-21 20:39:20 +020092
Philipp Maier9ca7b312018-10-10 17:00:49 +020093static void gsm48_cc_guard_timeout(void *arg)
94{
95 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010096 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020097 trans_free(trans);
98 return;
99}
100
101static void gsm48_stop_guard_timer(struct gsm_trans *trans)
102{
103 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100104 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200105 osmo_timer_del(&trans->cc.timer_guard);
106 }
107}
108
109static void gsm48_start_guard_timer(struct gsm_trans *trans)
110{
111 /* NOTE: The purpose of this timer is to prevent the cc state machine
112 * from hanging in cases where mncc, gsm48 or both become unresponsive
113 * for some reason. The timer is started initially with the setup from
114 * the gsm48 side and then re-started with every incoming mncc message.
115 * Once the mncc state reaches its active state the timer is stopped.
116 * So if the cc state machine does not show any activity for an
117 * extended amount of time during call setup or teardown the guard
118 * timer will time out and hard-clear the connection. */
119 if (osmo_timer_pending(&trans->cc.timer_guard))
120 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100121 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200122 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
123 osmo_timer_schedule(&trans->cc.timer_guard,
124 trans->net->mncc_guard_timeout, 0);
125}
Harald Welte27989d42018-06-21 20:39:20 +0200126
127/* Call Control */
128
129void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
130{
131 net->mncc_recv(net, msg);
132}
133
134int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
135{
136 struct gsm48_hdr *gh;
137 struct msgb *ss_notify;
138
139 ss_notify = gsm0480_create_notifySS(message);
140 if (!ss_notify)
141 return -1;
142
143 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
144 uint8_t *data = msgb_push(ss_notify, 1);
145 data[0] = ss_notify->len - 1;
146 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
147 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100148 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200149}
150
151/* FIXME: this count_statistics is a state machine behaviour. we should convert
152 * the complete call control into a state machine. Afterwards we can move this
153 * code into state transitions.
154 */
155static void count_statistics(struct gsm_trans *trans, int new_state)
156{
157 int old_state = trans->cc.state;
158 struct rate_ctr_group *msc = trans->net->msc_ctrs;
159
160 if (old_state == new_state)
161 return;
162
163 /* state incoming */
164 switch (new_state) {
165 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200166 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
167 1);
168 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200169 break;
170 }
171
172 /* state outgoing */
173 switch (old_state) {
174 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200175 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
176 1);
Harald Welte27989d42018-06-21 20:39:20 +0200177 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
178 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200179 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200180 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200181 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200182 break;
183 }
184}
185
Harald Welte27989d42018-06-21 20:39:20 +0200186static void new_cc_state(struct gsm_trans *trans, int state)
187{
188 if (state > 31 || state < 0)
189 return;
190
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100191 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
192 gsm48_cc_state_name(trans->cc.state),
193 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200194
195 count_statistics(trans, state);
196 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200197
198 /* Stop the guard timer when a call reaches the active state */
199 if (state == GSM_CSTATE_ACTIVE)
200 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200201}
202
203static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
204{
205 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
206 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
207 uint8_t *cause, *call_state;
208
209 gh->msg_type = GSM48_MT_CC_STATUS;
210
211 cause = msgb_put(msg, 3);
212 cause[0] = 2;
213 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
214 cause[2] = 0x80 | 30; /* response to status inquiry */
215
216 call_state = msgb_put(msg, 1);
217 call_state[0] = 0xc0 | 0x00;
218
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100219 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200220}
221
222static void gsm48_stop_cc_timer(struct gsm_trans *trans)
223{
224 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100225 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200226 osmo_timer_del(&trans->cc.timer);
227 trans->cc.Tcurrent = 0;
228 }
229}
230
231static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
232 int msg_type, struct gsm_mncc *mncc)
233{
234 struct msgb *msg;
235 unsigned char *data;
236
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100237 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200238
239 mncc->msg_type = msg_type;
240
241 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
242 if (!msg)
243 return -ENOMEM;
244
245 data = msgb_put(msg, sizeof(struct gsm_mncc));
246 memcpy(data, mncc, sizeof(struct gsm_mncc));
247
248 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200249 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
250 if (trans)
251 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200252
253 return 0;
254}
255
256int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
257 uint32_t callref, int location, int value)
258{
259 struct gsm_mncc rel;
260
261 memset(&rel, 0, sizeof(rel));
262 rel.callref = callref;
263 mncc_set_cause(&rel, location, value);
264 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
265 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
266 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
267}
268
269/* Call Control Specific transaction release.
270 * gets called by trans_free, DO NOT CALL YOURSELF! */
271void _gsm48_cc_trans_free(struct gsm_trans *trans)
272{
273 gsm48_stop_cc_timer(trans);
274
Harald Welte27989d42018-06-21 20:39:20 +0200275 /* send release to L4, if callref still exists */
276 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100277 /* Send MNCC REL.ind (cause='Resource unavailable') */
278 if (trans->cc.mncc_initiated) {
279 mncc_release_ind(trans->net, trans, trans->callref,
280 GSM48_CAUSE_LOC_PRN_S_LU,
281 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
282 }
283
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100284 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
285 * CC Release to the MS if it gets freed here. Hack it to do so. */
286 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
287 struct gsm_mncc rel = {};
288 rel.callref = trans->callref;
289 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
290 gsm48_cc_tx_release(trans, &rel);
291 }
Harald Welte27989d42018-06-21 20:39:20 +0200292 /* This is a final freeing of the transaction. The MNCC release may have triggered the
293 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
294 gsm48_stop_cc_timer(trans);
295 }
296 if (trans->cc.state != GSM_CSTATE_NULL)
297 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200298
299 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100300
301 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
302 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200303}
304
Harald Welte27989d42018-06-21 20:39:20 +0200305/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100306static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200307{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100308 if (trans->msc_a) {
309 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
310 "Handle paging error: transaction already associated with subscriber,"
311 " apparently it was already handled. Skip.\n");
312 return;
Harald Welte27989d42018-06-21 20:39:20 +0200313 }
314
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100315 if (msc_a) {
316 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
317 /* Assign conn */
318 msc_a_get(msc_a, MSC_A_USE_CC);
319 trans->msc_a = msc_a;
320 trans->paging_request = NULL;
321 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
322 /* send SETUP request to called party */
323 gsm48_cc_tx_setup(trans, &trans->cc.msg);
324 } else {
325 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
326 /* Temporarily out of order */
327 mncc_release_ind(trans->net, trans,
328 trans->callref,
329 GSM48_CAUSE_LOC_PRN_S_LU,
330 GSM48_CC_CAUSE_DEST_OOO);
331 trans->callref = 0;
332 trans->paging_request = NULL;
333 trans_free(trans);
334 }
Harald Welte27989d42018-06-21 20:39:20 +0200335}
336
337/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100338static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200339{
340 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
341 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100342 struct call_leg *cl1;
343 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200344
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100345 if (!trans1 || !trans2) {
346 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200347 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100348 }
Harald Welte27989d42018-06-21 20:39:20 +0200349
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100350 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100351 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
352 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 +0200353 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100354 }
355
356 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
357 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200358
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
360 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
361 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
362 * If we can't, then we must give up. */
363 cl1 = trans1->msc_a->cc.call_leg;
364 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200365
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100366 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200367}
368
369static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
370{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100371 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200372 return gsm48_cc_tx_status(trans, msg);
373}
374
Harald Welte27989d42018-06-21 20:39:20 +0200375static void gsm48_cc_timeout(void *arg)
376{
377 struct gsm_trans *trans = arg;
378 int disconnect = 0, release = 0;
379 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
380 int mo_location = GSM48_CAUSE_LOC_USER;
381 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
382 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
383 struct gsm_mncc mo_rel, l4_rel;
384
385 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
386 mo_rel.callref = trans->callref;
387 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
388 l4_rel.callref = trans->callref;
389
390 switch(trans->cc.Tcurrent) {
391 case 0x303:
392 release = 1;
393 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
394 break;
395 case 0x310:
396 disconnect = 1;
397 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
398 break;
399 case 0x313:
400 disconnect = 1;
401 /* unknown, did not find it in the specs */
402 break;
403 case 0x301:
404 disconnect = 1;
405 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
406 break;
407 case 0x308:
408 if (!trans->cc.T308_second) {
409 /* restart T308 a second time */
410 gsm48_cc_tx_release(trans, &trans->cc.msg);
411 trans->cc.T308_second = 1;
412 break; /* stay in release state */
413 }
414 trans_free(trans);
415 return;
416 case 0x306:
417 release = 1;
418 mo_cause = trans->cc.msg.cause.value;
419 mo_location = trans->cc.msg.cause.location;
420 break;
421 case 0x323:
422 disconnect = 1;
423 break;
424 default:
425 release = 1;
426 }
427
428 if (release && trans->callref) {
429 /* process release towards layer 4 */
430 mncc_release_ind(trans->net, trans, trans->callref,
431 l4_location, l4_cause);
432 trans->callref = 0;
433 }
434
435 if (disconnect && trans->callref) {
436 /* process disconnect towards layer 4 */
437 mncc_set_cause(&l4_rel, l4_location, l4_cause);
438 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
439 }
440
441 /* process disconnect towards mobile station */
442 if (disconnect || release) {
443 mncc_set_cause(&mo_rel, mo_location, mo_cause);
444 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
445 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
446 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
447 mo_rel.cause.diag_len = 3;
448
449 if (disconnect)
450 gsm48_cc_tx_disconnect(trans, &mo_rel);
451 if (release)
452 gsm48_cc_tx_release(trans, &mo_rel);
453 }
454
455}
456
457/* disconnect both calls from the bridge */
458static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100459 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200460{
461 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
462 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
463 struct gsm_mncc mx_rel;
464 if (!trans0 || !trans1)
465 return;
466
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100467 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
468 trans0->callref, trans1->callref, strerror(err));
469 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200470 trans0->callref, trans1->callref, strerror(err));
471
472 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
473 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
474 GSM48_CC_CAUSE_CHAN_UNACCEPT);
475
476 mx_rel.callref = trans0->callref;
477 gsm48_cc_tx_disconnect(trans0, &mx_rel);
478
479 mx_rel.callref = trans1->callref;
480 gsm48_cc_tx_disconnect(trans1, &mx_rel);
481}
482
483static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
484 int sec, int micro)
485{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100486 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200487 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
488 osmo_timer_schedule(&trans->cc.timer, sec, micro);
489 trans->cc.Tcurrent = current;
490}
491
492static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
493{
494 struct gsm48_hdr *gh = msgb_l3(msg);
495 uint8_t msg_type = gsm48_hdr_msg_type(gh);
496 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
497 struct tlv_parsed tp;
498 struct gsm_mncc setup;
499
Philipp Maier9ca7b312018-10-10 17:00:49 +0200500 gsm48_start_guard_timer(trans);
501
Harald Welte27989d42018-06-21 20:39:20 +0200502 memset(&setup, 0, sizeof(struct gsm_mncc));
503 setup.callref = trans->callref;
504
505 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
506 /* emergency setup is identified by msg_type */
507 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
508 setup.fields |= MNCC_F_EMERGENCY;
509 setup.emergency = 1;
510 /* use destination number as configured by user (if any) */
511 if (trans->net->emergency.route_to_msisdn) {
512 setup.fields |= MNCC_F_CALLED;
513 setup.called.type = 0; /* unknown */
514 setup.called.plan = 0; /* unknown */
515 OSMO_STRLCPY_ARRAY(setup.called.number,
516 trans->net->emergency.route_to_msisdn);
517 }
518 }
519
520 /* use subscriber as calling party number */
521 setup.fields |= MNCC_F_CALLING;
522 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
523 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
524
525 /* bearer capability */
526 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
527 setup.fields |= MNCC_F_BEARER_CAP;
528 gsm48_decode_bearer_cap(&setup.bearer_cap,
529 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
530
531 /* Create a copy of the bearer capability
532 * in the transaction struct, so we can use
533 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100534 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200535 sizeof(trans->bearer_cap));
536 }
537 /* facility */
538 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
539 setup.fields |= MNCC_F_FACILITY;
540 gsm48_decode_facility(&setup.facility,
541 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
542 }
543 /* called party bcd number */
544 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
545 setup.fields |= MNCC_F_CALLED;
546 gsm48_decode_called(&setup.called,
547 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
548 }
549 /* user-user */
550 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
551 setup.fields |= MNCC_F_USERUSER;
552 gsm48_decode_useruser(&setup.useruser,
553 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
554 }
555 /* ss-version */
556 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
557 setup.fields |= MNCC_F_SSVERSION;
558 gsm48_decode_ssversion(&setup.ssversion,
559 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
560 }
561 /* CLIR suppression */
562 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
563 setup.clir.sup = 1;
564 /* CLIR invocation */
565 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
566 setup.clir.inv = 1;
567 /* cc cap */
568 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
569 setup.fields |= MNCC_F_CCCAP;
570 gsm48_decode_cccap(&setup.cccap,
571 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
572 }
573
574 new_cc_state(trans, GSM_CSTATE_INITIATED);
575
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100576 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
577 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200578
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200579 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200580
581 /* indicate setup to MNCC */
582 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
583
584 /* MNCC code will modify the channel asynchronously, we should
585 * ipaccess-bind only after the modification has been made to the
586 * lchan->tch_mode */
587 return 0;
588}
589
590static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
591{
592 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
593 struct gsm48_hdr *gh;
594 struct gsm_mncc *setup = arg;
595 int rc, trans_id;
596
597 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
598
599 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700600 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100601 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200602 "This is not allowed!\n");
603 /* Temporarily out of order */
604 rc = mncc_release_ind(trans->net, trans, trans->callref,
605 GSM48_CAUSE_LOC_PRN_S_LU,
606 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
607 trans->callref = 0;
608 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200609 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200610 return rc;
611 }
612
613 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100614 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200615 if (trans_id < 0) {
616 /* no free transaction ID */
617 rc = mncc_release_ind(trans->net, trans, trans->callref,
618 GSM48_CAUSE_LOC_PRN_S_LU,
619 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
620 trans->callref = 0;
621 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200622 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200623 return rc;
624 }
625 trans->transaction_id = trans_id;
626
627 gh->msg_type = GSM48_MT_CC_SETUP;
628
629 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
630
631 /* bearer capability */
632 if (setup->fields & MNCC_F_BEARER_CAP) {
633 /* Create a copy of the bearer capability in the transaction struct, so we
634 * can use this information later */
635 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
636 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
637 }
638 /* facility */
639 if (setup->fields & MNCC_F_FACILITY)
640 gsm48_encode_facility(msg, 0, &setup->facility);
641 /* progress */
642 if (setup->fields & MNCC_F_PROGRESS)
643 gsm48_encode_progress(msg, 0, &setup->progress);
644 /* calling party BCD number */
645 if (setup->fields & MNCC_F_CALLING)
646 gsm48_encode_calling(msg, &setup->calling);
647 /* called party BCD number */
648 if (setup->fields & MNCC_F_CALLED)
649 gsm48_encode_called(msg, &setup->called);
650 /* user-user */
651 if (setup->fields & MNCC_F_USERUSER)
652 gsm48_encode_useruser(msg, 0, &setup->useruser);
653 /* redirecting party BCD number */
654 if (setup->fields & MNCC_F_REDIRECTING)
655 gsm48_encode_redirecting(msg, &setup->redirecting);
656 /* signal */
657 if (setup->fields & MNCC_F_SIGNAL)
658 gsm48_encode_signal(msg, setup->signal);
659
660 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
661
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200662 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200663
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100664 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200665}
666
667static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
668{
669 struct gsm48_hdr *gh = msgb_l3(msg);
670 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
671 struct tlv_parsed tp;
672 struct gsm_mncc call_conf;
673 int rc;
674
675 gsm48_stop_cc_timer(trans);
676 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
677
678 memset(&call_conf, 0, sizeof(struct gsm_mncc));
679 call_conf.callref = trans->callref;
680
681 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
682#if 0
683 /* repeat */
684 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
685 call_conf.repeat = 1;
686 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
687 call_conf.repeat = 2;
688#endif
689 /* bearer capability */
690 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
691 call_conf.fields |= MNCC_F_BEARER_CAP;
692 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
693 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
694
695 /* Create a copy of the bearer capability
696 * in the transaction struct, so we can use
697 * this information later */
698 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
699 sizeof(trans->bearer_cap));
700 }
701 /* cause */
702 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
703 call_conf.fields |= MNCC_F_CAUSE;
704 gsm48_decode_cause(&call_conf.cause,
705 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
706 }
707 /* cc cap */
708 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
709 call_conf.fields |= MNCC_F_CCCAP;
710 gsm48_decode_cccap(&call_conf.cccap,
711 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
712 }
713
714 /* IMSI of called subscriber */
715 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
716
717 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
718
719 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100720 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200721
722 /* don't continue, if there were problems with
723 * the call assignment. */
724 if (rc)
725 return rc;
726
727 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
728 &call_conf);
729}
730
731static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
732{
733 struct gsm_mncc *proceeding = arg;
734 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
735 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
736 int rc;
737
738 gh->msg_type = GSM48_MT_CC_CALL_PROC;
739
740 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
741
742 /* bearer capability */
743 if (proceeding->fields & MNCC_F_BEARER_CAP) {
744 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
745 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
746 }
747 /* facility */
748 if (proceeding->fields & MNCC_F_FACILITY)
749 gsm48_encode_facility(msg, 0, &proceeding->facility);
750 /* progress */
751 if (proceeding->fields & MNCC_F_PROGRESS)
752 gsm48_encode_progress(msg, 0, &proceeding->progress);
753
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100754 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200755 if (rc)
756 return rc;
757
758 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100759 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200760}
761
762static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
763{
764 struct gsm48_hdr *gh = msgb_l3(msg);
765 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
766 struct tlv_parsed tp;
767 struct gsm_mncc alerting;
768
769 gsm48_stop_cc_timer(trans);
770 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
771
772 memset(&alerting, 0, sizeof(struct gsm_mncc));
773 alerting.callref = trans->callref;
774 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
775 /* facility */
776 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
777 alerting.fields |= MNCC_F_FACILITY;
778 gsm48_decode_facility(&alerting.facility,
779 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
780 }
781
782 /* progress */
783 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
784 alerting.fields |= MNCC_F_PROGRESS;
785 gsm48_decode_progress(&alerting.progress,
786 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
787 }
788 /* ss-version */
789 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
790 alerting.fields |= MNCC_F_SSVERSION;
791 gsm48_decode_ssversion(&alerting.ssversion,
792 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
793 }
794
795 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
796
797 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
798 &alerting);
799}
800
801static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
802{
803 struct gsm_mncc *alerting = arg;
804 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
805 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
806
807 gh->msg_type = GSM48_MT_CC_ALERTING;
808
809 /* facility */
810 if (alerting->fields & MNCC_F_FACILITY)
811 gsm48_encode_facility(msg, 0, &alerting->facility);
812 /* progress */
813 if (alerting->fields & MNCC_F_PROGRESS)
814 gsm48_encode_progress(msg, 0, &alerting->progress);
815 /* user-user */
816 if (alerting->fields & MNCC_F_USERUSER)
817 gsm48_encode_useruser(msg, 0, &alerting->useruser);
818
819 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
820
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100821 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200822}
823
824static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
825{
826 struct gsm_mncc *progress = arg;
827 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
828 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
829
830 gh->msg_type = GSM48_MT_CC_PROGRESS;
831
832 /* progress */
833 gsm48_encode_progress(msg, 1, &progress->progress);
834 /* user-user */
835 if (progress->fields & MNCC_F_USERUSER)
836 gsm48_encode_useruser(msg, 0, &progress->useruser);
837
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100838 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200839}
840
841static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
842{
843 struct gsm_mncc *connect = arg;
844 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
845 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
846
847 gh->msg_type = GSM48_MT_CC_CONNECT;
848
849 gsm48_stop_cc_timer(trans);
850 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
851
852 /* facility */
853 if (connect->fields & MNCC_F_FACILITY)
854 gsm48_encode_facility(msg, 0, &connect->facility);
855 /* progress */
856 if (connect->fields & MNCC_F_PROGRESS)
857 gsm48_encode_progress(msg, 0, &connect->progress);
858 /* connected number */
859 if (connect->fields & MNCC_F_CONNECTED)
860 gsm48_encode_connected(msg, &connect->connected);
861 /* user-user */
862 if (connect->fields & MNCC_F_USERUSER)
863 gsm48_encode_useruser(msg, 0, &connect->useruser);
864
865 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
866
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100867 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200868}
869
870static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
871{
872 struct gsm48_hdr *gh = msgb_l3(msg);
873 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
874 struct tlv_parsed tp;
875 struct gsm_mncc connect;
876
877 gsm48_stop_cc_timer(trans);
878
879 memset(&connect, 0, sizeof(struct gsm_mncc));
880 connect.callref = trans->callref;
881 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
882 /* use subscriber as connected party number */
883 connect.fields |= MNCC_F_CONNECTED;
884 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
885 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
886
887 /* facility */
888 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
889 connect.fields |= MNCC_F_FACILITY;
890 gsm48_decode_facility(&connect.facility,
891 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
892 }
893 /* user-user */
894 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
895 connect.fields |= MNCC_F_USERUSER;
896 gsm48_decode_useruser(&connect.useruser,
897 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
898 }
899 /* ss-version */
900 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
901 connect.fields |= MNCC_F_SSVERSION;
902 gsm48_decode_ssversion(&connect.ssversion,
903 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
904 }
905
906 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200907 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +0200908
909 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
910}
911
912
913static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
914{
915 struct gsm_mncc connect_ack;
916
917 gsm48_stop_cc_timer(trans);
918
919 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200920 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 +0200921
922 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
923 connect_ack.callref = trans->callref;
924
925 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
926 &connect_ack);
927}
928
929static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
930{
931 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
932 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
933
934 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
935
936 new_cc_state(trans, GSM_CSTATE_ACTIVE);
937
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100938 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200939}
940
941static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
942{
943 struct gsm48_hdr *gh = msgb_l3(msg);
944 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
945 struct tlv_parsed tp;
946 struct gsm_mncc disc;
947
948 gsm48_stop_cc_timer(trans);
949
950 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
951
952 memset(&disc, 0, sizeof(struct gsm_mncc));
953 disc.callref = trans->callref;
954 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
955 /* cause */
956 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
957 disc.fields |= MNCC_F_CAUSE;
958 gsm48_decode_cause(&disc.cause,
959 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
960 }
961 /* facility */
962 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
963 disc.fields |= MNCC_F_FACILITY;
964 gsm48_decode_facility(&disc.facility,
965 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
966 }
967 /* user-user */
968 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
969 disc.fields |= MNCC_F_USERUSER;
970 gsm48_decode_useruser(&disc.useruser,
971 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
972 }
973 /* ss-version */
974 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
975 disc.fields |= MNCC_F_SSVERSION;
976 gsm48_decode_ssversion(&disc.ssversion,
977 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
978 }
979
980 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +0200981}
982
983static struct gsm_mncc_cause default_cause = {
984 .location = GSM48_CAUSE_LOC_PRN_S_LU,
985 .coding = 0,
986 .rec = 0,
987 .rec_val = 0,
988 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
989 .diag_len = 0,
990 .diag = { 0 },
991};
992
993static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
994{
995 struct gsm_mncc *disc = arg;
996 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
997 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
998
999 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1000
1001 gsm48_stop_cc_timer(trans);
1002 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1003
1004 /* cause */
1005 if (disc->fields & MNCC_F_CAUSE)
1006 gsm48_encode_cause(msg, 1, &disc->cause);
1007 else
1008 gsm48_encode_cause(msg, 1, &default_cause);
1009
1010 /* facility */
1011 if (disc->fields & MNCC_F_FACILITY)
1012 gsm48_encode_facility(msg, 0, &disc->facility);
1013 /* progress */
1014 if (disc->fields & MNCC_F_PROGRESS)
1015 gsm48_encode_progress(msg, 0, &disc->progress);
1016 /* user-user */
1017 if (disc->fields & MNCC_F_USERUSER)
1018 gsm48_encode_useruser(msg, 0, &disc->useruser);
1019
1020 /* store disconnect cause for T306 expiry */
1021 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1022
1023 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1024
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001025 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001026}
1027
1028static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1029{
1030 struct gsm48_hdr *gh = msgb_l3(msg);
1031 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1032 struct tlv_parsed tp;
1033 struct gsm_mncc rel;
1034 int rc;
1035
1036 gsm48_stop_cc_timer(trans);
1037
1038 memset(&rel, 0, sizeof(struct gsm_mncc));
1039 rel.callref = trans->callref;
1040 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1041 /* cause */
1042 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1043 rel.fields |= MNCC_F_CAUSE;
1044 gsm48_decode_cause(&rel.cause,
1045 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1046 }
1047 /* facility */
1048 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1049 rel.fields |= MNCC_F_FACILITY;
1050 gsm48_decode_facility(&rel.facility,
1051 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1052 }
1053 /* user-user */
1054 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1055 rel.fields |= MNCC_F_USERUSER;
1056 gsm48_decode_useruser(&rel.useruser,
1057 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1058 }
1059 /* ss-version */
1060 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1061 rel.fields |= MNCC_F_SSVERSION;
1062 gsm48_decode_ssversion(&rel.ssversion,
1063 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1064 }
1065
1066 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1067 /* release collision 5.4.5 */
1068 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1069 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001070 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001071 GSM48_PDISC_CC | (trans->transaction_id << 4),
1072 GSM48_MT_CC_RELEASE_COMPL);
1073 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1074 }
1075
1076 new_cc_state(trans, GSM_CSTATE_NULL);
1077
1078 trans->callref = 0;
1079 trans_free(trans);
1080
1081 return rc;
1082}
1083
1084static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1085{
1086 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001087 struct msgb *msg;
1088 struct gsm48_hdr *gh;
1089
1090 if (!trans->msc_a) {
1091 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1092 return -EINVAL;
1093 }
1094
1095 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1096 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001097
1098 gh->msg_type = GSM48_MT_CC_RELEASE;
1099
1100 gsm48_stop_cc_timer(trans);
1101 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1102
1103 /* cause */
1104 if (rel->fields & MNCC_F_CAUSE)
1105 gsm48_encode_cause(msg, 0, &rel->cause);
1106 /* facility */
1107 if (rel->fields & MNCC_F_FACILITY)
1108 gsm48_encode_facility(msg, 0, &rel->facility);
1109 /* user-user */
1110 if (rel->fields & MNCC_F_USERUSER)
1111 gsm48_encode_useruser(msg, 0, &rel->useruser);
1112
1113 trans->cc.T308_second = 0;
1114 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1115
1116 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1117 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1118
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001119 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001120}
1121
1122static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1123{
1124 struct gsm48_hdr *gh = msgb_l3(msg);
1125 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1126 struct tlv_parsed tp;
1127 struct gsm_mncc rel;
1128 int rc = 0;
1129
1130 gsm48_stop_cc_timer(trans);
1131
1132 memset(&rel, 0, sizeof(struct gsm_mncc));
1133 rel.callref = trans->callref;
1134 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1135 /* cause */
1136 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1137 rel.fields |= MNCC_F_CAUSE;
1138 gsm48_decode_cause(&rel.cause,
1139 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1140 }
1141 /* facility */
1142 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1143 rel.fields |= MNCC_F_FACILITY;
1144 gsm48_decode_facility(&rel.facility,
1145 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1146 }
1147 /* user-user */
1148 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1149 rel.fields |= MNCC_F_USERUSER;
1150 gsm48_decode_useruser(&rel.useruser,
1151 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1152 }
1153 /* ss-version */
1154 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1155 rel.fields |= MNCC_F_SSVERSION;
1156 gsm48_decode_ssversion(&rel.ssversion,
1157 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1158 }
1159
1160 if (trans->callref) {
1161 switch (trans->cc.state) {
1162 case GSM_CSTATE_CALL_PRESENT:
1163 rc = mncc_recvmsg(trans->net, trans,
1164 MNCC_REJ_IND, &rel);
1165 break;
1166 case GSM_CSTATE_RELEASE_REQ:
1167 rc = mncc_recvmsg(trans->net, trans,
1168 MNCC_REL_CNF, &rel);
1169 break;
1170 default:
1171 rc = mncc_recvmsg(trans->net, trans,
1172 MNCC_REL_IND, &rel);
1173 }
1174 }
1175
1176 trans->callref = 0;
1177 trans_free(trans);
1178
1179 return rc;
1180}
1181
1182static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1183{
1184 struct gsm_mncc *rel = arg;
1185 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1186 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1187 int ret;
1188
1189 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1190
1191 trans->callref = 0;
1192
1193 gsm48_stop_cc_timer(trans);
1194
1195 /* cause */
1196 if (rel->fields & MNCC_F_CAUSE)
1197 gsm48_encode_cause(msg, 0, &rel->cause);
1198 /* facility */
1199 if (rel->fields & MNCC_F_FACILITY)
1200 gsm48_encode_facility(msg, 0, &rel->facility);
1201 /* user-user */
1202 if (rel->fields & MNCC_F_USERUSER)
1203 gsm48_encode_useruser(msg, 0, &rel->useruser);
1204
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001205 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001206
1207 trans_free(trans);
1208
1209 return ret;
1210}
1211
1212static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1213{
1214 struct gsm48_hdr *gh = msgb_l3(msg);
1215 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1216 struct tlv_parsed tp;
1217 struct gsm_mncc fac;
1218
1219 memset(&fac, 0, sizeof(struct gsm_mncc));
1220 fac.callref = trans->callref;
1221 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1222 /* facility */
1223 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1224 fac.fields |= MNCC_F_FACILITY;
1225 gsm48_decode_facility(&fac.facility,
1226 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1227 }
1228 /* ss-version */
1229 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1230 fac.fields |= MNCC_F_SSVERSION;
1231 gsm48_decode_ssversion(&fac.ssversion,
1232 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1233 }
1234
1235 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1236}
1237
1238static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1239{
1240 struct gsm_mncc *fac = arg;
1241 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1242 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1243
1244 gh->msg_type = GSM48_MT_CC_FACILITY;
1245
1246 /* facility */
1247 gsm48_encode_facility(msg, 1, &fac->facility);
1248
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001249 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001250}
1251
1252static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1253{
1254 struct gsm_mncc hold;
1255
1256 memset(&hold, 0, sizeof(struct gsm_mncc));
1257 hold.callref = trans->callref;
1258 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1259}
1260
1261static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1262{
1263 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1264 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1265
1266 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1267
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001268 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001269}
1270
1271static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1272{
1273 struct gsm_mncc *hold_rej = arg;
1274 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1275 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1276
1277 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1278
1279 /* cause */
1280 if (hold_rej->fields & MNCC_F_CAUSE)
1281 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1282 else
1283 gsm48_encode_cause(msg, 1, &default_cause);
1284
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001285 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001286}
1287
1288static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1289{
1290 struct gsm_mncc retrieve;
1291
1292 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1293 retrieve.callref = trans->callref;
1294 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1295 &retrieve);
1296}
1297
1298static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1299{
1300 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1301 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1302
1303 gh->msg_type = GSM48_MT_CC_RETR_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_retrieve_rej(struct gsm_trans *trans, void *arg)
1309{
1310 struct gsm_mncc *retrieve_rej = arg;
1311 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1312 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1313
1314 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1315
1316 /* cause */
1317 if (retrieve_rej->fields & MNCC_F_CAUSE)
1318 gsm48_encode_cause(msg, 1, &retrieve_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_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1326{
1327 struct gsm48_hdr *gh = msgb_l3(msg);
1328 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1329 struct tlv_parsed tp;
1330 struct gsm_mncc dtmf;
1331
1332 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1333 dtmf.callref = trans->callref;
1334 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1335 /* keypad facility */
1336 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1337 dtmf.fields |= MNCC_F_KEYPAD;
1338 gsm48_decode_keypad(&dtmf.keypad,
1339 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1340 }
1341
1342 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1343}
1344
1345static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1346{
1347 struct gsm_mncc *dtmf = arg;
1348 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1349 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1350
1351 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1352
1353 /* keypad */
1354 if (dtmf->fields & MNCC_F_KEYPAD)
1355 gsm48_encode_keypad(msg, dtmf->keypad);
1356
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001357 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001358}
1359
1360static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1361{
1362 struct gsm_mncc *dtmf = arg;
1363 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1364 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1365
1366 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1367
1368 /* cause */
1369 if (dtmf->fields & MNCC_F_CAUSE)
1370 gsm48_encode_cause(msg, 1, &dtmf->cause);
1371 else
1372 gsm48_encode_cause(msg, 1, &default_cause);
1373
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001374 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001375}
1376
1377static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1378{
1379 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1380 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1381
1382 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1383
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001384 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001385}
1386
1387static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1388{
1389 struct gsm_mncc dtmf;
1390
1391 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1392 dtmf.callref = trans->callref;
1393
1394 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1395}
1396
1397static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1398{
1399 struct gsm48_hdr *gh = msgb_l3(msg);
1400 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1401 struct tlv_parsed tp;
1402 struct gsm_mncc modify;
1403
1404 memset(&modify, 0, sizeof(struct gsm_mncc));
1405 modify.callref = trans->callref;
1406 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1407 /* bearer capability */
1408 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1409 modify.fields |= MNCC_F_BEARER_CAP;
1410 gsm48_decode_bearer_cap(&modify.bearer_cap,
1411 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1412
1413 /* Create a copy of the bearer capability
1414 * in the transaction struct, so we can use
1415 * this information later */
1416 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1417 sizeof(trans->bearer_cap));
1418 }
1419
1420 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1421
1422 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1423}
1424
1425static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1426{
1427 struct gsm_mncc *modify = arg;
1428 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1429 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1430
1431 gh->msg_type = GSM48_MT_CC_MODIFY;
1432
1433 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1434
1435 /* bearer capability */
1436 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1437 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1438
1439 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1440
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001441 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001442}
1443
1444static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1445{
1446 struct gsm48_hdr *gh = msgb_l3(msg);
1447 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1448 struct tlv_parsed tp;
1449 struct gsm_mncc modify;
1450
1451 gsm48_stop_cc_timer(trans);
1452
1453 memset(&modify, 0, sizeof(struct gsm_mncc));
1454 modify.callref = trans->callref;
1455 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1456 /* bearer capability */
1457 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1458 modify.fields |= MNCC_F_BEARER_CAP;
1459 gsm48_decode_bearer_cap(&modify.bearer_cap,
1460 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1461
1462 /* Create a copy of the bearer capability
1463 * in the transaction struct, so we can use
1464 * this information later */
1465 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1466 sizeof(trans->bearer_cap));
1467 }
1468
1469 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1470
1471 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1472}
1473
1474static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1475{
1476 struct gsm_mncc *modify = arg;
1477 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1478 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1479
1480 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1481
1482 /* bearer capability */
1483 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1484 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1485
1486 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1487
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001488 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001489}
1490
1491static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1492{
1493 struct gsm48_hdr *gh = msgb_l3(msg);
1494 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1495 struct tlv_parsed tp;
1496 struct gsm_mncc modify;
1497
1498 gsm48_stop_cc_timer(trans);
1499
1500 memset(&modify, 0, sizeof(struct gsm_mncc));
1501 modify.callref = trans->callref;
1502 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1503 /* bearer capability */
1504 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1505 modify.fields |= GSM48_IE_BEARER_CAP;
1506 gsm48_decode_bearer_cap(&modify.bearer_cap,
1507 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1508
1509 /* Create a copy of the bearer capability
1510 * in the transaction struct, so we can use
1511 * this information later */
1512 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1513 sizeof(trans->bearer_cap));
1514 }
1515 /* cause */
1516 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1517 modify.fields |= MNCC_F_CAUSE;
1518 gsm48_decode_cause(&modify.cause,
1519 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1520 }
1521
1522 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1523
1524 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1525}
1526
1527static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1528{
1529 struct gsm_mncc *modify = arg;
1530 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1531 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1532
1533 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1534
1535 /* bearer capability */
1536 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1537 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1538 /* cause */
1539 gsm48_encode_cause(msg, 1, &modify->cause);
1540
1541 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1542
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001543 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001544}
1545
1546static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1547{
1548 struct gsm_mncc *notify = arg;
1549 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1550 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1551
1552 gh->msg_type = GSM48_MT_CC_NOTIFY;
1553
1554 /* notify */
1555 gsm48_encode_notify(msg, notify->notify);
1556
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001557 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001558}
1559
1560static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1561{
1562 struct gsm48_hdr *gh = msgb_l3(msg);
1563 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1564// struct tlv_parsed tp;
1565 struct gsm_mncc notify;
1566
1567 memset(&notify, 0, sizeof(struct gsm_mncc));
1568 notify.callref = trans->callref;
1569// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1570 if (payload_len >= 1)
1571 gsm48_decode_notify(&notify.notify, gh->data);
1572
1573 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1574}
1575
1576static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1577{
1578 struct gsm_mncc *user = arg;
1579 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1580 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1581
1582 gh->msg_type = GSM48_MT_CC_USER_INFO;
1583
1584 /* user-user */
1585 if (user->fields & MNCC_F_USERUSER)
1586 gsm48_encode_useruser(msg, 1, &user->useruser);
1587 /* more data */
1588 if (user->more)
1589 gsm48_encode_more(msg);
1590
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001591 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001592}
1593
1594static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1595{
1596 struct gsm48_hdr *gh = msgb_l3(msg);
1597 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1598 struct tlv_parsed tp;
1599 struct gsm_mncc user;
1600
1601 memset(&user, 0, sizeof(struct gsm_mncc));
1602 user.callref = trans->callref;
1603 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1604 /* user-user */
1605 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1606 user.fields |= MNCC_F_USERUSER;
1607 gsm48_decode_useruser(&user.useruser,
1608 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1609 }
1610 /* more data */
1611 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1612 user.more = 1;
1613
1614 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1615}
1616
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001617static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1618 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1619 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001620{
1621 uint8_t data[sizeof(struct gsm_mncc)];
1622 struct gsm_mncc_rtp *rtp;
1623
1624 memset(&data, 0, sizeof(data));
1625 rtp = (struct gsm_mncc_rtp *) &data[0];
1626
1627 rtp->callref = callref;
1628 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001629 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001630 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1631 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001632 }
Harald Welte27989d42018-06-21 20:39:20 +02001633 rtp->payload_type = payload_type;
1634 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001635 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001636}
1637
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001638static 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 +02001639{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001640 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001641}
1642
1643static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1644{
1645 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001646
1647 /* Find callref */
1648 trans = trans_find_by_callref(net, callref);
1649 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001650 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001651 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001652 return -EIO;
1653 }
1654 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001655 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001656 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001657 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001658 return 0;
1659 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001660 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001661
Harald Welte27989d42018-06-21 20:39:20 +02001662 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001663 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001664}
1665
1666/* Trigger TCH_RTP_CREATE acknowledgement */
1667int gsm48_tch_rtp_create(struct gsm_trans *trans)
1668{
1669 /* This function is called as soon as the port, on which the
1670 * mgcp-gw expects the incoming RTP stream from the remote
1671 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001672 struct msc_a *msc_a = trans->msc_a;
1673 struct gsm_network *net = msc_a_net(msc_a);
1674 struct call_leg *cl = msc_a->cc.call_leg;
1675 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001676 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1677 uint32_t payload_type;
1678 int payload_msg_type;
1679 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001680
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001681 if (!rtp_cn) {
1682 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1683 return -EINVAL;
1684 }
1685
1686 if (!rtp_cn->codec_known) {
1687 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1688 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1689 return -EINVAL;
1690 }
1691
1692 /* Codec */
1693 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1694
1695 /* Payload Type number */
1696 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001697 if (mgcp_info && mgcp_info->ptmap_len)
1698 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1699 else
1700 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001701
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001702 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1703 if (!rtp_cn_local) {
1704 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1705 return -EINVAL;
1706 }
1707
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001708 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 +02001709}
1710
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001711static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001712{
1713 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001714 struct call_leg *cl;
1715 struct rtp_stream *rtps;
1716 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001717 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001718
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001719 /* FIXME: in *rtp we should get the codec information of the remote
1720 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1721 * meaningful value based on this information but unfortunately we
1722 * can't do that yet because the mncc API can not signal dynamic
1723 * payload types yet. This must be fixed first. Also there may be
1724 * additional members necessary in trans->conn->rtp because we
1725 * somehow need to deal with dynamic payload types that do not
1726 * comply to 3gpp's assumptions of payload type numbers on the A
1727 * interface. See also related tickets: OS#3399 and OS1683 */
1728
Harald Welte27989d42018-06-21 20:39:20 +02001729 /* Find callref */
1730 trans = trans_find_by_callref(net, rtp->callref);
1731 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001732 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001733 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001734 return -EIO;
1735 }
1736 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001737 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001738 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001739 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001740 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001741 }
1742
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001743 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(MNCC_RTP_CONNECT),
1744 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1745 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001746
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001747 cl = trans->msc_a->cc.call_leg;
1748 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1749
1750 if (!rtps) {
1751 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1752 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1753 return -EINVAL;
1754 }
1755
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001756 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1757 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1758 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1759 return -EINVAL;
1760 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001761 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1762 rtp_stream_commit(rtps);
1763 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001764}
1765
1766static struct downstate {
1767 uint32_t states;
1768 int type;
1769 int (*rout) (struct gsm_trans *trans, void *arg);
1770} downstatelist[] = {
1771 /* mobile originating call establishment */
1772 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1773 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1774 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1775 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1776 {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 */
1777 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1778 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1779 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1780 /* mobile terminating call establishment */
1781 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1782 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1783 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1784 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1785 /* signalling during call */
1786 {SBIT(GSM_CSTATE_ACTIVE),
1787 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1788 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1789 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1790 {ALL_STATES,
1791 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1792 {ALL_STATES,
1793 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1794 {ALL_STATES,
1795 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1796 {SBIT(GSM_CSTATE_ACTIVE),
1797 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1798 {SBIT(GSM_CSTATE_ACTIVE),
1799 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1800 {SBIT(GSM_CSTATE_ACTIVE),
1801 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1802 {SBIT(GSM_CSTATE_ACTIVE),
1803 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1804 {SBIT(GSM_CSTATE_ACTIVE),
1805 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1806 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1807 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1808 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1809 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1810 {SBIT(GSM_CSTATE_ACTIVE),
1811 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1812 /* clearing */
1813 {SBIT(GSM_CSTATE_INITIATED),
1814 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1815 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1816 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1817 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1818 MNCC_REL_REQ, gsm48_cc_tx_release},
1819};
1820
1821#define DOWNSLLEN \
1822 (sizeof(downstatelist) / sizeof(struct downstate))
1823
1824
Philipp Maiercd64af72019-08-01 09:46:40 +02001825static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001826{
1827 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001828 struct msc_a *msc_a = NULL;
1829 struct gsm_trans *trans = NULL;
1830 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001831
Harald Welte27989d42018-06-21 20:39:20 +02001832 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001833 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001834 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001835 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001836 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001837 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001838 return rc;
1839 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001840 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001841 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001842 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001843 case MNCC_RTP_FREE:
1844 /* unused right now */
1845 return -EIO;
1846
1847 case MNCC_FRAME_DROP:
1848 case MNCC_FRAME_RECV:
1849 case GSM_TCHF_FRAME:
1850 case GSM_TCHF_FRAME_EFR:
1851 case GSM_TCHH_FRAME:
1852 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001853 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001854 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001855 return -ENOTSUP;
1856 }
1857
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001858 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001859
1860 /* Find callref */
1861 trans = trans_find_by_callref(net, data->callref);
1862
1863 /* Callref unknown */
1864 if (!trans) {
1865 struct vlr_subscr *vsub;
1866
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001867 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001868 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001869 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001870 /* Invalid call reference */
1871 return mncc_release_ind(net, NULL, data->callref,
1872 GSM48_CAUSE_LOC_PRN_S_LU,
1873 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1874 }
1875 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001876 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001877 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001878 /* Invalid number */
1879 return mncc_release_ind(net, NULL, data->callref,
1880 GSM48_CAUSE_LOC_PRN_S_LU,
1881 GSM48_CC_CAUSE_INV_NR_FORMAT);
1882 }
1883 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001884 if (data->called.number[0]) {
1885 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1886 if (!vsub)
1887 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001888 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001889 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001890 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001891 if (!vsub)
1892 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001893 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001894 }
1895 if (!vsub)
1896 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001897 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001898 /* update the subscriber we deal with */
1899 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1900
Harald Welte27989d42018-06-21 20:39:20 +02001901 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001902 if (!vsub->lu_complete) {
1903 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001904 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001905 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001906 /* Temporarily out of order */
1907 return mncc_release_ind(net, NULL, data->callref,
1908 GSM48_CAUSE_LOC_PRN_S_LU,
1909 GSM48_CC_CAUSE_DEST_OOO);
1910 }
Keith Whyte991bb422019-08-08 15:43:40 +02001911
1912 /* Find valid conn */
1913 msc_a = msc_a_for_vsub(vsub, true);
1914
1915 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1916 if (!net->call_waiting && msc_a) {
1917 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1918 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1919 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1920 "rx '%s' for subscriber %s with trans state (%s)"
1921 " rejecting with USER_BUSY\n",
1922 get_mncc_name(msg->msg_type), data->called.number,
1923 gsm48_cc_state_name(existing_cc_trans->cc.state));
1924 return mncc_release_ind(net, NULL, data->callref,
1925 GSM48_CAUSE_LOC_PRN_S_LU,
1926 GSM48_CC_CAUSE_USER_BUSY);
1927 }
1928 }
1929
Harald Welte27989d42018-06-21 20:39:20 +02001930 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001931 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001932 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001933 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001934 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001935 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001936 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001937 mncc_release_ind(net, NULL, data->callref,
1938 GSM48_CAUSE_LOC_PRN_S_LU,
1939 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1940 return -ENOMEM;
1941 }
1942
Harald Welte27989d42018-06-21 20:39:20 +02001943 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001944 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001945 /* This condition will return before the common logging of the received MNCC message below, so
1946 * log it now. */
1947 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001948
Harald Welte27989d42018-06-21 20:39:20 +02001949 /* store setup information until paging succeeds */
1950 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1951
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001952 /* Request a channel. If Paging already started, paging_request_start() will append the new
1953 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001954 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1955 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001956 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001957 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001958 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001959 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001960 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001961 return 0;
1962 }
1963
1964 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001965 trans->msc_a = msc_a;
1966 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001967 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001968 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001969 } else {
1970 /* update the subscriber we deal with */
1971 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1972 }
1973
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001974 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001975
Philipp Maier9ca7b312018-10-10 17:00:49 +02001976 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02001977 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02001978
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001979 if (trans->msc_a)
1980 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02001981
1982 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001983 if (!msc_a) {
1984 struct gsm_mncc rel = {
1985 .callref = data->callref,
1986 };
1987 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001988 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
1989 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001990 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02001991 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
1992 else
1993 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1994 trans->callref = 0;
1995 trans_free(trans);
1996 return rc;
1997 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001998 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001999 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002000 }
2001
2002 /* Find function for current state and message */
2003 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002004 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002005 && ((1 << trans->cc.state) & downstatelist[i].states))
2006 break;
2007 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002008 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002009 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002010 return 0;
2011 }
2012
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002013 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002014
2015 return rc;
2016}
2017
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002018struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2019{
2020 uint32_t callref;
2021
2022 switch (msg->msg_type) {
2023 case MNCC_BRIDGE:
2024 callref = msg->bridge.callref[0];
2025 break;
2026 case MNCC_RTP_CREATE:
2027 case MNCC_RTP_CONNECT:
2028 callref = msg->rtp.callref;
2029 break;
2030
2031 case MNCC_RTP_FREE:
2032 case MNCC_FRAME_DROP:
2033 case MNCC_FRAME_RECV:
2034 case GSM_TCHF_FRAME:
2035 case GSM_TCHF_FRAME_EFR:
2036 case GSM_TCHH_FRAME:
2037 case GSM_TCH_FRAME_AMR:
2038 return NULL;
2039
2040 default:
2041 callref = msg->signal.callref;
2042 break;
2043 }
2044
2045 return mncc_call_find_by_callref(callref);
2046}
2047
2048/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002049int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002050{
2051 const union mncc_msg *msg = arg;
2052 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002053
2054 if (msg->msg_type == MNCC_SETUP_REQ) {
2055 /* Incoming call to forward for inter-MSC Handover? */
2056 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2057 if (mncc_call)
2058 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2059 "Incoming call matches pending inter-MSC Handover Number\n");
2060 }
2061 if (!mncc_call) {
2062 /* Find already active MNCC FSM for this callref.
2063 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2064 * MNCC<->GSM-CC call handling. */
2065 mncc_call = mncc_find_by_callref_from_msg(msg);
2066 }
2067 if (mncc_call) {
2068 mncc_call_rx(mncc_call, msg);
2069 return 0;
2070 }
2071
2072 /* None of the above? Then it must be a normal GSM CC call related message. */
2073 return mncc_tx_to_gsm_cc(net, msg);
2074}
Harald Welte27989d42018-06-21 20:39:20 +02002075
2076static struct datastate {
2077 uint32_t states;
2078 int type;
2079 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2080} datastatelist[] = {
2081 /* mobile originating call establishment */
2082 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2083 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2084 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2085 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2086 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2087 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2088 /* mobile terminating call establishment */
2089 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2090 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2091 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2092 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2093 {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 */
2094 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2095 /* signalling during call */
2096 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2097 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2098 {SBIT(GSM_CSTATE_ACTIVE),
2099 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2100 {ALL_STATES,
2101 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2102 {ALL_STATES,
2103 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2104 {ALL_STATES,
2105 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2106 {SBIT(GSM_CSTATE_ACTIVE),
2107 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2108 {SBIT(GSM_CSTATE_ACTIVE),
2109 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2110 {SBIT(GSM_CSTATE_ACTIVE),
2111 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2112 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2113 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2114 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2115 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2116 {SBIT(GSM_CSTATE_ACTIVE),
2117 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2118 /* clearing */
2119 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2120 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2121 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2122 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2123 {ALL_STATES, /* 5.4.3.4 */
2124 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2125};
2126
2127#define DATASLLEN \
2128 (sizeof(datastatelist) / sizeof(struct datastate))
2129
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002130int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002131{
2132 struct gsm48_hdr *gh = msgb_l3(msg);
2133 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2134 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2135 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002136 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2137 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002138 int i, rc = 0;
2139
2140 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002141 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002142 return -EINVAL;
2143 }
2144
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002145 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002146 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002147 return -EINVAL;
2148 }
2149
2150 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002151 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002152
Harald Welte27989d42018-06-21 20:39:20 +02002153 /* Create transaction */
2154 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002155 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002156 trans = trans_alloc(net, vsub,
2157 TRANS_CC,
2158 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002159 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002160 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002161 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002162 GSM48_PDISC_CC | (transaction_id << 4),
2163 GSM48_MT_CC_RELEASE_COMPL);
2164 return -ENOMEM;
2165 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002166 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2167 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2168 trans_free(trans);
2169 return -EINVAL;
2170 }
2171
Harald Welte27989d42018-06-21 20:39:20 +02002172 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002173 msc_a_get(msc_a, MSC_A_USE_CC);
2174 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002175 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002176
2177 /* An earlier CM Service Request for this CC message now has concluded */
2178 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2179 LOG_MSC_A(msc_a, LOGL_ERROR,
2180 "Creating new CC transaction without prior CM Service Request\n");
2181 else
2182 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002183 }
2184
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002185 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2186 gsm48_cc_state_name(trans->cc.state));
2187
Harald Welte27989d42018-06-21 20:39:20 +02002188 /* find function for current state and message */
2189 for (i = 0; i < DATASLLEN; i++)
2190 if ((msg_type == datastatelist[i].type)
2191 && ((1 << trans->cc.state) & datastatelist[i].states))
2192 break;
2193 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002194 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002195
2196 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2197 * transaction right away. */
2198 if (trans->cc.state == GSM_CSTATE_NULL) {
2199 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2200 " -- disarding new CC transaction right away\n");
2201 trans_free(trans);
2202 }
Harald Welte27989d42018-06-21 20:39:20 +02002203 return 0;
2204 }
2205
2206 assert(trans->vsub);
2207
2208 rc = datastatelist[i].rout(trans, msg);
2209
Harald Welte27989d42018-06-21 20:39:20 +02002210 return rc;
2211}