blob: ed29e8494c6adf4729e532074c721154f207be02 [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:
Alexander Couzensefa7b972019-04-27 23:45:37 +0200166 osmo_stat_item_inc(trans->net->statg->items[MSC_STAT_ACTIVE_CALLS], 1);
Harald Welte27989d42018-06-21 20:39:20 +0200167 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
168 break;
169 }
170
171 /* state outgoing */
172 switch (old_state) {
173 case GSM_CSTATE_ACTIVE:
Alexander Couzensefa7b972019-04-27 23:45:37 +0200174 osmo_stat_item_dec(trans->net->statg->items[MSC_STAT_ACTIVE_CALLS], 1);
Harald Welte27989d42018-06-21 20:39:20 +0200175 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
176 new_state == GSM_CSTATE_DISCONNECT_IND)
177 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
178 else
179 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
180 break;
181 }
182}
183
Harald Welte27989d42018-06-21 20:39:20 +0200184static void new_cc_state(struct gsm_trans *trans, int state)
185{
186 if (state > 31 || state < 0)
187 return;
188
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100189 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
190 gsm48_cc_state_name(trans->cc.state),
191 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200192
193 count_statistics(trans, state);
194 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200195
196 /* Stop the guard timer when a call reaches the active state */
197 if (state == GSM_CSTATE_ACTIVE)
198 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200199}
200
201static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
202{
203 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
205 uint8_t *cause, *call_state;
206
207 gh->msg_type = GSM48_MT_CC_STATUS;
208
209 cause = msgb_put(msg, 3);
210 cause[0] = 2;
211 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
212 cause[2] = 0x80 | 30; /* response to status inquiry */
213
214 call_state = msgb_put(msg, 1);
215 call_state[0] = 0xc0 | 0x00;
216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100217 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200218}
219
220static void gsm48_stop_cc_timer(struct gsm_trans *trans)
221{
222 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100223 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200224 osmo_timer_del(&trans->cc.timer);
225 trans->cc.Tcurrent = 0;
226 }
227}
228
229static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
230 int msg_type, struct gsm_mncc *mncc)
231{
232 struct msgb *msg;
233 unsigned char *data;
234
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100235 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200236
237 mncc->msg_type = msg_type;
238
239 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
240 if (!msg)
241 return -ENOMEM;
242
243 data = msgb_put(msg, sizeof(struct gsm_mncc));
244 memcpy(data, mncc, sizeof(struct gsm_mncc));
245
246 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200247 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
248 if (trans)
249 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200250
251 return 0;
252}
253
254int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
255 uint32_t callref, int location, int value)
256{
257 struct gsm_mncc rel;
258
259 memset(&rel, 0, sizeof(rel));
260 rel.callref = callref;
261 mncc_set_cause(&rel, location, value);
262 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
263 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
264 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
265}
266
267/* Call Control Specific transaction release.
268 * gets called by trans_free, DO NOT CALL YOURSELF! */
269void _gsm48_cc_trans_free(struct gsm_trans *trans)
270{
271 gsm48_stop_cc_timer(trans);
272
Harald Welte27989d42018-06-21 20:39:20 +0200273 /* send release to L4, if callref still exists */
274 if (trans->callref) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100275 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
276 * CC Release to the MS if it gets freed here. Hack it to do so. */
277 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
278 struct gsm_mncc rel = {};
279 rel.callref = trans->callref;
280 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
281 gsm48_cc_tx_release(trans, &rel);
282 }
Martin Hauke3f07dac2019-11-14 17:49:08 +0100283 /* Resource unavailable */
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200284 if (trans->cc.mncc_initiated)
285 mncc_release_ind(trans->net, trans, trans->callref,
286 GSM48_CAUSE_LOC_PRN_S_LU,
287 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte27989d42018-06-21 20:39:20 +0200288 /* This is a final freeing of the transaction. The MNCC release may have triggered the
289 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
290 gsm48_stop_cc_timer(trans);
291 }
292 if (trans->cc.state != GSM_CSTATE_NULL)
293 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200294
295 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100296
297 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
298 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200299}
300
Harald Welte27989d42018-06-21 20:39:20 +0200301/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100302static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200303{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100304 if (trans->msc_a) {
305 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
306 "Handle paging error: transaction already associated with subscriber,"
307 " apparently it was already handled. Skip.\n");
308 return;
Harald Welte27989d42018-06-21 20:39:20 +0200309 }
310
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100311 if (msc_a) {
312 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
313 /* Assign conn */
314 msc_a_get(msc_a, MSC_A_USE_CC);
315 trans->msc_a = msc_a;
316 trans->paging_request = NULL;
317 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
318 /* send SETUP request to called party */
319 gsm48_cc_tx_setup(trans, &trans->cc.msg);
320 } else {
321 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
322 /* Temporarily out of order */
323 mncc_release_ind(trans->net, trans,
324 trans->callref,
325 GSM48_CAUSE_LOC_PRN_S_LU,
326 GSM48_CC_CAUSE_DEST_OOO);
327 trans->callref = 0;
328 trans->paging_request = NULL;
329 trans_free(trans);
330 }
Harald Welte27989d42018-06-21 20:39:20 +0200331}
332
333/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100334static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200335{
336 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
337 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100338 struct call_leg *cl1;
339 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200340
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100341 if (!trans1 || !trans2) {
342 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200343 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100344 }
Harald Welte27989d42018-06-21 20:39:20 +0200345
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100346 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100347 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
348 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 +0200349 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100350 }
351
352 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
353 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200354
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100355 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
356 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
357 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
358 * If we can't, then we must give up. */
359 cl1 = trans1->msc_a->cc.call_leg;
360 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200361
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100362 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200363}
364
365static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
366{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100367 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200368 return gsm48_cc_tx_status(trans, msg);
369}
370
Harald Welte27989d42018-06-21 20:39:20 +0200371static void gsm48_cc_timeout(void *arg)
372{
373 struct gsm_trans *trans = arg;
374 int disconnect = 0, release = 0;
375 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
376 int mo_location = GSM48_CAUSE_LOC_USER;
377 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
378 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
379 struct gsm_mncc mo_rel, l4_rel;
380
381 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
382 mo_rel.callref = trans->callref;
383 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
384 l4_rel.callref = trans->callref;
385
386 switch(trans->cc.Tcurrent) {
387 case 0x303:
388 release = 1;
389 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
390 break;
391 case 0x310:
392 disconnect = 1;
393 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
394 break;
395 case 0x313:
396 disconnect = 1;
397 /* unknown, did not find it in the specs */
398 break;
399 case 0x301:
400 disconnect = 1;
401 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
402 break;
403 case 0x308:
404 if (!trans->cc.T308_second) {
405 /* restart T308 a second time */
406 gsm48_cc_tx_release(trans, &trans->cc.msg);
407 trans->cc.T308_second = 1;
408 break; /* stay in release state */
409 }
410 trans_free(trans);
411 return;
412 case 0x306:
413 release = 1;
414 mo_cause = trans->cc.msg.cause.value;
415 mo_location = trans->cc.msg.cause.location;
416 break;
417 case 0x323:
418 disconnect = 1;
419 break;
420 default:
421 release = 1;
422 }
423
424 if (release && trans->callref) {
425 /* process release towards layer 4 */
426 mncc_release_ind(trans->net, trans, trans->callref,
427 l4_location, l4_cause);
428 trans->callref = 0;
429 }
430
431 if (disconnect && trans->callref) {
432 /* process disconnect towards layer 4 */
433 mncc_set_cause(&l4_rel, l4_location, l4_cause);
434 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
435 }
436
437 /* process disconnect towards mobile station */
438 if (disconnect || release) {
439 mncc_set_cause(&mo_rel, mo_location, mo_cause);
440 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
441 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
442 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
443 mo_rel.cause.diag_len = 3;
444
445 if (disconnect)
446 gsm48_cc_tx_disconnect(trans, &mo_rel);
447 if (release)
448 gsm48_cc_tx_release(trans, &mo_rel);
449 }
450
451}
452
453/* disconnect both calls from the bridge */
454static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100455 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200456{
457 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
458 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
459 struct gsm_mncc mx_rel;
460 if (!trans0 || !trans1)
461 return;
462
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100463 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
464 trans0->callref, trans1->callref, strerror(err));
465 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200466 trans0->callref, trans1->callref, strerror(err));
467
468 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
469 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
470 GSM48_CC_CAUSE_CHAN_UNACCEPT);
471
472 mx_rel.callref = trans0->callref;
473 gsm48_cc_tx_disconnect(trans0, &mx_rel);
474
475 mx_rel.callref = trans1->callref;
476 gsm48_cc_tx_disconnect(trans1, &mx_rel);
477}
478
479static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
480 int sec, int micro)
481{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100482 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200483 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
484 osmo_timer_schedule(&trans->cc.timer, sec, micro);
485 trans->cc.Tcurrent = current;
486}
487
488static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
489{
490 struct gsm48_hdr *gh = msgb_l3(msg);
491 uint8_t msg_type = gsm48_hdr_msg_type(gh);
492 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
493 struct tlv_parsed tp;
494 struct gsm_mncc setup;
495
Philipp Maier9ca7b312018-10-10 17:00:49 +0200496 gsm48_start_guard_timer(trans);
497
Harald Welte27989d42018-06-21 20:39:20 +0200498 memset(&setup, 0, sizeof(struct gsm_mncc));
499 setup.callref = trans->callref;
500
501 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
502 /* emergency setup is identified by msg_type */
503 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
504 setup.fields |= MNCC_F_EMERGENCY;
505 setup.emergency = 1;
506 /* use destination number as configured by user (if any) */
507 if (trans->net->emergency.route_to_msisdn) {
508 setup.fields |= MNCC_F_CALLED;
509 setup.called.type = 0; /* unknown */
510 setup.called.plan = 0; /* unknown */
511 OSMO_STRLCPY_ARRAY(setup.called.number,
512 trans->net->emergency.route_to_msisdn);
513 }
514 }
515
516 /* use subscriber as calling party number */
517 setup.fields |= MNCC_F_CALLING;
518 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
519 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
520
521 /* bearer capability */
522 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
523 setup.fields |= MNCC_F_BEARER_CAP;
524 gsm48_decode_bearer_cap(&setup.bearer_cap,
525 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
526
527 /* Create a copy of the bearer capability
528 * in the transaction struct, so we can use
529 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100530 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200531 sizeof(trans->bearer_cap));
532 }
533 /* facility */
534 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
535 setup.fields |= MNCC_F_FACILITY;
536 gsm48_decode_facility(&setup.facility,
537 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
538 }
539 /* called party bcd number */
540 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
541 setup.fields |= MNCC_F_CALLED;
542 gsm48_decode_called(&setup.called,
543 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
544 }
545 /* user-user */
546 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
547 setup.fields |= MNCC_F_USERUSER;
548 gsm48_decode_useruser(&setup.useruser,
549 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
550 }
551 /* ss-version */
552 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
553 setup.fields |= MNCC_F_SSVERSION;
554 gsm48_decode_ssversion(&setup.ssversion,
555 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
556 }
557 /* CLIR suppression */
558 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
559 setup.clir.sup = 1;
560 /* CLIR invocation */
561 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
562 setup.clir.inv = 1;
563 /* cc cap */
564 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
565 setup.fields |= MNCC_F_CCCAP;
566 gsm48_decode_cccap(&setup.cccap,
567 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
568 }
569
570 new_cc_state(trans, GSM_CSTATE_INITIATED);
571
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100572 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
573 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200574
575 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
576
577 /* indicate setup to MNCC */
578 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
579
580 /* MNCC code will modify the channel asynchronously, we should
581 * ipaccess-bind only after the modification has been made to the
582 * lchan->tch_mode */
583 return 0;
584}
585
586static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
587{
588 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
589 struct gsm48_hdr *gh;
590 struct gsm_mncc *setup = arg;
591 int rc, trans_id;
592
593 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
594
595 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700596 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100597 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200598 "This is not allowed!\n");
599 /* Temporarily out of order */
600 rc = mncc_release_ind(trans->net, trans, trans->callref,
601 GSM48_CAUSE_LOC_PRN_S_LU,
602 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
603 trans->callref = 0;
604 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200605 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200606 return rc;
607 }
608
609 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100610 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200611 if (trans_id < 0) {
612 /* no free transaction ID */
613 rc = mncc_release_ind(trans->net, trans, trans->callref,
614 GSM48_CAUSE_LOC_PRN_S_LU,
615 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
616 trans->callref = 0;
617 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200618 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200619 return rc;
620 }
621 trans->transaction_id = trans_id;
622
623 gh->msg_type = GSM48_MT_CC_SETUP;
624
625 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
626
627 /* bearer capability */
628 if (setup->fields & MNCC_F_BEARER_CAP) {
629 /* Create a copy of the bearer capability in the transaction struct, so we
630 * can use this information later */
631 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
632 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
633 }
634 /* facility */
635 if (setup->fields & MNCC_F_FACILITY)
636 gsm48_encode_facility(msg, 0, &setup->facility);
637 /* progress */
638 if (setup->fields & MNCC_F_PROGRESS)
639 gsm48_encode_progress(msg, 0, &setup->progress);
640 /* calling party BCD number */
641 if (setup->fields & MNCC_F_CALLING)
642 gsm48_encode_calling(msg, &setup->calling);
643 /* called party BCD number */
644 if (setup->fields & MNCC_F_CALLED)
645 gsm48_encode_called(msg, &setup->called);
646 /* user-user */
647 if (setup->fields & MNCC_F_USERUSER)
648 gsm48_encode_useruser(msg, 0, &setup->useruser);
649 /* redirecting party BCD number */
650 if (setup->fields & MNCC_F_REDIRECTING)
651 gsm48_encode_redirecting(msg, &setup->redirecting);
652 /* signal */
653 if (setup->fields & MNCC_F_SIGNAL)
654 gsm48_encode_signal(msg, setup->signal);
655
656 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
657
658 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
659
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100660 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200661}
662
663static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
664{
665 struct gsm48_hdr *gh = msgb_l3(msg);
666 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
667 struct tlv_parsed tp;
668 struct gsm_mncc call_conf;
669 int rc;
670
671 gsm48_stop_cc_timer(trans);
672 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
673
674 memset(&call_conf, 0, sizeof(struct gsm_mncc));
675 call_conf.callref = trans->callref;
676
677 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
678#if 0
679 /* repeat */
680 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
681 call_conf.repeat = 1;
682 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
683 call_conf.repeat = 2;
684#endif
685 /* bearer capability */
686 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
687 call_conf.fields |= MNCC_F_BEARER_CAP;
688 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
689 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
690
691 /* Create a copy of the bearer capability
692 * in the transaction struct, so we can use
693 * this information later */
694 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
695 sizeof(trans->bearer_cap));
696 }
697 /* cause */
698 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
699 call_conf.fields |= MNCC_F_CAUSE;
700 gsm48_decode_cause(&call_conf.cause,
701 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
702 }
703 /* cc cap */
704 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
705 call_conf.fields |= MNCC_F_CCCAP;
706 gsm48_decode_cccap(&call_conf.cccap,
707 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
708 }
709
710 /* IMSI of called subscriber */
711 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
712
713 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
714
715 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100716 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200717
718 /* don't continue, if there were problems with
719 * the call assignment. */
720 if (rc)
721 return rc;
722
723 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
724 &call_conf);
725}
726
727static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
728{
729 struct gsm_mncc *proceeding = arg;
730 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
732 int rc;
733
734 gh->msg_type = GSM48_MT_CC_CALL_PROC;
735
736 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
737
738 /* bearer capability */
739 if (proceeding->fields & MNCC_F_BEARER_CAP) {
740 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
741 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
742 }
743 /* facility */
744 if (proceeding->fields & MNCC_F_FACILITY)
745 gsm48_encode_facility(msg, 0, &proceeding->facility);
746 /* progress */
747 if (proceeding->fields & MNCC_F_PROGRESS)
748 gsm48_encode_progress(msg, 0, &proceeding->progress);
749
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100750 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200751 if (rc)
752 return rc;
753
754 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100755 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200756}
757
758static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
759{
760 struct gsm48_hdr *gh = msgb_l3(msg);
761 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
762 struct tlv_parsed tp;
763 struct gsm_mncc alerting;
764
765 gsm48_stop_cc_timer(trans);
766 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
767
768 memset(&alerting, 0, sizeof(struct gsm_mncc));
769 alerting.callref = trans->callref;
770 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
771 /* facility */
772 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
773 alerting.fields |= MNCC_F_FACILITY;
774 gsm48_decode_facility(&alerting.facility,
775 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
776 }
777
778 /* progress */
779 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
780 alerting.fields |= MNCC_F_PROGRESS;
781 gsm48_decode_progress(&alerting.progress,
782 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
783 }
784 /* ss-version */
785 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
786 alerting.fields |= MNCC_F_SSVERSION;
787 gsm48_decode_ssversion(&alerting.ssversion,
788 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
789 }
790
791 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
792
793 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
794 &alerting);
795}
796
797static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
798{
799 struct gsm_mncc *alerting = arg;
800 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
801 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
802
803 gh->msg_type = GSM48_MT_CC_ALERTING;
804
805 /* facility */
806 if (alerting->fields & MNCC_F_FACILITY)
807 gsm48_encode_facility(msg, 0, &alerting->facility);
808 /* progress */
809 if (alerting->fields & MNCC_F_PROGRESS)
810 gsm48_encode_progress(msg, 0, &alerting->progress);
811 /* user-user */
812 if (alerting->fields & MNCC_F_USERUSER)
813 gsm48_encode_useruser(msg, 0, &alerting->useruser);
814
815 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
816
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100817 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200818}
819
820static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
821{
822 struct gsm_mncc *progress = arg;
823 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
824 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
825
826 gh->msg_type = GSM48_MT_CC_PROGRESS;
827
828 /* progress */
829 gsm48_encode_progress(msg, 1, &progress->progress);
830 /* user-user */
831 if (progress->fields & MNCC_F_USERUSER)
832 gsm48_encode_useruser(msg, 0, &progress->useruser);
833
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100834 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200835}
836
837static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
838{
839 struct gsm_mncc *connect = arg;
840 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
841 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
842
843 gh->msg_type = GSM48_MT_CC_CONNECT;
844
845 gsm48_stop_cc_timer(trans);
846 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
847
848 /* facility */
849 if (connect->fields & MNCC_F_FACILITY)
850 gsm48_encode_facility(msg, 0, &connect->facility);
851 /* progress */
852 if (connect->fields & MNCC_F_PROGRESS)
853 gsm48_encode_progress(msg, 0, &connect->progress);
854 /* connected number */
855 if (connect->fields & MNCC_F_CONNECTED)
856 gsm48_encode_connected(msg, &connect->connected);
857 /* user-user */
858 if (connect->fields & MNCC_F_USERUSER)
859 gsm48_encode_useruser(msg, 0, &connect->useruser);
860
861 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
862
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100863 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200864}
865
866static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
867{
868 struct gsm48_hdr *gh = msgb_l3(msg);
869 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
870 struct tlv_parsed tp;
871 struct gsm_mncc connect;
872
873 gsm48_stop_cc_timer(trans);
874
875 memset(&connect, 0, sizeof(struct gsm_mncc));
876 connect.callref = trans->callref;
877 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
878 /* use subscriber as connected party number */
879 connect.fields |= MNCC_F_CONNECTED;
880 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
881 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
882
883 /* facility */
884 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
885 connect.fields |= MNCC_F_FACILITY;
886 gsm48_decode_facility(&connect.facility,
887 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
888 }
889 /* user-user */
890 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
891 connect.fields |= MNCC_F_USERUSER;
892 gsm48_decode_useruser(&connect.useruser,
893 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
894 }
895 /* ss-version */
896 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
897 connect.fields |= MNCC_F_SSVERSION;
898 gsm48_decode_ssversion(&connect.ssversion,
899 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
900 }
901
902 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
903 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
904
905 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
906}
907
908
909static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
910{
911 struct gsm_mncc connect_ack;
912
913 gsm48_stop_cc_timer(trans);
914
915 new_cc_state(trans, GSM_CSTATE_ACTIVE);
916 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
917
918 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
919 connect_ack.callref = trans->callref;
920
921 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
922 &connect_ack);
923}
924
925static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
926{
927 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
928 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
929
930 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
931
932 new_cc_state(trans, GSM_CSTATE_ACTIVE);
933
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100934 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200935}
936
937static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
938{
939 struct gsm48_hdr *gh = msgb_l3(msg);
940 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
941 struct tlv_parsed tp;
942 struct gsm_mncc disc;
943
944 gsm48_stop_cc_timer(trans);
945
946 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
947
948 memset(&disc, 0, sizeof(struct gsm_mncc));
949 disc.callref = trans->callref;
950 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
951 /* cause */
952 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
953 disc.fields |= MNCC_F_CAUSE;
954 gsm48_decode_cause(&disc.cause,
955 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
956 }
957 /* facility */
958 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
959 disc.fields |= MNCC_F_FACILITY;
960 gsm48_decode_facility(&disc.facility,
961 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
962 }
963 /* user-user */
964 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
965 disc.fields |= MNCC_F_USERUSER;
966 gsm48_decode_useruser(&disc.useruser,
967 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
968 }
969 /* ss-version */
970 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
971 disc.fields |= MNCC_F_SSVERSION;
972 gsm48_decode_ssversion(&disc.ssversion,
973 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
974 }
975
976 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +0200977}
978
979static struct gsm_mncc_cause default_cause = {
980 .location = GSM48_CAUSE_LOC_PRN_S_LU,
981 .coding = 0,
982 .rec = 0,
983 .rec_val = 0,
984 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
985 .diag_len = 0,
986 .diag = { 0 },
987};
988
989static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
990{
991 struct gsm_mncc *disc = arg;
992 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
993 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
994
995 gh->msg_type = GSM48_MT_CC_DISCONNECT;
996
997 gsm48_stop_cc_timer(trans);
998 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
999
1000 /* cause */
1001 if (disc->fields & MNCC_F_CAUSE)
1002 gsm48_encode_cause(msg, 1, &disc->cause);
1003 else
1004 gsm48_encode_cause(msg, 1, &default_cause);
1005
1006 /* facility */
1007 if (disc->fields & MNCC_F_FACILITY)
1008 gsm48_encode_facility(msg, 0, &disc->facility);
1009 /* progress */
1010 if (disc->fields & MNCC_F_PROGRESS)
1011 gsm48_encode_progress(msg, 0, &disc->progress);
1012 /* user-user */
1013 if (disc->fields & MNCC_F_USERUSER)
1014 gsm48_encode_useruser(msg, 0, &disc->useruser);
1015
1016 /* store disconnect cause for T306 expiry */
1017 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1018
1019 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1020
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001021 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001022}
1023
1024static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1025{
1026 struct gsm48_hdr *gh = msgb_l3(msg);
1027 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1028 struct tlv_parsed tp;
1029 struct gsm_mncc rel;
1030 int rc;
1031
1032 gsm48_stop_cc_timer(trans);
1033
1034 memset(&rel, 0, sizeof(struct gsm_mncc));
1035 rel.callref = trans->callref;
1036 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1037 /* cause */
1038 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1039 rel.fields |= MNCC_F_CAUSE;
1040 gsm48_decode_cause(&rel.cause,
1041 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1042 }
1043 /* facility */
1044 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1045 rel.fields |= MNCC_F_FACILITY;
1046 gsm48_decode_facility(&rel.facility,
1047 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1048 }
1049 /* user-user */
1050 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1051 rel.fields |= MNCC_F_USERUSER;
1052 gsm48_decode_useruser(&rel.useruser,
1053 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1054 }
1055 /* ss-version */
1056 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1057 rel.fields |= MNCC_F_SSVERSION;
1058 gsm48_decode_ssversion(&rel.ssversion,
1059 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1060 }
1061
1062 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1063 /* release collision 5.4.5 */
1064 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1065 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001066 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001067 GSM48_PDISC_CC | (trans->transaction_id << 4),
1068 GSM48_MT_CC_RELEASE_COMPL);
1069 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1070 }
1071
1072 new_cc_state(trans, GSM_CSTATE_NULL);
1073
1074 trans->callref = 0;
1075 trans_free(trans);
1076
1077 return rc;
1078}
1079
1080static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1081{
1082 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001083 struct msgb *msg;
1084 struct gsm48_hdr *gh;
1085
1086 if (!trans->msc_a) {
1087 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1088 return -EINVAL;
1089 }
1090
1091 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1092 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001093
1094 gh->msg_type = GSM48_MT_CC_RELEASE;
1095
1096 gsm48_stop_cc_timer(trans);
1097 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1098
1099 /* cause */
1100 if (rel->fields & MNCC_F_CAUSE)
1101 gsm48_encode_cause(msg, 0, &rel->cause);
1102 /* facility */
1103 if (rel->fields & MNCC_F_FACILITY)
1104 gsm48_encode_facility(msg, 0, &rel->facility);
1105 /* user-user */
1106 if (rel->fields & MNCC_F_USERUSER)
1107 gsm48_encode_useruser(msg, 0, &rel->useruser);
1108
1109 trans->cc.T308_second = 0;
1110 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1111
1112 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1113 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1114
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001115 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001116}
1117
1118static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1119{
1120 struct gsm48_hdr *gh = msgb_l3(msg);
1121 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1122 struct tlv_parsed tp;
1123 struct gsm_mncc rel;
1124 int rc = 0;
1125
1126 gsm48_stop_cc_timer(trans);
1127
1128 memset(&rel, 0, sizeof(struct gsm_mncc));
1129 rel.callref = trans->callref;
1130 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1131 /* cause */
1132 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1133 rel.fields |= MNCC_F_CAUSE;
1134 gsm48_decode_cause(&rel.cause,
1135 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1136 }
1137 /* facility */
1138 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1139 rel.fields |= MNCC_F_FACILITY;
1140 gsm48_decode_facility(&rel.facility,
1141 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1142 }
1143 /* user-user */
1144 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1145 rel.fields |= MNCC_F_USERUSER;
1146 gsm48_decode_useruser(&rel.useruser,
1147 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1148 }
1149 /* ss-version */
1150 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1151 rel.fields |= MNCC_F_SSVERSION;
1152 gsm48_decode_ssversion(&rel.ssversion,
1153 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1154 }
1155
1156 if (trans->callref) {
1157 switch (trans->cc.state) {
1158 case GSM_CSTATE_CALL_PRESENT:
1159 rc = mncc_recvmsg(trans->net, trans,
1160 MNCC_REJ_IND, &rel);
1161 break;
1162 case GSM_CSTATE_RELEASE_REQ:
1163 rc = mncc_recvmsg(trans->net, trans,
1164 MNCC_REL_CNF, &rel);
1165 break;
1166 default:
1167 rc = mncc_recvmsg(trans->net, trans,
1168 MNCC_REL_IND, &rel);
1169 }
1170 }
1171
1172 trans->callref = 0;
1173 trans_free(trans);
1174
1175 return rc;
1176}
1177
1178static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1179{
1180 struct gsm_mncc *rel = arg;
1181 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1182 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1183 int ret;
1184
1185 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1186
1187 trans->callref = 0;
1188
1189 gsm48_stop_cc_timer(trans);
1190
1191 /* cause */
1192 if (rel->fields & MNCC_F_CAUSE)
1193 gsm48_encode_cause(msg, 0, &rel->cause);
1194 /* facility */
1195 if (rel->fields & MNCC_F_FACILITY)
1196 gsm48_encode_facility(msg, 0, &rel->facility);
1197 /* user-user */
1198 if (rel->fields & MNCC_F_USERUSER)
1199 gsm48_encode_useruser(msg, 0, &rel->useruser);
1200
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001201 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001202
1203 trans_free(trans);
1204
1205 return ret;
1206}
1207
1208static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1209{
1210 struct gsm48_hdr *gh = msgb_l3(msg);
1211 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1212 struct tlv_parsed tp;
1213 struct gsm_mncc fac;
1214
1215 memset(&fac, 0, sizeof(struct gsm_mncc));
1216 fac.callref = trans->callref;
1217 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1218 /* facility */
1219 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1220 fac.fields |= MNCC_F_FACILITY;
1221 gsm48_decode_facility(&fac.facility,
1222 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1223 }
1224 /* ss-version */
1225 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1226 fac.fields |= MNCC_F_SSVERSION;
1227 gsm48_decode_ssversion(&fac.ssversion,
1228 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1229 }
1230
1231 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1232}
1233
1234static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1235{
1236 struct gsm_mncc *fac = arg;
1237 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1238 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1239
1240 gh->msg_type = GSM48_MT_CC_FACILITY;
1241
1242 /* facility */
1243 gsm48_encode_facility(msg, 1, &fac->facility);
1244
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001245 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001246}
1247
1248static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1249{
1250 struct gsm_mncc hold;
1251
1252 memset(&hold, 0, sizeof(struct gsm_mncc));
1253 hold.callref = trans->callref;
1254 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1255}
1256
1257static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1258{
1259 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1260 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1261
1262 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1263
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001264 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001265}
1266
1267static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1268{
1269 struct gsm_mncc *hold_rej = arg;
1270 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1271 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1272
1273 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1274
1275 /* cause */
1276 if (hold_rej->fields & MNCC_F_CAUSE)
1277 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1278 else
1279 gsm48_encode_cause(msg, 1, &default_cause);
1280
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001281 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001282}
1283
1284static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1285{
1286 struct gsm_mncc retrieve;
1287
1288 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1289 retrieve.callref = trans->callref;
1290 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1291 &retrieve);
1292}
1293
1294static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1295{
1296 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1298
1299 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1300
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001301 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001302}
1303
1304static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1305{
1306 struct gsm_mncc *retrieve_rej = arg;
1307 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1308 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1309
1310 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1311
1312 /* cause */
1313 if (retrieve_rej->fields & MNCC_F_CAUSE)
1314 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1315 else
1316 gsm48_encode_cause(msg, 1, &default_cause);
1317
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001318 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001319}
1320
1321static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1322{
1323 struct gsm48_hdr *gh = msgb_l3(msg);
1324 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1325 struct tlv_parsed tp;
1326 struct gsm_mncc dtmf;
1327
1328 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1329 dtmf.callref = trans->callref;
1330 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1331 /* keypad facility */
1332 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1333 dtmf.fields |= MNCC_F_KEYPAD;
1334 gsm48_decode_keypad(&dtmf.keypad,
1335 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1336 }
1337
1338 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1339}
1340
1341static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1342{
1343 struct gsm_mncc *dtmf = arg;
1344 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1345 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1346
1347 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1348
1349 /* keypad */
1350 if (dtmf->fields & MNCC_F_KEYPAD)
1351 gsm48_encode_keypad(msg, dtmf->keypad);
1352
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001353 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001354}
1355
1356static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1357{
1358 struct gsm_mncc *dtmf = arg;
1359 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1360 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1361
1362 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1363
1364 /* cause */
1365 if (dtmf->fields & MNCC_F_CAUSE)
1366 gsm48_encode_cause(msg, 1, &dtmf->cause);
1367 else
1368 gsm48_encode_cause(msg, 1, &default_cause);
1369
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001370 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001371}
1372
1373static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1374{
1375 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1376 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1377
1378 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1379
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001380 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001381}
1382
1383static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1384{
1385 struct gsm_mncc dtmf;
1386
1387 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1388 dtmf.callref = trans->callref;
1389
1390 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1391}
1392
1393static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1394{
1395 struct gsm48_hdr *gh = msgb_l3(msg);
1396 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1397 struct tlv_parsed tp;
1398 struct gsm_mncc modify;
1399
1400 memset(&modify, 0, sizeof(struct gsm_mncc));
1401 modify.callref = trans->callref;
1402 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1403 /* bearer capability */
1404 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1405 modify.fields |= MNCC_F_BEARER_CAP;
1406 gsm48_decode_bearer_cap(&modify.bearer_cap,
1407 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1408
1409 /* Create a copy of the bearer capability
1410 * in the transaction struct, so we can use
1411 * this information later */
1412 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1413 sizeof(trans->bearer_cap));
1414 }
1415
1416 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1417
1418 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1419}
1420
1421static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1422{
1423 struct gsm_mncc *modify = arg;
1424 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1425 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1426
1427 gh->msg_type = GSM48_MT_CC_MODIFY;
1428
1429 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1430
1431 /* bearer capability */
1432 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1433 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1434
1435 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1436
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001437 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001438}
1439
1440static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1441{
1442 struct gsm48_hdr *gh = msgb_l3(msg);
1443 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1444 struct tlv_parsed tp;
1445 struct gsm_mncc modify;
1446
1447 gsm48_stop_cc_timer(trans);
1448
1449 memset(&modify, 0, sizeof(struct gsm_mncc));
1450 modify.callref = trans->callref;
1451 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1452 /* bearer capability */
1453 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1454 modify.fields |= MNCC_F_BEARER_CAP;
1455 gsm48_decode_bearer_cap(&modify.bearer_cap,
1456 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1457
1458 /* Create a copy of the bearer capability
1459 * in the transaction struct, so we can use
1460 * this information later */
1461 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1462 sizeof(trans->bearer_cap));
1463 }
1464
1465 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1466
1467 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1468}
1469
1470static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1471{
1472 struct gsm_mncc *modify = arg;
1473 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1474 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1475
1476 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1477
1478 /* bearer capability */
1479 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1480 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1481
1482 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1483
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001484 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001485}
1486
1487static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1488{
1489 struct gsm48_hdr *gh = msgb_l3(msg);
1490 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1491 struct tlv_parsed tp;
1492 struct gsm_mncc modify;
1493
1494 gsm48_stop_cc_timer(trans);
1495
1496 memset(&modify, 0, sizeof(struct gsm_mncc));
1497 modify.callref = trans->callref;
1498 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1499 /* bearer capability */
1500 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1501 modify.fields |= GSM48_IE_BEARER_CAP;
1502 gsm48_decode_bearer_cap(&modify.bearer_cap,
1503 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1504
1505 /* Create a copy of the bearer capability
1506 * in the transaction struct, so we can use
1507 * this information later */
1508 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1509 sizeof(trans->bearer_cap));
1510 }
1511 /* cause */
1512 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1513 modify.fields |= MNCC_F_CAUSE;
1514 gsm48_decode_cause(&modify.cause,
1515 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1516 }
1517
1518 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1519
1520 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1521}
1522
1523static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1524{
1525 struct gsm_mncc *modify = arg;
1526 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1527 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1528
1529 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1530
1531 /* bearer capability */
1532 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1533 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1534 /* cause */
1535 gsm48_encode_cause(msg, 1, &modify->cause);
1536
1537 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1538
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001539 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001540}
1541
1542static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1543{
1544 struct gsm_mncc *notify = arg;
1545 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1546 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1547
1548 gh->msg_type = GSM48_MT_CC_NOTIFY;
1549
1550 /* notify */
1551 gsm48_encode_notify(msg, notify->notify);
1552
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001553 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001554}
1555
1556static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1557{
1558 struct gsm48_hdr *gh = msgb_l3(msg);
1559 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1560// struct tlv_parsed tp;
1561 struct gsm_mncc notify;
1562
1563 memset(&notify, 0, sizeof(struct gsm_mncc));
1564 notify.callref = trans->callref;
1565// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1566 if (payload_len >= 1)
1567 gsm48_decode_notify(&notify.notify, gh->data);
1568
1569 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1570}
1571
1572static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1573{
1574 struct gsm_mncc *user = arg;
1575 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1576 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1577
1578 gh->msg_type = GSM48_MT_CC_USER_INFO;
1579
1580 /* user-user */
1581 if (user->fields & MNCC_F_USERUSER)
1582 gsm48_encode_useruser(msg, 1, &user->useruser);
1583 /* more data */
1584 if (user->more)
1585 gsm48_encode_more(msg);
1586
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001587 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001588}
1589
1590static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1591{
1592 struct gsm48_hdr *gh = msgb_l3(msg);
1593 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1594 struct tlv_parsed tp;
1595 struct gsm_mncc user;
1596
1597 memset(&user, 0, sizeof(struct gsm_mncc));
1598 user.callref = trans->callref;
1599 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1600 /* user-user */
1601 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1602 user.fields |= MNCC_F_USERUSER;
1603 gsm48_decode_useruser(&user.useruser,
1604 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1605 }
1606 /* more data */
1607 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1608 user.more = 1;
1609
1610 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1611}
1612
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001613static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1614 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1615 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001616{
1617 uint8_t data[sizeof(struct gsm_mncc)];
1618 struct gsm_mncc_rtp *rtp;
1619
1620 memset(&data, 0, sizeof(data));
1621 rtp = (struct gsm_mncc_rtp *) &data[0];
1622
1623 rtp->callref = callref;
1624 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001625 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001626 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1627 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001628 }
Harald Welte27989d42018-06-21 20:39:20 +02001629 rtp->payload_type = payload_type;
1630 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001631 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001632}
1633
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001634static 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 +02001635{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001636 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001637}
1638
1639static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1640{
1641 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001642
1643 /* Find callref */
1644 trans = trans_find_by_callref(net, callref);
1645 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001646 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001647 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001648 return -EIO;
1649 }
1650 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001651 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001652 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001653 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001654 return 0;
1655 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001656 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001657
Harald Welte27989d42018-06-21 20:39:20 +02001658 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001659 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001660}
1661
1662/* Trigger TCH_RTP_CREATE acknowledgement */
1663int gsm48_tch_rtp_create(struct gsm_trans *trans)
1664{
1665 /* This function is called as soon as the port, on which the
1666 * mgcp-gw expects the incoming RTP stream from the remote
1667 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001668 struct msc_a *msc_a = trans->msc_a;
1669 struct gsm_network *net = msc_a_net(msc_a);
1670 struct call_leg *cl = msc_a->cc.call_leg;
1671 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001672 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1673 uint32_t payload_type;
1674 int payload_msg_type;
1675 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001676
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001677 if (!rtp_cn) {
1678 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1679 return -EINVAL;
1680 }
1681
1682 if (!rtp_cn->codec_known) {
1683 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1684 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1685 return -EINVAL;
1686 }
1687
1688 /* Codec */
1689 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1690
1691 /* Payload Type number */
1692 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001693 if (mgcp_info && mgcp_info->ptmap_len)
1694 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1695 else
1696 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001697
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001698 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1699 if (!rtp_cn_local) {
1700 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1701 return -EINVAL;
1702 }
1703
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001704 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 +02001705}
1706
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001707static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001708{
1709 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001710 struct call_leg *cl;
1711 struct rtp_stream *rtps;
1712 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001713 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001714
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001715 /* FIXME: in *rtp we should get the codec information of the remote
1716 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1717 * meaningful value based on this information but unfortunately we
1718 * can't do that yet because the mncc API can not signal dynamic
1719 * payload types yet. This must be fixed first. Also there may be
1720 * additional members necessary in trans->conn->rtp because we
1721 * somehow need to deal with dynamic payload types that do not
1722 * comply to 3gpp's assumptions of payload type numbers on the A
1723 * interface. See also related tickets: OS#3399 and OS1683 */
1724
Harald Welte27989d42018-06-21 20:39:20 +02001725 /* Find callref */
1726 trans = trans_find_by_callref(net, rtp->callref);
1727 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001728 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001729 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001730 return -EIO;
1731 }
1732 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001733 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001734 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001735 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001736 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001737 }
1738
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001739 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(MNCC_RTP_CONNECT),
1740 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1741 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001742
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001743 cl = trans->msc_a->cc.call_leg;
1744 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1745
1746 if (!rtps) {
1747 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1748 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1749 return -EINVAL;
1750 }
1751
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001752 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1753 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1754 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1755 return -EINVAL;
1756 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001757 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1758 rtp_stream_commit(rtps);
1759 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001760}
1761
1762static struct downstate {
1763 uint32_t states;
1764 int type;
1765 int (*rout) (struct gsm_trans *trans, void *arg);
1766} downstatelist[] = {
1767 /* mobile originating call establishment */
1768 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1769 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1770 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1771 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1772 {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 */
1773 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1774 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1775 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1776 /* mobile terminating call establishment */
1777 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1778 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1779 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1780 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1781 /* signalling during call */
1782 {SBIT(GSM_CSTATE_ACTIVE),
1783 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1784 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1785 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1786 {ALL_STATES,
1787 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1788 {ALL_STATES,
1789 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1790 {ALL_STATES,
1791 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1792 {SBIT(GSM_CSTATE_ACTIVE),
1793 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1794 {SBIT(GSM_CSTATE_ACTIVE),
1795 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1796 {SBIT(GSM_CSTATE_ACTIVE),
1797 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1798 {SBIT(GSM_CSTATE_ACTIVE),
1799 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1800 {SBIT(GSM_CSTATE_ACTIVE),
1801 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1802 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1803 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1804 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1805 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1806 {SBIT(GSM_CSTATE_ACTIVE),
1807 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1808 /* clearing */
1809 {SBIT(GSM_CSTATE_INITIATED),
1810 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1811 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1812 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1813 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1814 MNCC_REL_REQ, gsm48_cc_tx_release},
1815};
1816
1817#define DOWNSLLEN \
1818 (sizeof(downstatelist) / sizeof(struct downstate))
1819
1820
Philipp Maiercd64af72019-08-01 09:46:40 +02001821static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001822{
1823 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001824 struct msc_a *msc_a = NULL;
1825 struct gsm_trans *trans = NULL;
1826 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001827
Harald Welte27989d42018-06-21 20:39:20 +02001828 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001829 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001830 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001831 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001832 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001833 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001834 return rc;
1835 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001836 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001837 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001838 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001839 case MNCC_RTP_FREE:
1840 /* unused right now */
1841 return -EIO;
1842
1843 case MNCC_FRAME_DROP:
1844 case MNCC_FRAME_RECV:
1845 case GSM_TCHF_FRAME:
1846 case GSM_TCHF_FRAME_EFR:
1847 case GSM_TCHH_FRAME:
1848 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001849 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001850 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001851 return -ENOTSUP;
1852 }
1853
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001854 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001855
1856 /* Find callref */
1857 trans = trans_find_by_callref(net, data->callref);
1858
1859 /* Callref unknown */
1860 if (!trans) {
1861 struct vlr_subscr *vsub;
1862
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001863 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001864 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001865 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001866 /* Invalid call reference */
1867 return mncc_release_ind(net, NULL, data->callref,
1868 GSM48_CAUSE_LOC_PRN_S_LU,
1869 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1870 }
1871 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001872 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001873 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001874 /* Invalid number */
1875 return mncc_release_ind(net, NULL, data->callref,
1876 GSM48_CAUSE_LOC_PRN_S_LU,
1877 GSM48_CC_CAUSE_INV_NR_FORMAT);
1878 }
1879 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001880 if (data->called.number[0]) {
1881 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1882 if (!vsub)
1883 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001884 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001885 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001886 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001887 if (!vsub)
1888 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001889 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001890 }
1891 if (!vsub)
1892 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001893 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001894 /* update the subscriber we deal with */
1895 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1896
Harald Welte27989d42018-06-21 20:39:20 +02001897 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001898 if (!vsub->lu_complete) {
1899 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001900 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001901 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001902 /* Temporarily out of order */
1903 return mncc_release_ind(net, NULL, data->callref,
1904 GSM48_CAUSE_LOC_PRN_S_LU,
1905 GSM48_CC_CAUSE_DEST_OOO);
1906 }
Keith Whyte991bb422019-08-08 15:43:40 +02001907
1908 /* Find valid conn */
1909 msc_a = msc_a_for_vsub(vsub, true);
1910
1911 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1912 if (!net->call_waiting && msc_a) {
1913 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1914 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1915 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1916 "rx '%s' for subscriber %s with trans state (%s)"
1917 " rejecting with USER_BUSY\n",
1918 get_mncc_name(msg->msg_type), data->called.number,
1919 gsm48_cc_state_name(existing_cc_trans->cc.state));
1920 return mncc_release_ind(net, NULL, data->callref,
1921 GSM48_CAUSE_LOC_PRN_S_LU,
1922 GSM48_CC_CAUSE_USER_BUSY);
1923 }
1924 }
1925
Harald Welte27989d42018-06-21 20:39:20 +02001926 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001927 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001928 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001929 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001930 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001931 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001932 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001933 mncc_release_ind(net, NULL, data->callref,
1934 GSM48_CAUSE_LOC_PRN_S_LU,
1935 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1936 return -ENOMEM;
1937 }
1938
Harald Welte27989d42018-06-21 20:39:20 +02001939 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001940 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001941 /* This condition will return before the common logging of the received MNCC message below, so
1942 * log it now. */
1943 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001944
Harald Welte27989d42018-06-21 20:39:20 +02001945 /* store setup information until paging succeeds */
1946 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1947
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001948 /* Request a channel. If Paging already started, paging_request_start() will append the new
1949 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001950 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1951 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001952 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001953 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001954 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001955 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001956 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001957 return 0;
1958 }
1959
1960 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001961 trans->msc_a = msc_a;
1962 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001963 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001964 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001965 } else {
1966 /* update the subscriber we deal with */
1967 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1968 }
1969
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001970 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001971
Philipp Maier9ca7b312018-10-10 17:00:49 +02001972 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02001973 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02001974
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001975 if (trans->msc_a)
1976 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02001977
1978 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001979 if (!msc_a) {
1980 struct gsm_mncc rel = {
1981 .callref = data->callref,
1982 };
1983 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001984 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
1985 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001986 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02001987 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
1988 else
1989 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1990 trans->callref = 0;
1991 trans_free(trans);
1992 return rc;
1993 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001994 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001995 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02001996 }
1997
1998 /* Find function for current state and message */
1999 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002000 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002001 && ((1 << trans->cc.state) & downstatelist[i].states))
2002 break;
2003 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002004 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002005 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002006 return 0;
2007 }
2008
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002009 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002010
2011 return rc;
2012}
2013
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002014struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2015{
2016 uint32_t callref;
2017
2018 switch (msg->msg_type) {
2019 case MNCC_BRIDGE:
2020 callref = msg->bridge.callref[0];
2021 break;
2022 case MNCC_RTP_CREATE:
2023 case MNCC_RTP_CONNECT:
2024 callref = msg->rtp.callref;
2025 break;
2026
2027 case MNCC_RTP_FREE:
2028 case MNCC_FRAME_DROP:
2029 case MNCC_FRAME_RECV:
2030 case GSM_TCHF_FRAME:
2031 case GSM_TCHF_FRAME_EFR:
2032 case GSM_TCHH_FRAME:
2033 case GSM_TCH_FRAME_AMR:
2034 return NULL;
2035
2036 default:
2037 callref = msg->signal.callref;
2038 break;
2039 }
2040
2041 return mncc_call_find_by_callref(callref);
2042}
2043
2044/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002045int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002046{
2047 const union mncc_msg *msg = arg;
2048 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002049
2050 if (msg->msg_type == MNCC_SETUP_REQ) {
2051 /* Incoming call to forward for inter-MSC Handover? */
2052 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2053 if (mncc_call)
2054 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2055 "Incoming call matches pending inter-MSC Handover Number\n");
2056 }
2057 if (!mncc_call) {
2058 /* Find already active MNCC FSM for this callref.
2059 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2060 * MNCC<->GSM-CC call handling. */
2061 mncc_call = mncc_find_by_callref_from_msg(msg);
2062 }
2063 if (mncc_call) {
2064 mncc_call_rx(mncc_call, msg);
2065 return 0;
2066 }
2067
2068 /* None of the above? Then it must be a normal GSM CC call related message. */
2069 return mncc_tx_to_gsm_cc(net, msg);
2070}
Harald Welte27989d42018-06-21 20:39:20 +02002071
2072static struct datastate {
2073 uint32_t states;
2074 int type;
2075 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2076} datastatelist[] = {
2077 /* mobile originating call establishment */
2078 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2079 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2080 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2081 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2082 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2083 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2084 /* mobile terminating call establishment */
2085 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2086 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2087 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2088 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2089 {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 */
2090 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2091 /* signalling during call */
2092 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2093 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2094 {SBIT(GSM_CSTATE_ACTIVE),
2095 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2096 {ALL_STATES,
2097 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2098 {ALL_STATES,
2099 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2100 {ALL_STATES,
2101 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2102 {SBIT(GSM_CSTATE_ACTIVE),
2103 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2104 {SBIT(GSM_CSTATE_ACTIVE),
2105 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2106 {SBIT(GSM_CSTATE_ACTIVE),
2107 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2108 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2109 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2110 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2111 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2112 {SBIT(GSM_CSTATE_ACTIVE),
2113 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2114 /* clearing */
2115 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2116 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2117 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2118 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2119 {ALL_STATES, /* 5.4.3.4 */
2120 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2121};
2122
2123#define DATASLLEN \
2124 (sizeof(datastatelist) / sizeof(struct datastate))
2125
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002126int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002127{
2128 struct gsm48_hdr *gh = msgb_l3(msg);
2129 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2130 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2131 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002132 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2133 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002134 int i, rc = 0;
2135
2136 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002137 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002138 return -EINVAL;
2139 }
2140
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002141 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002142 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002143 return -EINVAL;
2144 }
2145
2146 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002147 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002148
Harald Welte27989d42018-06-21 20:39:20 +02002149 /* Create transaction */
2150 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002151 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002152 trans = trans_alloc(net, vsub,
2153 TRANS_CC,
2154 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002155 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002156 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002157 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002158 GSM48_PDISC_CC | (transaction_id << 4),
2159 GSM48_MT_CC_RELEASE_COMPL);
2160 return -ENOMEM;
2161 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002162 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2163 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2164 trans_free(trans);
2165 return -EINVAL;
2166 }
2167
Harald Welte27989d42018-06-21 20:39:20 +02002168 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002169 msc_a_get(msc_a, MSC_A_USE_CC);
2170 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002171 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002172
2173 /* An earlier CM Service Request for this CC message now has concluded */
2174 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2175 LOG_MSC_A(msc_a, LOGL_ERROR,
2176 "Creating new CC transaction without prior CM Service Request\n");
2177 else
2178 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002179 }
2180
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002181 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2182 gsm48_cc_state_name(trans->cc.state));
2183
Harald Welte27989d42018-06-21 20:39:20 +02002184 /* find function for current state and message */
2185 for (i = 0; i < DATASLLEN; i++)
2186 if ((msg_type == datastatelist[i].type)
2187 && ((1 << trans->cc.state) & datastatelist[i].states))
2188 break;
2189 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002190 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002191
2192 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2193 * transaction right away. */
2194 if (trans->cc.state == GSM_CSTATE_NULL) {
2195 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2196 " -- disarding new CC transaction right away\n");
2197 trans_free(trans);
2198 }
Harald Welte27989d42018-06-21 20:39:20 +02002199 return 0;
2200 }
2201
2202 assert(trans->vsub);
2203
2204 rc = datastatelist[i].rout(trans, msg);
2205
Harald Welte27989d42018-06-21 20:39:20 +02002206 return rc;
2207}