blob: 704e5964efbf1f0ecfe34394a0ab7b82ee4e01dc [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) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100275 /* Send MNCC REL.ind (cause='Resource unavailable') */
276 if (trans->cc.mncc_initiated) {
277 mncc_release_ind(trans->net, trans, trans->callref,
278 GSM48_CAUSE_LOC_PRN_S_LU,
279 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
280 }
281
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100282 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
283 * CC Release to the MS if it gets freed here. Hack it to do so. */
284 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
285 struct gsm_mncc rel = {};
286 rel.callref = trans->callref;
287 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
288 gsm48_cc_tx_release(trans, &rel);
289 }
Harald Welte27989d42018-06-21 20:39:20 +0200290 /* This is a final freeing of the transaction. The MNCC release may have triggered the
291 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
292 gsm48_stop_cc_timer(trans);
293 }
294 if (trans->cc.state != GSM_CSTATE_NULL)
295 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200296
297 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100298
299 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
300 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200301}
302
Harald Welte27989d42018-06-21 20:39:20 +0200303/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100304static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200305{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100306 if (trans->msc_a) {
307 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
308 "Handle paging error: transaction already associated with subscriber,"
309 " apparently it was already handled. Skip.\n");
310 return;
Harald Welte27989d42018-06-21 20:39:20 +0200311 }
312
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100313 if (msc_a) {
314 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
315 /* Assign conn */
316 msc_a_get(msc_a, MSC_A_USE_CC);
317 trans->msc_a = msc_a;
318 trans->paging_request = NULL;
319 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
320 /* send SETUP request to called party */
321 gsm48_cc_tx_setup(trans, &trans->cc.msg);
322 } else {
323 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
324 /* Temporarily out of order */
325 mncc_release_ind(trans->net, trans,
326 trans->callref,
327 GSM48_CAUSE_LOC_PRN_S_LU,
328 GSM48_CC_CAUSE_DEST_OOO);
329 trans->callref = 0;
330 trans->paging_request = NULL;
331 trans_free(trans);
332 }
Harald Welte27989d42018-06-21 20:39:20 +0200333}
334
335/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100336static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200337{
338 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
339 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100340 struct call_leg *cl1;
341 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200342
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100343 if (!trans1 || !trans2) {
344 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200345 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100346 }
Harald Welte27989d42018-06-21 20:39:20 +0200347
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100348 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100349 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
350 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 +0200351 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100352 }
353
354 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
355 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200356
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100357 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
358 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
359 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
360 * If we can't, then we must give up. */
361 cl1 = trans1->msc_a->cc.call_leg;
362 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200363
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100364 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200365}
366
367static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
368{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100369 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200370 return gsm48_cc_tx_status(trans, msg);
371}
372
Harald Welte27989d42018-06-21 20:39:20 +0200373static void gsm48_cc_timeout(void *arg)
374{
375 struct gsm_trans *trans = arg;
376 int disconnect = 0, release = 0;
377 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
378 int mo_location = GSM48_CAUSE_LOC_USER;
379 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
380 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
381 struct gsm_mncc mo_rel, l4_rel;
382
383 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
384 mo_rel.callref = trans->callref;
385 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
386 l4_rel.callref = trans->callref;
387
388 switch(trans->cc.Tcurrent) {
389 case 0x303:
390 release = 1;
391 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
392 break;
393 case 0x310:
394 disconnect = 1;
395 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
396 break;
397 case 0x313:
398 disconnect = 1;
399 /* unknown, did not find it in the specs */
400 break;
401 case 0x301:
402 disconnect = 1;
403 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
404 break;
405 case 0x308:
406 if (!trans->cc.T308_second) {
407 /* restart T308 a second time */
408 gsm48_cc_tx_release(trans, &trans->cc.msg);
409 trans->cc.T308_second = 1;
410 break; /* stay in release state */
411 }
412 trans_free(trans);
413 return;
414 case 0x306:
415 release = 1;
416 mo_cause = trans->cc.msg.cause.value;
417 mo_location = trans->cc.msg.cause.location;
418 break;
419 case 0x323:
420 disconnect = 1;
421 break;
422 default:
423 release = 1;
424 }
425
426 if (release && trans->callref) {
427 /* process release towards layer 4 */
428 mncc_release_ind(trans->net, trans, trans->callref,
429 l4_location, l4_cause);
430 trans->callref = 0;
431 }
432
433 if (disconnect && trans->callref) {
434 /* process disconnect towards layer 4 */
435 mncc_set_cause(&l4_rel, l4_location, l4_cause);
436 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
437 }
438
439 /* process disconnect towards mobile station */
440 if (disconnect || release) {
441 mncc_set_cause(&mo_rel, mo_location, mo_cause);
442 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
443 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
444 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
445 mo_rel.cause.diag_len = 3;
446
447 if (disconnect)
448 gsm48_cc_tx_disconnect(trans, &mo_rel);
449 if (release)
450 gsm48_cc_tx_release(trans, &mo_rel);
451 }
452
453}
454
455/* disconnect both calls from the bridge */
456static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100457 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200458{
459 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
460 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
461 struct gsm_mncc mx_rel;
462 if (!trans0 || !trans1)
463 return;
464
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100465 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
466 trans0->callref, trans1->callref, strerror(err));
467 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200468 trans0->callref, trans1->callref, strerror(err));
469
470 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
471 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
472 GSM48_CC_CAUSE_CHAN_UNACCEPT);
473
474 mx_rel.callref = trans0->callref;
475 gsm48_cc_tx_disconnect(trans0, &mx_rel);
476
477 mx_rel.callref = trans1->callref;
478 gsm48_cc_tx_disconnect(trans1, &mx_rel);
479}
480
481static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
482 int sec, int micro)
483{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100484 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200485 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
486 osmo_timer_schedule(&trans->cc.timer, sec, micro);
487 trans->cc.Tcurrent = current;
488}
489
490static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
491{
492 struct gsm48_hdr *gh = msgb_l3(msg);
493 uint8_t msg_type = gsm48_hdr_msg_type(gh);
494 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
495 struct tlv_parsed tp;
496 struct gsm_mncc setup;
497
Philipp Maier9ca7b312018-10-10 17:00:49 +0200498 gsm48_start_guard_timer(trans);
499
Harald Welte27989d42018-06-21 20:39:20 +0200500 memset(&setup, 0, sizeof(struct gsm_mncc));
501 setup.callref = trans->callref;
502
503 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
504 /* emergency setup is identified by msg_type */
505 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
506 setup.fields |= MNCC_F_EMERGENCY;
507 setup.emergency = 1;
508 /* use destination number as configured by user (if any) */
509 if (trans->net->emergency.route_to_msisdn) {
510 setup.fields |= MNCC_F_CALLED;
511 setup.called.type = 0; /* unknown */
512 setup.called.plan = 0; /* unknown */
513 OSMO_STRLCPY_ARRAY(setup.called.number,
514 trans->net->emergency.route_to_msisdn);
515 }
516 }
517
518 /* use subscriber as calling party number */
519 setup.fields |= MNCC_F_CALLING;
520 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
521 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
522
523 /* bearer capability */
524 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
525 setup.fields |= MNCC_F_BEARER_CAP;
526 gsm48_decode_bearer_cap(&setup.bearer_cap,
527 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
528
529 /* Create a copy of the bearer capability
530 * in the transaction struct, so we can use
531 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100532 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200533 sizeof(trans->bearer_cap));
534 }
535 /* facility */
536 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
537 setup.fields |= MNCC_F_FACILITY;
538 gsm48_decode_facility(&setup.facility,
539 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
540 }
541 /* called party bcd number */
542 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
543 setup.fields |= MNCC_F_CALLED;
544 gsm48_decode_called(&setup.called,
545 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
546 }
547 /* user-user */
548 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
549 setup.fields |= MNCC_F_USERUSER;
550 gsm48_decode_useruser(&setup.useruser,
551 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
552 }
553 /* ss-version */
554 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
555 setup.fields |= MNCC_F_SSVERSION;
556 gsm48_decode_ssversion(&setup.ssversion,
557 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
558 }
559 /* CLIR suppression */
560 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
561 setup.clir.sup = 1;
562 /* CLIR invocation */
563 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
564 setup.clir.inv = 1;
565 /* cc cap */
566 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
567 setup.fields |= MNCC_F_CCCAP;
568 gsm48_decode_cccap(&setup.cccap,
569 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
570 }
571
572 new_cc_state(trans, GSM_CSTATE_INITIATED);
573
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100574 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
575 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200576
577 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
578
579 /* indicate setup to MNCC */
580 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
581
582 /* MNCC code will modify the channel asynchronously, we should
583 * ipaccess-bind only after the modification has been made to the
584 * lchan->tch_mode */
585 return 0;
586}
587
588static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
589{
590 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
591 struct gsm48_hdr *gh;
592 struct gsm_mncc *setup = arg;
593 int rc, trans_id;
594
595 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
596
597 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700598 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100599 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200600 "This is not allowed!\n");
601 /* Temporarily out of order */
602 rc = mncc_release_ind(trans->net, trans, trans->callref,
603 GSM48_CAUSE_LOC_PRN_S_LU,
604 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
605 trans->callref = 0;
606 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200607 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200608 return rc;
609 }
610
611 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100612 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200613 if (trans_id < 0) {
614 /* no free transaction ID */
615 rc = mncc_release_ind(trans->net, trans, trans->callref,
616 GSM48_CAUSE_LOC_PRN_S_LU,
617 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
618 trans->callref = 0;
619 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200620 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200621 return rc;
622 }
623 trans->transaction_id = trans_id;
624
625 gh->msg_type = GSM48_MT_CC_SETUP;
626
627 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
628
629 /* bearer capability */
630 if (setup->fields & MNCC_F_BEARER_CAP) {
631 /* Create a copy of the bearer capability in the transaction struct, so we
632 * can use this information later */
633 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
634 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
635 }
636 /* facility */
637 if (setup->fields & MNCC_F_FACILITY)
638 gsm48_encode_facility(msg, 0, &setup->facility);
639 /* progress */
640 if (setup->fields & MNCC_F_PROGRESS)
641 gsm48_encode_progress(msg, 0, &setup->progress);
642 /* calling party BCD number */
643 if (setup->fields & MNCC_F_CALLING)
644 gsm48_encode_calling(msg, &setup->calling);
645 /* called party BCD number */
646 if (setup->fields & MNCC_F_CALLED)
647 gsm48_encode_called(msg, &setup->called);
648 /* user-user */
649 if (setup->fields & MNCC_F_USERUSER)
650 gsm48_encode_useruser(msg, 0, &setup->useruser);
651 /* redirecting party BCD number */
652 if (setup->fields & MNCC_F_REDIRECTING)
653 gsm48_encode_redirecting(msg, &setup->redirecting);
654 /* signal */
655 if (setup->fields & MNCC_F_SIGNAL)
656 gsm48_encode_signal(msg, setup->signal);
657
658 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
659
660 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
661
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100662 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200663}
664
665static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
666{
667 struct gsm48_hdr *gh = msgb_l3(msg);
668 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
669 struct tlv_parsed tp;
670 struct gsm_mncc call_conf;
671 int rc;
672
673 gsm48_stop_cc_timer(trans);
674 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
675
676 memset(&call_conf, 0, sizeof(struct gsm_mncc));
677 call_conf.callref = trans->callref;
678
679 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
680#if 0
681 /* repeat */
682 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
683 call_conf.repeat = 1;
684 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
685 call_conf.repeat = 2;
686#endif
687 /* bearer capability */
688 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
689 call_conf.fields |= MNCC_F_BEARER_CAP;
690 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
691 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
692
693 /* Create a copy of the bearer capability
694 * in the transaction struct, so we can use
695 * this information later */
696 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
697 sizeof(trans->bearer_cap));
698 }
699 /* cause */
700 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
701 call_conf.fields |= MNCC_F_CAUSE;
702 gsm48_decode_cause(&call_conf.cause,
703 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
704 }
705 /* cc cap */
706 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
707 call_conf.fields |= MNCC_F_CCCAP;
708 gsm48_decode_cccap(&call_conf.cccap,
709 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
710 }
711
712 /* IMSI of called subscriber */
713 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
714
715 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
716
717 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100718 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200719
720 /* don't continue, if there were problems with
721 * the call assignment. */
722 if (rc)
723 return rc;
724
725 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
726 &call_conf);
727}
728
729static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
730{
731 struct gsm_mncc *proceeding = arg;
732 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
733 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
734 int rc;
735
736 gh->msg_type = GSM48_MT_CC_CALL_PROC;
737
738 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
739
740 /* bearer capability */
741 if (proceeding->fields & MNCC_F_BEARER_CAP) {
742 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
743 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
744 }
745 /* facility */
746 if (proceeding->fields & MNCC_F_FACILITY)
747 gsm48_encode_facility(msg, 0, &proceeding->facility);
748 /* progress */
749 if (proceeding->fields & MNCC_F_PROGRESS)
750 gsm48_encode_progress(msg, 0, &proceeding->progress);
751
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100752 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200753 if (rc)
754 return rc;
755
756 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100757 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200758}
759
760static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
761{
762 struct gsm48_hdr *gh = msgb_l3(msg);
763 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
764 struct tlv_parsed tp;
765 struct gsm_mncc alerting;
766
767 gsm48_stop_cc_timer(trans);
768 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
769
770 memset(&alerting, 0, sizeof(struct gsm_mncc));
771 alerting.callref = trans->callref;
772 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
773 /* facility */
774 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
775 alerting.fields |= MNCC_F_FACILITY;
776 gsm48_decode_facility(&alerting.facility,
777 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
778 }
779
780 /* progress */
781 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
782 alerting.fields |= MNCC_F_PROGRESS;
783 gsm48_decode_progress(&alerting.progress,
784 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
785 }
786 /* ss-version */
787 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
788 alerting.fields |= MNCC_F_SSVERSION;
789 gsm48_decode_ssversion(&alerting.ssversion,
790 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
791 }
792
793 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
794
795 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
796 &alerting);
797}
798
799static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
800{
801 struct gsm_mncc *alerting = arg;
802 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
803 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
804
805 gh->msg_type = GSM48_MT_CC_ALERTING;
806
807 /* facility */
808 if (alerting->fields & MNCC_F_FACILITY)
809 gsm48_encode_facility(msg, 0, &alerting->facility);
810 /* progress */
811 if (alerting->fields & MNCC_F_PROGRESS)
812 gsm48_encode_progress(msg, 0, &alerting->progress);
813 /* user-user */
814 if (alerting->fields & MNCC_F_USERUSER)
815 gsm48_encode_useruser(msg, 0, &alerting->useruser);
816
817 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
818
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100819 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200820}
821
822static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
823{
824 struct gsm_mncc *progress = arg;
825 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
826 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
827
828 gh->msg_type = GSM48_MT_CC_PROGRESS;
829
830 /* progress */
831 gsm48_encode_progress(msg, 1, &progress->progress);
832 /* user-user */
833 if (progress->fields & MNCC_F_USERUSER)
834 gsm48_encode_useruser(msg, 0, &progress->useruser);
835
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100836 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200837}
838
839static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
840{
841 struct gsm_mncc *connect = arg;
842 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
843 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
844
845 gh->msg_type = GSM48_MT_CC_CONNECT;
846
847 gsm48_stop_cc_timer(trans);
848 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
849
850 /* facility */
851 if (connect->fields & MNCC_F_FACILITY)
852 gsm48_encode_facility(msg, 0, &connect->facility);
853 /* progress */
854 if (connect->fields & MNCC_F_PROGRESS)
855 gsm48_encode_progress(msg, 0, &connect->progress);
856 /* connected number */
857 if (connect->fields & MNCC_F_CONNECTED)
858 gsm48_encode_connected(msg, &connect->connected);
859 /* user-user */
860 if (connect->fields & MNCC_F_USERUSER)
861 gsm48_encode_useruser(msg, 0, &connect->useruser);
862
863 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
864
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100865 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200866}
867
868static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
869{
870 struct gsm48_hdr *gh = msgb_l3(msg);
871 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
872 struct tlv_parsed tp;
873 struct gsm_mncc connect;
874
875 gsm48_stop_cc_timer(trans);
876
877 memset(&connect, 0, sizeof(struct gsm_mncc));
878 connect.callref = trans->callref;
879 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
880 /* use subscriber as connected party number */
881 connect.fields |= MNCC_F_CONNECTED;
882 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
883 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
884
885 /* facility */
886 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
887 connect.fields |= MNCC_F_FACILITY;
888 gsm48_decode_facility(&connect.facility,
889 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
890 }
891 /* user-user */
892 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
893 connect.fields |= MNCC_F_USERUSER;
894 gsm48_decode_useruser(&connect.useruser,
895 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
896 }
897 /* ss-version */
898 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
899 connect.fields |= MNCC_F_SSVERSION;
900 gsm48_decode_ssversion(&connect.ssversion,
901 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
902 }
903
904 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
905 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
906
907 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
908}
909
910
911static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
912{
913 struct gsm_mncc connect_ack;
914
915 gsm48_stop_cc_timer(trans);
916
917 new_cc_state(trans, GSM_CSTATE_ACTIVE);
918 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
919
920 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
921 connect_ack.callref = trans->callref;
922
923 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
924 &connect_ack);
925}
926
927static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
928{
929 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
930 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
931
932 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
933
934 new_cc_state(trans, GSM_CSTATE_ACTIVE);
935
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100936 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200937}
938
939static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
940{
941 struct gsm48_hdr *gh = msgb_l3(msg);
942 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
943 struct tlv_parsed tp;
944 struct gsm_mncc disc;
945
946 gsm48_stop_cc_timer(trans);
947
948 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
949
950 memset(&disc, 0, sizeof(struct gsm_mncc));
951 disc.callref = trans->callref;
952 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
953 /* cause */
954 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
955 disc.fields |= MNCC_F_CAUSE;
956 gsm48_decode_cause(&disc.cause,
957 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
958 }
959 /* facility */
960 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
961 disc.fields |= MNCC_F_FACILITY;
962 gsm48_decode_facility(&disc.facility,
963 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
964 }
965 /* user-user */
966 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
967 disc.fields |= MNCC_F_USERUSER;
968 gsm48_decode_useruser(&disc.useruser,
969 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
970 }
971 /* ss-version */
972 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
973 disc.fields |= MNCC_F_SSVERSION;
974 gsm48_decode_ssversion(&disc.ssversion,
975 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
976 }
977
978 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +0200979}
980
981static struct gsm_mncc_cause default_cause = {
982 .location = GSM48_CAUSE_LOC_PRN_S_LU,
983 .coding = 0,
984 .rec = 0,
985 .rec_val = 0,
986 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
987 .diag_len = 0,
988 .diag = { 0 },
989};
990
991static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
992{
993 struct gsm_mncc *disc = arg;
994 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
995 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
996
997 gh->msg_type = GSM48_MT_CC_DISCONNECT;
998
999 gsm48_stop_cc_timer(trans);
1000 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1001
1002 /* cause */
1003 if (disc->fields & MNCC_F_CAUSE)
1004 gsm48_encode_cause(msg, 1, &disc->cause);
1005 else
1006 gsm48_encode_cause(msg, 1, &default_cause);
1007
1008 /* facility */
1009 if (disc->fields & MNCC_F_FACILITY)
1010 gsm48_encode_facility(msg, 0, &disc->facility);
1011 /* progress */
1012 if (disc->fields & MNCC_F_PROGRESS)
1013 gsm48_encode_progress(msg, 0, &disc->progress);
1014 /* user-user */
1015 if (disc->fields & MNCC_F_USERUSER)
1016 gsm48_encode_useruser(msg, 0, &disc->useruser);
1017
1018 /* store disconnect cause for T306 expiry */
1019 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1020
1021 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1022
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001023 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001024}
1025
1026static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1027{
1028 struct gsm48_hdr *gh = msgb_l3(msg);
1029 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1030 struct tlv_parsed tp;
1031 struct gsm_mncc rel;
1032 int rc;
1033
1034 gsm48_stop_cc_timer(trans);
1035
1036 memset(&rel, 0, sizeof(struct gsm_mncc));
1037 rel.callref = trans->callref;
1038 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1039 /* cause */
1040 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1041 rel.fields |= MNCC_F_CAUSE;
1042 gsm48_decode_cause(&rel.cause,
1043 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1044 }
1045 /* facility */
1046 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1047 rel.fields |= MNCC_F_FACILITY;
1048 gsm48_decode_facility(&rel.facility,
1049 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1050 }
1051 /* user-user */
1052 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1053 rel.fields |= MNCC_F_USERUSER;
1054 gsm48_decode_useruser(&rel.useruser,
1055 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1056 }
1057 /* ss-version */
1058 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1059 rel.fields |= MNCC_F_SSVERSION;
1060 gsm48_decode_ssversion(&rel.ssversion,
1061 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1062 }
1063
1064 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1065 /* release collision 5.4.5 */
1066 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1067 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001068 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001069 GSM48_PDISC_CC | (trans->transaction_id << 4),
1070 GSM48_MT_CC_RELEASE_COMPL);
1071 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1072 }
1073
1074 new_cc_state(trans, GSM_CSTATE_NULL);
1075
1076 trans->callref = 0;
1077 trans_free(trans);
1078
1079 return rc;
1080}
1081
1082static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1083{
1084 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001085 struct msgb *msg;
1086 struct gsm48_hdr *gh;
1087
1088 if (!trans->msc_a) {
1089 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1090 return -EINVAL;
1091 }
1092
1093 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1094 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001095
1096 gh->msg_type = GSM48_MT_CC_RELEASE;
1097
1098 gsm48_stop_cc_timer(trans);
1099 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1100
1101 /* cause */
1102 if (rel->fields & MNCC_F_CAUSE)
1103 gsm48_encode_cause(msg, 0, &rel->cause);
1104 /* facility */
1105 if (rel->fields & MNCC_F_FACILITY)
1106 gsm48_encode_facility(msg, 0, &rel->facility);
1107 /* user-user */
1108 if (rel->fields & MNCC_F_USERUSER)
1109 gsm48_encode_useruser(msg, 0, &rel->useruser);
1110
1111 trans->cc.T308_second = 0;
1112 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1113
1114 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1115 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1116
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001117 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001118}
1119
1120static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1121{
1122 struct gsm48_hdr *gh = msgb_l3(msg);
1123 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1124 struct tlv_parsed tp;
1125 struct gsm_mncc rel;
1126 int rc = 0;
1127
1128 gsm48_stop_cc_timer(trans);
1129
1130 memset(&rel, 0, sizeof(struct gsm_mncc));
1131 rel.callref = trans->callref;
1132 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1133 /* cause */
1134 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1135 rel.fields |= MNCC_F_CAUSE;
1136 gsm48_decode_cause(&rel.cause,
1137 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1138 }
1139 /* facility */
1140 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1141 rel.fields |= MNCC_F_FACILITY;
1142 gsm48_decode_facility(&rel.facility,
1143 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1144 }
1145 /* user-user */
1146 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1147 rel.fields |= MNCC_F_USERUSER;
1148 gsm48_decode_useruser(&rel.useruser,
1149 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1150 }
1151 /* ss-version */
1152 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1153 rel.fields |= MNCC_F_SSVERSION;
1154 gsm48_decode_ssversion(&rel.ssversion,
1155 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1156 }
1157
1158 if (trans->callref) {
1159 switch (trans->cc.state) {
1160 case GSM_CSTATE_CALL_PRESENT:
1161 rc = mncc_recvmsg(trans->net, trans,
1162 MNCC_REJ_IND, &rel);
1163 break;
1164 case GSM_CSTATE_RELEASE_REQ:
1165 rc = mncc_recvmsg(trans->net, trans,
1166 MNCC_REL_CNF, &rel);
1167 break;
1168 default:
1169 rc = mncc_recvmsg(trans->net, trans,
1170 MNCC_REL_IND, &rel);
1171 }
1172 }
1173
1174 trans->callref = 0;
1175 trans_free(trans);
1176
1177 return rc;
1178}
1179
1180static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1181{
1182 struct gsm_mncc *rel = arg;
1183 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1184 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1185 int ret;
1186
1187 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1188
1189 trans->callref = 0;
1190
1191 gsm48_stop_cc_timer(trans);
1192
1193 /* cause */
1194 if (rel->fields & MNCC_F_CAUSE)
1195 gsm48_encode_cause(msg, 0, &rel->cause);
1196 /* facility */
1197 if (rel->fields & MNCC_F_FACILITY)
1198 gsm48_encode_facility(msg, 0, &rel->facility);
1199 /* user-user */
1200 if (rel->fields & MNCC_F_USERUSER)
1201 gsm48_encode_useruser(msg, 0, &rel->useruser);
1202
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001203 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001204
1205 trans_free(trans);
1206
1207 return ret;
1208}
1209
1210static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1211{
1212 struct gsm48_hdr *gh = msgb_l3(msg);
1213 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1214 struct tlv_parsed tp;
1215 struct gsm_mncc fac;
1216
1217 memset(&fac, 0, sizeof(struct gsm_mncc));
1218 fac.callref = trans->callref;
1219 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1220 /* facility */
1221 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1222 fac.fields |= MNCC_F_FACILITY;
1223 gsm48_decode_facility(&fac.facility,
1224 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1225 }
1226 /* ss-version */
1227 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1228 fac.fields |= MNCC_F_SSVERSION;
1229 gsm48_decode_ssversion(&fac.ssversion,
1230 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1231 }
1232
1233 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1234}
1235
1236static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1237{
1238 struct gsm_mncc *fac = arg;
1239 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1240 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1241
1242 gh->msg_type = GSM48_MT_CC_FACILITY;
1243
1244 /* facility */
1245 gsm48_encode_facility(msg, 1, &fac->facility);
1246
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001247 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001248}
1249
1250static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1251{
1252 struct gsm_mncc hold;
1253
1254 memset(&hold, 0, sizeof(struct gsm_mncc));
1255 hold.callref = trans->callref;
1256 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1257}
1258
1259static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1260{
1261 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1262 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1263
1264 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1265
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001266 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001267}
1268
1269static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1270{
1271 struct gsm_mncc *hold_rej = arg;
1272 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1273 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1274
1275 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1276
1277 /* cause */
1278 if (hold_rej->fields & MNCC_F_CAUSE)
1279 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1280 else
1281 gsm48_encode_cause(msg, 1, &default_cause);
1282
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001283 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001284}
1285
1286static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1287{
1288 struct gsm_mncc retrieve;
1289
1290 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1291 retrieve.callref = trans->callref;
1292 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1293 &retrieve);
1294}
1295
1296static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1297{
1298 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1299 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1300
1301 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1302
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001303 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001304}
1305
1306static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1307{
1308 struct gsm_mncc *retrieve_rej = arg;
1309 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1310 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1311
1312 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1313
1314 /* cause */
1315 if (retrieve_rej->fields & MNCC_F_CAUSE)
1316 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1317 else
1318 gsm48_encode_cause(msg, 1, &default_cause);
1319
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001320 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001321}
1322
1323static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1324{
1325 struct gsm48_hdr *gh = msgb_l3(msg);
1326 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1327 struct tlv_parsed tp;
1328 struct gsm_mncc dtmf;
1329
1330 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1331 dtmf.callref = trans->callref;
1332 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1333 /* keypad facility */
1334 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1335 dtmf.fields |= MNCC_F_KEYPAD;
1336 gsm48_decode_keypad(&dtmf.keypad,
1337 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1338 }
1339
1340 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1341}
1342
1343static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1344{
1345 struct gsm_mncc *dtmf = arg;
1346 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1347 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1348
1349 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1350
1351 /* keypad */
1352 if (dtmf->fields & MNCC_F_KEYPAD)
1353 gsm48_encode_keypad(msg, dtmf->keypad);
1354
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001355 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001356}
1357
1358static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1359{
1360 struct gsm_mncc *dtmf = arg;
1361 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1362 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1363
1364 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1365
1366 /* cause */
1367 if (dtmf->fields & MNCC_F_CAUSE)
1368 gsm48_encode_cause(msg, 1, &dtmf->cause);
1369 else
1370 gsm48_encode_cause(msg, 1, &default_cause);
1371
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001372 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001373}
1374
1375static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1376{
1377 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1379
1380 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1381
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001382 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001383}
1384
1385static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1386{
1387 struct gsm_mncc dtmf;
1388
1389 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1390 dtmf.callref = trans->callref;
1391
1392 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1393}
1394
1395static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1396{
1397 struct gsm48_hdr *gh = msgb_l3(msg);
1398 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1399 struct tlv_parsed tp;
1400 struct gsm_mncc modify;
1401
1402 memset(&modify, 0, sizeof(struct gsm_mncc));
1403 modify.callref = trans->callref;
1404 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1405 /* bearer capability */
1406 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1407 modify.fields |= MNCC_F_BEARER_CAP;
1408 gsm48_decode_bearer_cap(&modify.bearer_cap,
1409 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1410
1411 /* Create a copy of the bearer capability
1412 * in the transaction struct, so we can use
1413 * this information later */
1414 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1415 sizeof(trans->bearer_cap));
1416 }
1417
1418 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1419
1420 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1421}
1422
1423static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1424{
1425 struct gsm_mncc *modify = arg;
1426 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1427 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1428
1429 gh->msg_type = GSM48_MT_CC_MODIFY;
1430
1431 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1432
1433 /* bearer capability */
1434 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1435 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1436
1437 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1438
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001439 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001440}
1441
1442static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1443{
1444 struct gsm48_hdr *gh = msgb_l3(msg);
1445 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1446 struct tlv_parsed tp;
1447 struct gsm_mncc modify;
1448
1449 gsm48_stop_cc_timer(trans);
1450
1451 memset(&modify, 0, sizeof(struct gsm_mncc));
1452 modify.callref = trans->callref;
1453 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1454 /* bearer capability */
1455 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1456 modify.fields |= MNCC_F_BEARER_CAP;
1457 gsm48_decode_bearer_cap(&modify.bearer_cap,
1458 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1459
1460 /* Create a copy of the bearer capability
1461 * in the transaction struct, so we can use
1462 * this information later */
1463 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1464 sizeof(trans->bearer_cap));
1465 }
1466
1467 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1468
1469 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1470}
1471
1472static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1473{
1474 struct gsm_mncc *modify = arg;
1475 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1476 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1477
1478 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1479
1480 /* bearer capability */
1481 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1482 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1483
1484 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1485
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001486 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001487}
1488
1489static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1490{
1491 struct gsm48_hdr *gh = msgb_l3(msg);
1492 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1493 struct tlv_parsed tp;
1494 struct gsm_mncc modify;
1495
1496 gsm48_stop_cc_timer(trans);
1497
1498 memset(&modify, 0, sizeof(struct gsm_mncc));
1499 modify.callref = trans->callref;
1500 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1501 /* bearer capability */
1502 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1503 modify.fields |= GSM48_IE_BEARER_CAP;
1504 gsm48_decode_bearer_cap(&modify.bearer_cap,
1505 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1506
1507 /* Create a copy of the bearer capability
1508 * in the transaction struct, so we can use
1509 * this information later */
1510 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1511 sizeof(trans->bearer_cap));
1512 }
1513 /* cause */
1514 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1515 modify.fields |= MNCC_F_CAUSE;
1516 gsm48_decode_cause(&modify.cause,
1517 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1518 }
1519
1520 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1521
1522 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1523}
1524
1525static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1526{
1527 struct gsm_mncc *modify = arg;
1528 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1529 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1530
1531 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1532
1533 /* bearer capability */
1534 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1535 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1536 /* cause */
1537 gsm48_encode_cause(msg, 1, &modify->cause);
1538
1539 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1540
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001541 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001542}
1543
1544static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1545{
1546 struct gsm_mncc *notify = arg;
1547 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1548 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1549
1550 gh->msg_type = GSM48_MT_CC_NOTIFY;
1551
1552 /* notify */
1553 gsm48_encode_notify(msg, notify->notify);
1554
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001555 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001556}
1557
1558static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1559{
1560 struct gsm48_hdr *gh = msgb_l3(msg);
1561 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1562// struct tlv_parsed tp;
1563 struct gsm_mncc notify;
1564
1565 memset(&notify, 0, sizeof(struct gsm_mncc));
1566 notify.callref = trans->callref;
1567// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1568 if (payload_len >= 1)
1569 gsm48_decode_notify(&notify.notify, gh->data);
1570
1571 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1572}
1573
1574static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1575{
1576 struct gsm_mncc *user = arg;
1577 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1578 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1579
1580 gh->msg_type = GSM48_MT_CC_USER_INFO;
1581
1582 /* user-user */
1583 if (user->fields & MNCC_F_USERUSER)
1584 gsm48_encode_useruser(msg, 1, &user->useruser);
1585 /* more data */
1586 if (user->more)
1587 gsm48_encode_more(msg);
1588
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001589 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001590}
1591
1592static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1593{
1594 struct gsm48_hdr *gh = msgb_l3(msg);
1595 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1596 struct tlv_parsed tp;
1597 struct gsm_mncc user;
1598
1599 memset(&user, 0, sizeof(struct gsm_mncc));
1600 user.callref = trans->callref;
1601 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1602 /* user-user */
1603 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1604 user.fields |= MNCC_F_USERUSER;
1605 gsm48_decode_useruser(&user.useruser,
1606 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1607 }
1608 /* more data */
1609 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1610 user.more = 1;
1611
1612 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1613}
1614
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001615static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1616 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1617 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001618{
1619 uint8_t data[sizeof(struct gsm_mncc)];
1620 struct gsm_mncc_rtp *rtp;
1621
1622 memset(&data, 0, sizeof(data));
1623 rtp = (struct gsm_mncc_rtp *) &data[0];
1624
1625 rtp->callref = callref;
1626 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001627 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001628 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1629 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001630 }
Harald Welte27989d42018-06-21 20:39:20 +02001631 rtp->payload_type = payload_type;
1632 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001633 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001634}
1635
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001636static 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 +02001637{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001638 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001639}
1640
1641static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1642{
1643 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001644
1645 /* Find callref */
1646 trans = trans_find_by_callref(net, callref);
1647 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001648 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001649 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001650 return -EIO;
1651 }
1652 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001653 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001654 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001655 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001656 return 0;
1657 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001658 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001659
Harald Welte27989d42018-06-21 20:39:20 +02001660 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001661 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001662}
1663
1664/* Trigger TCH_RTP_CREATE acknowledgement */
1665int gsm48_tch_rtp_create(struct gsm_trans *trans)
1666{
1667 /* This function is called as soon as the port, on which the
1668 * mgcp-gw expects the incoming RTP stream from the remote
1669 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001670 struct msc_a *msc_a = trans->msc_a;
1671 struct gsm_network *net = msc_a_net(msc_a);
1672 struct call_leg *cl = msc_a->cc.call_leg;
1673 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001674 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1675 uint32_t payload_type;
1676 int payload_msg_type;
1677 const struct mgcp_conn_peer *mgcp_info;
Harald Welte27989d42018-06-21 20:39:20 +02001678
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001679 if (!rtp_cn) {
1680 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1681 return -EINVAL;
1682 }
1683
1684 if (!rtp_cn->codec_known) {
1685 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1686 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1687 return -EINVAL;
1688 }
1689
1690 /* Codec */
1691 payload_msg_type = mgcp_codec_to_mncc_payload_msg_type(rtp_cn->codec);
1692
1693 /* Payload Type number */
1694 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001695 if (mgcp_info && mgcp_info->ptmap_len)
1696 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1697 else
1698 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001699
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001700 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1701 if (!rtp_cn_local) {
1702 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1703 return -EINVAL;
1704 }
1705
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001706 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 +02001707}
1708
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001709static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001710{
1711 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001712 struct call_leg *cl;
1713 struct rtp_stream *rtps;
1714 struct osmo_sockaddr_str rtp_addr;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001715 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001716
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001717 /* FIXME: in *rtp we should get the codec information of the remote
1718 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1719 * meaningful value based on this information but unfortunately we
1720 * can't do that yet because the mncc API can not signal dynamic
1721 * payload types yet. This must be fixed first. Also there may be
1722 * additional members necessary in trans->conn->rtp because we
1723 * somehow need to deal with dynamic payload types that do not
1724 * comply to 3gpp's assumptions of payload type numbers on the A
1725 * interface. See also related tickets: OS#3399 and OS1683 */
1726
Harald Welte27989d42018-06-21 20:39:20 +02001727 /* Find callref */
1728 trans = trans_find_by_callref(net, rtp->callref);
1729 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001730 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001731 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001732 return -EIO;
1733 }
1734 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001735 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001736 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001737 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001738 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001739 }
1740
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001741 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(MNCC_RTP_CONNECT),
1742 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1743 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001744
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001745 cl = trans->msc_a->cc.call_leg;
1746 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1747
1748 if (!rtps) {
1749 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1750 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1751 return -EINVAL;
1752 }
1753
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001754 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1755 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1756 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1757 return -EINVAL;
1758 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001759 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1760 rtp_stream_commit(rtps);
1761 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001762}
1763
1764static struct downstate {
1765 uint32_t states;
1766 int type;
1767 int (*rout) (struct gsm_trans *trans, void *arg);
1768} downstatelist[] = {
1769 /* mobile originating call establishment */
1770 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1771 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1772 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1773 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1774 {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 */
1775 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1776 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1777 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1778 /* mobile terminating call establishment */
1779 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1780 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1781 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1782 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1783 /* signalling during call */
1784 {SBIT(GSM_CSTATE_ACTIVE),
1785 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1786 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1787 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1788 {ALL_STATES,
1789 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1790 {ALL_STATES,
1791 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1792 {ALL_STATES,
1793 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1794 {SBIT(GSM_CSTATE_ACTIVE),
1795 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1796 {SBIT(GSM_CSTATE_ACTIVE),
1797 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1798 {SBIT(GSM_CSTATE_ACTIVE),
1799 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1800 {SBIT(GSM_CSTATE_ACTIVE),
1801 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1802 {SBIT(GSM_CSTATE_ACTIVE),
1803 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1804 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1805 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1806 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1807 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1808 {SBIT(GSM_CSTATE_ACTIVE),
1809 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1810 /* clearing */
1811 {SBIT(GSM_CSTATE_INITIATED),
1812 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1813 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1814 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1815 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1816 MNCC_REL_REQ, gsm48_cc_tx_release},
1817};
1818
1819#define DOWNSLLEN \
1820 (sizeof(downstatelist) / sizeof(struct downstate))
1821
1822
Philipp Maiercd64af72019-08-01 09:46:40 +02001823static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001824{
1825 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001826 struct msc_a *msc_a = NULL;
1827 struct gsm_trans *trans = NULL;
1828 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001829
Harald Welte27989d42018-06-21 20:39:20 +02001830 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001831 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001832 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001833 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001834 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001835 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001836 return rc;
1837 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001838 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001839 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001840 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001841 case MNCC_RTP_FREE:
1842 /* unused right now */
1843 return -EIO;
1844
1845 case MNCC_FRAME_DROP:
1846 case MNCC_FRAME_RECV:
1847 case GSM_TCHF_FRAME:
1848 case GSM_TCHF_FRAME_EFR:
1849 case GSM_TCHH_FRAME:
1850 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001851 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001852 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001853 return -ENOTSUP;
1854 }
1855
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001856 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001857
1858 /* Find callref */
1859 trans = trans_find_by_callref(net, data->callref);
1860
1861 /* Callref unknown */
1862 if (!trans) {
1863 struct vlr_subscr *vsub;
1864
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001865 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001866 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001867 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001868 /* Invalid call reference */
1869 return mncc_release_ind(net, NULL, data->callref,
1870 GSM48_CAUSE_LOC_PRN_S_LU,
1871 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1872 }
1873 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001874 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001875 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001876 /* Invalid number */
1877 return mncc_release_ind(net, NULL, data->callref,
1878 GSM48_CAUSE_LOC_PRN_S_LU,
1879 GSM48_CC_CAUSE_INV_NR_FORMAT);
1880 }
1881 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001882 if (data->called.number[0]) {
1883 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1884 if (!vsub)
1885 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001886 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001887 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001888 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001889 if (!vsub)
1890 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001891 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001892 }
1893 if (!vsub)
1894 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001895 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001896 /* update the subscriber we deal with */
1897 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1898
Harald Welte27989d42018-06-21 20:39:20 +02001899 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001900 if (!vsub->lu_complete) {
1901 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001903 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001904 /* Temporarily out of order */
1905 return mncc_release_ind(net, NULL, data->callref,
1906 GSM48_CAUSE_LOC_PRN_S_LU,
1907 GSM48_CC_CAUSE_DEST_OOO);
1908 }
Keith Whyte991bb422019-08-08 15:43:40 +02001909
1910 /* Find valid conn */
1911 msc_a = msc_a_for_vsub(vsub, true);
1912
1913 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
1914 if (!net->call_waiting && msc_a) {
1915 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
1916 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
1917 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
1918 "rx '%s' for subscriber %s with trans state (%s)"
1919 " rejecting with USER_BUSY\n",
1920 get_mncc_name(msg->msg_type), data->called.number,
1921 gsm48_cc_state_name(existing_cc_trans->cc.state));
1922 return mncc_release_ind(net, NULL, data->callref,
1923 GSM48_CAUSE_LOC_PRN_S_LU,
1924 GSM48_CC_CAUSE_USER_BUSY);
1925 }
1926 }
1927
Harald Welte27989d42018-06-21 20:39:20 +02001928 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001929 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001930 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001931 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001932 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001933 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01001934 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02001935 mncc_release_ind(net, NULL, data->callref,
1936 GSM48_CAUSE_LOC_PRN_S_LU,
1937 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1938 return -ENOMEM;
1939 }
1940
Harald Welte27989d42018-06-21 20:39:20 +02001941 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001942 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02001943 /* This condition will return before the common logging of the received MNCC message below, so
1944 * log it now. */
1945 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001946
Harald Welte27989d42018-06-21 20:39:20 +02001947 /* store setup information until paging succeeds */
1948 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1949
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02001950 /* Request a channel. If Paging already started, paging_request_start() will append the new
1951 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001952 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1953 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001954 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001955 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001956 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001957 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001958 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001959 return 0;
1960 }
1961
1962 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001963 trans->msc_a = msc_a;
1964 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001965 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001966 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001967 } else {
1968 /* update the subscriber we deal with */
1969 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1970 }
1971
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001972 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001973
Philipp Maier9ca7b312018-10-10 17:00:49 +02001974 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02001975 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02001976
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001977 if (trans->msc_a)
1978 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02001979
1980 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001981 if (!msc_a) {
1982 struct gsm_mncc rel = {
1983 .callref = data->callref,
1984 };
1985 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001986 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
1987 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001988 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02001989 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
1990 else
1991 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1992 trans->callref = 0;
1993 trans_free(trans);
1994 return rc;
1995 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001996 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02001998 }
1999
2000 /* Find function for current state and message */
2001 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002002 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002003 && ((1 << trans->cc.state) & downstatelist[i].states))
2004 break;
2005 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002006 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002007 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002008 return 0;
2009 }
2010
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002011 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002012
2013 return rc;
2014}
2015
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2017{
2018 uint32_t callref;
2019
2020 switch (msg->msg_type) {
2021 case MNCC_BRIDGE:
2022 callref = msg->bridge.callref[0];
2023 break;
2024 case MNCC_RTP_CREATE:
2025 case MNCC_RTP_CONNECT:
2026 callref = msg->rtp.callref;
2027 break;
2028
2029 case MNCC_RTP_FREE:
2030 case MNCC_FRAME_DROP:
2031 case MNCC_FRAME_RECV:
2032 case GSM_TCHF_FRAME:
2033 case GSM_TCHF_FRAME_EFR:
2034 case GSM_TCHH_FRAME:
2035 case GSM_TCH_FRAME_AMR:
2036 return NULL;
2037
2038 default:
2039 callref = msg->signal.callref;
2040 break;
2041 }
2042
2043 return mncc_call_find_by_callref(callref);
2044}
2045
2046/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002047int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002048{
2049 const union mncc_msg *msg = arg;
2050 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002051
2052 if (msg->msg_type == MNCC_SETUP_REQ) {
2053 /* Incoming call to forward for inter-MSC Handover? */
2054 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2055 if (mncc_call)
2056 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2057 "Incoming call matches pending inter-MSC Handover Number\n");
2058 }
2059 if (!mncc_call) {
2060 /* Find already active MNCC FSM for this callref.
2061 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2062 * MNCC<->GSM-CC call handling. */
2063 mncc_call = mncc_find_by_callref_from_msg(msg);
2064 }
2065 if (mncc_call) {
2066 mncc_call_rx(mncc_call, msg);
2067 return 0;
2068 }
2069
2070 /* None of the above? Then it must be a normal GSM CC call related message. */
2071 return mncc_tx_to_gsm_cc(net, msg);
2072}
Harald Welte27989d42018-06-21 20:39:20 +02002073
2074static struct datastate {
2075 uint32_t states;
2076 int type;
2077 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2078} datastatelist[] = {
2079 /* mobile originating call establishment */
2080 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2081 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2082 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2083 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2084 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2085 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2086 /* mobile terminating call establishment */
2087 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2088 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2089 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2090 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2091 {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 */
2092 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2093 /* signalling during call */
2094 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2095 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2096 {SBIT(GSM_CSTATE_ACTIVE),
2097 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2098 {ALL_STATES,
2099 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2100 {ALL_STATES,
2101 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2102 {ALL_STATES,
2103 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2104 {SBIT(GSM_CSTATE_ACTIVE),
2105 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2106 {SBIT(GSM_CSTATE_ACTIVE),
2107 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2108 {SBIT(GSM_CSTATE_ACTIVE),
2109 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2110 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2111 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2112 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2113 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2114 {SBIT(GSM_CSTATE_ACTIVE),
2115 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2116 /* clearing */
2117 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2118 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2119 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2120 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2121 {ALL_STATES, /* 5.4.3.4 */
2122 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2123};
2124
2125#define DATASLLEN \
2126 (sizeof(datastatelist) / sizeof(struct datastate))
2127
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002128int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002129{
2130 struct gsm48_hdr *gh = msgb_l3(msg);
2131 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2132 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2133 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002134 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2135 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002136 int i, rc = 0;
2137
2138 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002139 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002140 return -EINVAL;
2141 }
2142
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002143 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002144 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002145 return -EINVAL;
2146 }
2147
2148 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002149 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002150
Harald Welte27989d42018-06-21 20:39:20 +02002151 /* Create transaction */
2152 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002153 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002154 trans = trans_alloc(net, vsub,
2155 TRANS_CC,
2156 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002157 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002158 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002159 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002160 GSM48_PDISC_CC | (transaction_id << 4),
2161 GSM48_MT_CC_RELEASE_COMPL);
2162 return -ENOMEM;
2163 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002164 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2165 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2166 trans_free(trans);
2167 return -EINVAL;
2168 }
2169
Harald Welte27989d42018-06-21 20:39:20 +02002170 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002171 msc_a_get(msc_a, MSC_A_USE_CC);
2172 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002173 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002174
2175 /* An earlier CM Service Request for this CC message now has concluded */
2176 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2177 LOG_MSC_A(msc_a, LOGL_ERROR,
2178 "Creating new CC transaction without prior CM Service Request\n");
2179 else
2180 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002181 }
2182
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002183 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2184 gsm48_cc_state_name(trans->cc.state));
2185
Harald Welte27989d42018-06-21 20:39:20 +02002186 /* find function for current state and message */
2187 for (i = 0; i < DATASLLEN; i++)
2188 if ((msg_type == datastatelist[i].type)
2189 && ((1 << trans->cc.state) & datastatelist[i].states))
2190 break;
2191 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002192 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002193
2194 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2195 * transaction right away. */
2196 if (trans->cc.state == GSM_CSTATE_NULL) {
2197 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2198 " -- disarding new CC transaction right away\n");
2199 trans_free(trans);
2200 }
Harald Welte27989d42018-06-21 20:39:20 +02002201 return 0;
2202 }
2203
2204 assert(trans->vsub);
2205
2206 rc = datastatelist[i].rout(trans, msg);
2207
Harald Welte27989d42018-06-21 20:39:20 +02002208 return rc;
2209}