blob: 3e9ceee6e09ad8fafb31c6373c04288c5d70a1c3 [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>
Neels Hofmeyr9a539f32022-01-13 21:39:11 +010058#include <osmocom/msc/codec_sdp_cc_t9n.h>
Harald Welte27989d42018-06-21 20:39:20 +020059
60#include <osmocom/gsm/gsm48.h>
61#include <osmocom/gsm/gsm0480.h>
62#include <osmocom/gsm/gsm_utils.h>
63#include <osmocom/gsm/protocol/gsm_04_08.h>
64#include <osmocom/core/msgb.h>
65#include <osmocom/core/talloc.h>
66#include <osmocom/core/utils.h>
67#include <osmocom/core/byteswap.h>
68#include <osmocom/gsm/tlv.h>
69#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020070
71#include <assert.h>
72
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010073static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
75static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
76
77static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
78{
79 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
80 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
81 OMSC_LINKID_CB(msg) = trans->dlci;
82
83 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
84}
85
86uint32_t msc_cc_next_outgoing_callref() {
87 static uint32_t last_callref = 0x80000000;
88 last_callref++;
89 if (last_callref < 0x80000001)
90 last_callref = 0x80000001;
91 return last_callref;
92}
Harald Welte27989d42018-06-21 20:39:20 +020093
Philipp Maier9ca7b312018-10-10 17:00:49 +020094static void gsm48_cc_guard_timeout(void *arg)
95{
96 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010097 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020098 trans_free(trans);
99 return;
100}
101
102static void gsm48_stop_guard_timer(struct gsm_trans *trans)
103{
104 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100105 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200106 osmo_timer_del(&trans->cc.timer_guard);
107 }
108}
109
110static void gsm48_start_guard_timer(struct gsm_trans *trans)
111{
112 /* NOTE: The purpose of this timer is to prevent the cc state machine
113 * from hanging in cases where mncc, gsm48 or both become unresponsive
114 * for some reason. The timer is started initially with the setup from
115 * the gsm48 side and then re-started with every incoming mncc message.
116 * Once the mncc state reaches its active state the timer is stopped.
117 * So if the cc state machine does not show any activity for an
118 * extended amount of time during call setup or teardown the guard
119 * timer will time out and hard-clear the connection. */
120 if (osmo_timer_pending(&trans->cc.timer_guard))
121 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100122 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200123 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
124 osmo_timer_schedule(&trans->cc.timer_guard,
125 trans->net->mncc_guard_timeout, 0);
126}
Harald Welte27989d42018-06-21 20:39:20 +0200127
128/* Call Control */
129
130void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
131{
132 net->mncc_recv(net, msg);
133}
134
135int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
136{
137 struct gsm48_hdr *gh;
138 struct msgb *ss_notify;
139
140 ss_notify = gsm0480_create_notifySS(message);
141 if (!ss_notify)
142 return -1;
143
144 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
145 uint8_t *data = msgb_push(ss_notify, 1);
146 data[0] = ss_notify->len - 1;
147 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
148 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100149 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200150}
151
152/* FIXME: this count_statistics is a state machine behaviour. we should convert
153 * the complete call control into a state machine. Afterwards we can move this
154 * code into state transitions.
155 */
156static void count_statistics(struct gsm_trans *trans, int new_state)
157{
158 int old_state = trans->cc.state;
159 struct rate_ctr_group *msc = trans->net->msc_ctrs;
160
161 if (old_state == new_state)
162 return;
163
164 /* state incoming */
165 switch (new_state) {
166 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200167 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
168 1);
169 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200170 break;
171 }
172
173 /* state outgoing */
174 switch (old_state) {
175 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200176 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
177 1);
Harald Welte27989d42018-06-21 20:39:20 +0200178 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
179 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200180 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200181 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200182 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200183 break;
184 }
185}
186
Keith Whyte24e5dc62022-10-15 01:32:02 +0100187void update_lcls(struct gsm_trans *trans) {
188 struct ran_msg msg;
189
190 if (!trans->cc.lcls)
191 return;
192 msg = (struct ran_msg){
193 .msg_type = RAN_MSG_LCLS_CONNECT_CTRL,
194 .lcls_config_ctrl.config = trans->cc.lcls->config,
195 .lcls_config_ctrl.control = trans->cc.lcls->control,
196 };
197 msc_a_ran_down(trans->msc_a, MSC_ROLE_I, &msg);
198}
199
Harald Welte27989d42018-06-21 20:39:20 +0200200static void new_cc_state(struct gsm_trans *trans, int state)
201{
202 if (state > 31 || state < 0)
203 return;
204
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100205 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
206 gsm48_cc_state_name(trans->cc.state),
207 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200208
209 count_statistics(trans, state);
210 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200211
212 /* Stop the guard timer when a call reaches the active state */
213 if (state == GSM_CSTATE_ACTIVE)
214 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200215}
216
217static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
218{
219 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
220 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
221 uint8_t *cause, *call_state;
222
223 gh->msg_type = GSM48_MT_CC_STATUS;
224
225 cause = msgb_put(msg, 3);
226 cause[0] = 2;
227 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
228 cause[2] = 0x80 | 30; /* response to status inquiry */
229
230 call_state = msgb_put(msg, 1);
231 call_state[0] = 0xc0 | 0x00;
232
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100233 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200234}
235
236static void gsm48_stop_cc_timer(struct gsm_trans *trans)
237{
238 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100239 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200240 osmo_timer_del(&trans->cc.timer);
241 trans->cc.Tcurrent = 0;
242 }
243}
244
Neels Hofmeyrad19b452022-08-07 02:43:15 +0200245#define mncc_recvmsg(net, trans, msg_type, mncc) \
246 _mncc_recvmsg(__FILE__, __LINE__, net, trans, msg_type, mncc)
247static int _mncc_recvmsg(const char *file, int line, struct gsm_network *net, struct gsm_trans *trans,
Harald Welte27989d42018-06-21 20:39:20 +0200248 int msg_type, struct gsm_mncc *mncc)
249{
250 struct msgb *msg;
251 unsigned char *data;
252
Neels Hofmeyre3b99c92022-01-14 03:04:56 +0100253 switch (msg_type) {
254 case MNCC_DISC_IND:
255 case MNCC_REL_IND:
256 if (trans) {
257 if (trans->cc.mncc_release_sent) {
258 LOG_TRANS_CAT_SRC(trans, DMNCC, file, line, LOGL_DEBUG,
259 "Already released, skipping tx %s\n", get_mncc_name(msg_type));
260 return 0;
261 }
262 trans->cc.mncc_release_sent = true;
263 }
264 break;
265 default:
266 break;
267 }
268
Neels Hofmeyrad19b452022-08-07 02:43:15 +0200269 LOG_TRANS_CAT_SRC(trans, DMNCC, file, line, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200270
271 mncc->msg_type = msg_type;
272
273 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
274 if (!msg)
275 return -ENOMEM;
276
277 data = msgb_put(msg, sizeof(struct gsm_mncc));
278 memcpy(data, mncc, sizeof(struct gsm_mncc));
279
280 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200281 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
282 if (trans)
283 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200284
285 return 0;
286}
287
288int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
289 uint32_t callref, int location, int value)
290{
291 struct gsm_mncc rel;
292
293 memset(&rel, 0, sizeof(rel));
294 rel.callref = callref;
295 mncc_set_cause(&rel, location, value);
296 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
297 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
298 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
299}
300
301/* Call Control Specific transaction release.
302 * gets called by trans_free, DO NOT CALL YOURSELF! */
303void _gsm48_cc_trans_free(struct gsm_trans *trans)
304{
305 gsm48_stop_cc_timer(trans);
306
Harald Welte27989d42018-06-21 20:39:20 +0200307 /* send release to L4, if callref still exists */
308 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100309 /* Send MNCC REL.ind (cause='Resource unavailable') */
310 if (trans->cc.mncc_initiated) {
311 mncc_release_ind(trans->net, trans, trans->callref,
312 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100313 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
314 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100315 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
316 }
317
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100318 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
319 * CC Release to the MS if it gets freed here. Hack it to do so. */
320 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
321 struct gsm_mncc rel = {};
322 rel.callref = trans->callref;
323 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
324 gsm48_cc_tx_release(trans, &rel);
325 }
Harald Welte27989d42018-06-21 20:39:20 +0200326 /* This is a final freeing of the transaction. The MNCC release may have triggered the
327 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
328 gsm48_stop_cc_timer(trans);
329 }
330 if (trans->cc.state != GSM_CSTATE_NULL)
331 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200332
333 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100334
335 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
336 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200337}
338
Harald Welte27989d42018-06-21 20:39:20 +0200339/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100340static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200341{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100342 if (trans->msc_a) {
343 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
344 "Handle paging error: transaction already associated with subscriber,"
345 " apparently it was already handled. Skip.\n");
346 return;
Harald Welte27989d42018-06-21 20:39:20 +0200347 }
348
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100349 if (msc_a) {
350 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
351 /* Assign conn */
352 msc_a_get(msc_a, MSC_A_USE_CC);
353 trans->msc_a = msc_a;
354 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200355
Keith Whytea1a70be2021-05-16 02:59:52 +0200356
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100357 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
358 /* send SETUP request to called party */
359 gsm48_cc_tx_setup(trans, &trans->cc.msg);
360 } else {
361 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
362 /* Temporarily out of order */
363 mncc_release_ind(trans->net, trans,
364 trans->callref,
365 GSM48_CAUSE_LOC_PRN_S_LU,
366 GSM48_CC_CAUSE_DEST_OOO);
367 trans->callref = 0;
368 trans->paging_request = NULL;
369 trans_free(trans);
370 }
Harald Welte27989d42018-06-21 20:39:20 +0200371}
372
373/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100374static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200375{
376 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
377 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100378 struct call_leg *cl1;
379 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200380
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100381 if (!trans1 || !trans2) {
382 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200383 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100384 }
Harald Welte27989d42018-06-21 20:39:20 +0200385
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100386 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100387 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
388 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 +0200389 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100390 }
391
392 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
393 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200394
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100395 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
396 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
397 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
398 * If we can't, then we must give up. */
399 cl1 = trans1->msc_a->cc.call_leg;
400 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200401
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100402 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200403}
404
405static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
406{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100407 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200408 return gsm48_cc_tx_status(trans, msg);
409}
410
Harald Welte27989d42018-06-21 20:39:20 +0200411static void gsm48_cc_timeout(void *arg)
412{
413 struct gsm_trans *trans = arg;
414 int disconnect = 0, release = 0;
415 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
416 int mo_location = GSM48_CAUSE_LOC_USER;
417 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
418 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
419 struct gsm_mncc mo_rel, l4_rel;
420
Neels Hofmeyrac7d9a62022-08-06 14:16:55 +0200421 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
422
Harald Welte27989d42018-06-21 20:39:20 +0200423 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
424 mo_rel.callref = trans->callref;
425 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
426 l4_rel.callref = trans->callref;
427
428 switch(trans->cc.Tcurrent) {
429 case 0x303:
430 release = 1;
431 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
432 break;
433 case 0x310:
434 disconnect = 1;
435 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
436 break;
437 case 0x313:
438 disconnect = 1;
439 /* unknown, did not find it in the specs */
440 break;
441 case 0x301:
442 disconnect = 1;
443 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
444 break;
445 case 0x308:
446 if (!trans->cc.T308_second) {
447 /* restart T308 a second time */
448 gsm48_cc_tx_release(trans, &trans->cc.msg);
449 trans->cc.T308_second = 1;
450 break; /* stay in release state */
451 }
452 trans_free(trans);
453 return;
454 case 0x306:
455 release = 1;
456 mo_cause = trans->cc.msg.cause.value;
457 mo_location = trans->cc.msg.cause.location;
458 break;
459 case 0x323:
460 disconnect = 1;
461 break;
462 default:
463 release = 1;
464 }
465
466 if (release && trans->callref) {
467 /* process release towards layer 4 */
468 mncc_release_ind(trans->net, trans, trans->callref,
469 l4_location, l4_cause);
470 trans->callref = 0;
471 }
472
473 if (disconnect && trans->callref) {
474 /* process disconnect towards layer 4 */
475 mncc_set_cause(&l4_rel, l4_location, l4_cause);
476 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
477 }
478
479 /* process disconnect towards mobile station */
480 if (disconnect || release) {
481 mncc_set_cause(&mo_rel, mo_location, mo_cause);
482 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
483 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
484 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
485 mo_rel.cause.diag_len = 3;
486
487 if (disconnect)
488 gsm48_cc_tx_disconnect(trans, &mo_rel);
489 if (release)
490 gsm48_cc_tx_release(trans, &mo_rel);
491 }
492
493}
494
495/* disconnect both calls from the bridge */
496static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100497 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200498{
499 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
500 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
501 struct gsm_mncc mx_rel;
502 if (!trans0 || !trans1)
503 return;
504
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100505 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
506 trans0->callref, trans1->callref, strerror(err));
507 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200508 trans0->callref, trans1->callref, strerror(err));
509
510 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
511 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
512 GSM48_CC_CAUSE_CHAN_UNACCEPT);
513
514 mx_rel.callref = trans0->callref;
515 gsm48_cc_tx_disconnect(trans0, &mx_rel);
516
517 mx_rel.callref = trans1->callref;
518 gsm48_cc_tx_disconnect(trans1, &mx_rel);
519}
520
521static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
522 int sec, int micro)
523{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100524 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200525 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
526 osmo_timer_schedule(&trans->cc.timer, sec, micro);
527 trans->cc.Tcurrent = current;
528}
529
530static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
531{
532 struct gsm48_hdr *gh = msgb_l3(msg);
533 uint8_t msg_type = gsm48_hdr_msg_type(gh);
534 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
535 struct tlv_parsed tp;
536 struct gsm_mncc setup;
537
Philipp Maier9ca7b312018-10-10 17:00:49 +0200538 gsm48_start_guard_timer(trans);
539
Harald Welte27989d42018-06-21 20:39:20 +0200540 memset(&setup, 0, sizeof(struct gsm_mncc));
541 setup.callref = trans->callref;
542
Keith Whytea1a70be2021-05-16 02:59:52 +0200543 /* New Global Call Reference */
544 if (!trans->cc.lcls)
545 trans->cc.lcls = trans_lcls_compose(trans, true);
546
547 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300548 if (trans->cc.lcls) {
549 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
550 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
551 int rc;
552
553 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
554 memcpy(&setup.gcr[0], gcr_msg->data, rc);
555 setup.fields |= MNCC_F_GCR;
556 } else
557 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
558 msgb_free(gcr_msg);
559 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200560
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100561 OSMO_ASSERT(trans->msc_a);
562
Harald Welte27989d42018-06-21 20:39:20 +0200563 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
564 /* emergency setup is identified by msg_type */
565 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
566 setup.fields |= MNCC_F_EMERGENCY;
567 setup.emergency = 1;
568 /* use destination number as configured by user (if any) */
569 if (trans->net->emergency.route_to_msisdn) {
570 setup.fields |= MNCC_F_CALLED;
571 setup.called.type = 0; /* unknown */
572 setup.called.plan = 0; /* unknown */
573 OSMO_STRLCPY_ARRAY(setup.called.number,
574 trans->net->emergency.route_to_msisdn);
575 }
576 }
577
578 /* use subscriber as calling party number */
579 setup.fields |= MNCC_F_CALLING;
580 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
581 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
582
583 /* bearer capability */
584 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
585 setup.fields |= MNCC_F_BEARER_CAP;
586 gsm48_decode_bearer_cap(&setup.bearer_cap,
587 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
588
589 /* Create a copy of the bearer capability
590 * in the transaction struct, so we can use
591 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100592 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200593 sizeof(trans->bearer_cap));
594 }
595 /* facility */
596 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
597 setup.fields |= MNCC_F_FACILITY;
598 gsm48_decode_facility(&setup.facility,
599 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
600 }
601 /* called party bcd number */
602 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
603 setup.fields |= MNCC_F_CALLED;
604 gsm48_decode_called(&setup.called,
605 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
606 }
607 /* user-user */
608 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
609 setup.fields |= MNCC_F_USERUSER;
610 gsm48_decode_useruser(&setup.useruser,
611 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
612 }
613 /* ss-version */
614 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
615 setup.fields |= MNCC_F_SSVERSION;
616 gsm48_decode_ssversion(&setup.ssversion,
617 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
618 }
619 /* CLIR suppression */
620 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
621 setup.clir.sup = 1;
622 /* CLIR invocation */
623 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
624 setup.clir.inv = 1;
625 /* cc cap */
626 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
627 setup.fields |= MNCC_F_CCCAP;
628 gsm48_decode_cccap(&setup.cccap,
629 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
630 }
631
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100632 /* MO call leg starting, gather all codec information so far known: */
633 codec_filter_init(&trans->cc.codecs);
634 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
635 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
636 if (setup.fields & MNCC_F_BEARER_CAP)
637 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
638 codec_filter_run(&trans->cc.codecs);
639
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100640 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
641 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100642 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200643
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200644 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200645
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100646 new_cc_state(trans, GSM_CSTATE_INITIATED);
647
648 /* To complete the MNCC_SETUP_IND, we need to provide an RTP address and port. First instruct the MGW to create
649 * a CN-side RTP conn, and continue with MNCC_SETUP_IND once that is done. Leave trans.cc in GSM_CSTATE_NULL and
650 * note down the msg_type to indicate that we indeed composed an MNCC_SETUP_IND for later. */
651 setup.msg_type = MNCC_SETUP_IND;
652 trans->cc.msg = setup;
653 return msc_a_try_call_assignment(trans);
654 /* continue in gsm48_cc_rx_setup_cn_local_rtp_port_known() */
655}
656
657/* Callback for MNCC_SETUP_IND waiting for the core network RTP port to be established by the MGW (via msc_a) */
658void gsm48_cc_rx_setup_cn_local_rtp_port_known(struct gsm_trans *trans)
659{
660 struct msc_a *msc_a = trans->msc_a;
661 struct gsm_mncc setup = trans->cc.msg;
662 struct osmo_sockaddr_str *rtp_cn_local;
663 struct sdp_msg *sdp;
664 int rc;
665
666 if (trans->cc.state != GSM_CSTATE_INITIATED
667 || setup.msg_type != MNCC_SETUP_IND) {
668 LOG_TRANS(trans, LOGL_ERROR,
669 "Unexpected CC state. Expected GSM_CSTATE_NULL and a buffered MNCC_SETUP_IND message,"
670 " found CC state %d and msg_type %s\n",
671 trans->cc.state, get_mncc_name(setup.msg_type));
672 trans->callref = 0;
673 trans_free(trans);
674 return;
675 }
676
677 if (!msc_a) {
678 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
679 trans->callref = 0;
680 trans_free(trans);
681 return;
682 }
683
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100684
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100685 /* Insert the CN side RTP port now available into SDP and compose SDP string */
686 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
687 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
688 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100689 trans_free(trans);
690 return;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100691 }
692
693 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
694 codec_filter_run(&trans->cc.codecs);
695 sdp = trans->cc.codecs.result.audio_codecs.count ? &trans->cc.codecs.result : NULL;
696 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
697 if (rc >= sizeof(setup.sdp)) {
698 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
699 trans_free(trans);
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100700 return;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100701 }
702
Harald Welte27989d42018-06-21 20:39:20 +0200703 /* indicate setup to MNCC */
704 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte27989d42018-06-21 20:39:20 +0200705}
706
707static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
708{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100709 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200710 struct gsm48_hdr *gh;
711 struct gsm_mncc *setup = arg;
712 int rc, trans_id;
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100713 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200714
715 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
716
717 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700718 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100719 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200720 "This is not allowed!\n");
721 /* Temporarily out of order */
722 rc = mncc_release_ind(trans->net, trans, trans->callref,
723 GSM48_CAUSE_LOC_PRN_S_LU,
724 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
725 trans->callref = 0;
726 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200727 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200728 return rc;
729 }
730
731 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100732 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200733 if (trans_id < 0) {
734 /* no free transaction ID */
735 rc = mncc_release_ind(trans->net, trans, trans->callref,
736 GSM48_CAUSE_LOC_PRN_S_LU,
737 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
738 trans->callref = 0;
739 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200740 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200741 return rc;
742 }
743 trans->transaction_id = trans_id;
744
745 gh->msg_type = GSM48_MT_CC_SETUP;
746
Neels Hofmeyra4195db2022-01-13 21:40:58 +0100747 /* MT call leg is starting. Gather all codecs information so far known.
748 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
749 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
750 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
751 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
752 * Capabilities. */
753 codec_filter_init(&trans->cc.codecs);
754 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
755 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
756 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
757 if (setup->sdp[0]) {
758 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
759 if (rc)
760 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
761 }
762 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
763 * MNCC, if any. */
764 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
765 trans->cc.codecs.remote = (struct sdp_msg){};
766 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
767 &setup->bearer_cap);
768 }
769 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
770 if (!trans->cc.codecs.remote.audio_codecs.count)
771 LOG_TRANS(trans, LOGL_ERROR,
772 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
773
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100774 /* Compose outgoing Bearer Capabilities: translate SDP to bearer capability Speech Version entries.
775 * Send only codecs that remain according to the codec filter. */
Harald Welte27989d42018-06-21 20:39:20 +0200776 /* Create a copy of the bearer capability in the transaction struct, so we
777 * can use this information later */
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100778 codec_filter_run(&trans->cc.codecs);
779 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
780 bearer_cap = (struct gsm_mncc_bearer_cap){
781 .speech_ver = { -1 },
782 };
783 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.codecs.result.audio_codecs);
784 rc = bearer_cap_set_radio(&bearer_cap);
785 if (rc) {
786 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
787 trans_free(trans);
788 msgb_free(msg);
789 return rc;
Harald Welte27989d42018-06-21 20:39:20 +0200790 }
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100791 /* (As earlier code did, keep a copy in trans->bearer_cap) */
792 trans->bearer_cap = bearer_cap;
793 /* If no resulting codecs remain, error out. If the MGW were able to transcode, we would just use unidentical
794 * codecs on each conn of the MGW endpoint. */
795 if (bearer_cap.speech_ver[0] == -1) {
796 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
797 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
798
799 /* incompatible codecs */
800 rc = mncc_release_ind(trans->net, trans, trans->callref,
801 GSM48_CAUSE_LOC_PRN_S_LU,
802 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
803 trans_free(trans);
804 msgb_free(msg);
805 return rc;
806 }
807 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
808
Harald Welte27989d42018-06-21 20:39:20 +0200809 /* facility */
810 if (setup->fields & MNCC_F_FACILITY)
811 gsm48_encode_facility(msg, 0, &setup->facility);
812 /* progress */
813 if (setup->fields & MNCC_F_PROGRESS)
814 gsm48_encode_progress(msg, 0, &setup->progress);
815 /* calling party BCD number */
816 if (setup->fields & MNCC_F_CALLING)
817 gsm48_encode_calling(msg, &setup->calling);
818 /* called party BCD number */
819 if (setup->fields & MNCC_F_CALLED)
820 gsm48_encode_called(msg, &setup->called);
821 /* user-user */
822 if (setup->fields & MNCC_F_USERUSER)
823 gsm48_encode_useruser(msg, 0, &setup->useruser);
824 /* redirecting party BCD number */
825 if (setup->fields & MNCC_F_REDIRECTING)
826 gsm48_encode_redirecting(msg, &setup->redirecting);
827 /* signal */
828 if (setup->fields & MNCC_F_SIGNAL)
829 gsm48_encode_signal(msg, setup->signal);
830
831 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
832
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200833 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200834
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100835 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
836
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100837 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200838}
839
840static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
841{
842 struct gsm48_hdr *gh = msgb_l3(msg);
843 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
844 struct tlv_parsed tp;
845 struct gsm_mncc call_conf;
846 int rc;
847
848 gsm48_stop_cc_timer(trans);
849 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
850
851 memset(&call_conf, 0, sizeof(struct gsm_mncc));
852 call_conf.callref = trans->callref;
853
854 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
855#if 0
856 /* repeat */
857 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
858 call_conf.repeat = 1;
859 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
860 call_conf.repeat = 2;
861#endif
862 /* bearer capability */
863 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
864 call_conf.fields |= MNCC_F_BEARER_CAP;
865 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
866 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
867
868 /* Create a copy of the bearer capability
869 * in the transaction struct, so we can use
870 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100871 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200872 sizeof(trans->bearer_cap));
Neels Hofmeyrdb61f732022-01-13 19:59:02 +0100873
874 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
875 * Store in codecs filter. */
876 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200877 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100878
Harald Welte27989d42018-06-21 20:39:20 +0200879 /* cause */
880 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
881 call_conf.fields |= MNCC_F_CAUSE;
882 gsm48_decode_cause(&call_conf.cause,
883 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
884 }
885 /* cc cap */
886 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
887 call_conf.fields |= MNCC_F_CCCAP;
888 gsm48_decode_cccap(&call_conf.cccap,
889 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
890 }
891
892 /* IMSI of called subscriber */
893 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
894
Harald Welte27989d42018-06-21 20:39:20 +0200895 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100896 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200897
898 /* don't continue, if there were problems with
899 * the call assignment. */
900 if (rc)
901 return rc;
902
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100903 /* Directly ack with MNCC_CALL_CONF_IND, not yet containing SDP or RTP IP:port information. */
904 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
905 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND, &call_conf);
906}
907
908static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
909 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
910 uint32_t payload_msg_type, const struct sdp_msg *sdp);
911
Neels Hofmeyr201fae42022-08-09 02:14:42 +0200912static int gsm48_cc_mt_rtp_port_and_codec_known(struct gsm_trans *trans)
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +0100913{
914 struct msc_a *msc_a = trans->msc_a;
915 struct osmo_sockaddr_str *rtp_cn_local;
916 struct gsm_mncc_rtp;
917
918 if (!msc_a) {
919 LOG_TRANS(trans, LOGL_ERROR, "No connection for CC trans\n");
920 trans->callref = 0;
921 trans_free(trans);
922 return -EINVAL;
923 }
924
925 /* Insert the CN side RTP port now available into SDP */
926 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
927 if (!rtp_cn_local) {
928 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_RTP_CREATE: no RTP set up for the CN side\n");
929 trans_free(trans);
930 return -EINVAL;
931 }
932 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
933
934 codec_filter_run(&trans->cc.codecs);
935 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
936
937 if (!sdp_audio_codec_is_set(&trans->cc.codecs.assignment))
938 return 0;
939
940 return mncc_recv_rtp(msc_a_net(msc_a), trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, 0, 0,
941 &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +0200942}
943
944static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
945{
946 struct gsm_mncc *proceeding = arg;
947 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
948 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
949 int rc;
950
951 gh->msg_type = GSM48_MT_CC_CALL_PROC;
952
953 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
954
955 /* bearer capability */
956 if (proceeding->fields & MNCC_F_BEARER_CAP) {
957 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
958 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
959 }
960 /* facility */
961 if (proceeding->fields & MNCC_F_FACILITY)
962 gsm48_encode_facility(msg, 0, &proceeding->facility);
963 /* progress */
964 if (proceeding->fields & MNCC_F_PROGRESS)
965 gsm48_encode_progress(msg, 0, &proceeding->progress);
966
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100967 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200968 if (rc)
969 return rc;
970
971 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100972 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200973}
974
975static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
976{
977 struct gsm48_hdr *gh = msgb_l3(msg);
978 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
979 struct tlv_parsed tp;
980 struct gsm_mncc alerting;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100981 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200982
983 gsm48_stop_cc_timer(trans);
984 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
985
986 memset(&alerting, 0, sizeof(struct gsm_mncc));
987 alerting.callref = trans->callref;
988 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
989 /* facility */
990 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
991 alerting.fields |= MNCC_F_FACILITY;
992 gsm48_decode_facility(&alerting.facility,
993 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
994 }
995
996 /* progress */
997 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
998 alerting.fields |= MNCC_F_PROGRESS;
999 gsm48_decode_progress(&alerting.progress,
1000 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1001 }
1002 /* ss-version */
1003 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1004 alerting.fields |= MNCC_F_SSVERSION;
1005 gsm48_decode_ssversion(&alerting.ssversion,
1006 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1007 }
1008
1009 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1010
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001011 codec_filter_run(&trans->cc.codecs);
1012 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1013 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
1014 if (rc >= sizeof(alerting.sdp)) {
1015 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
1016 rc, sizeof(alerting.sdp));
1017 trans_free(trans);
1018 return -EINVAL;
1019 }
1020
Harald Welte27989d42018-06-21 20:39:20 +02001021 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
1022 &alerting);
1023}
1024
1025static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1026{
1027 struct gsm_mncc *alerting = arg;
1028 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1029 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1030
1031 gh->msg_type = GSM48_MT_CC_ALERTING;
1032
1033 /* facility */
1034 if (alerting->fields & MNCC_F_FACILITY)
1035 gsm48_encode_facility(msg, 0, &alerting->facility);
1036 /* progress */
1037 if (alerting->fields & MNCC_F_PROGRESS)
1038 gsm48_encode_progress(msg, 0, &alerting->progress);
1039 /* user-user */
1040 if (alerting->fields & MNCC_F_USERUSER)
1041 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1042
1043 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1044
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001045 if (alerting->sdp[0]) {
1046 struct call_leg *cl = trans->msc_a->cc.call_leg;
1047 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1048 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
1049 codec_filter_run(&trans->cc.codecs);
1050 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1051 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
1052 if (rtp_cn) {
1053 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1054 rtp_stream_commit(rtp_cn);
1055 }
1056 }
1057
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001058 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001059}
1060
1061static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1062{
1063 struct gsm_mncc *progress = arg;
1064 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1065 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1066
1067 gh->msg_type = GSM48_MT_CC_PROGRESS;
1068
1069 /* progress */
1070 gsm48_encode_progress(msg, 1, &progress->progress);
1071 /* user-user */
1072 if (progress->fields & MNCC_F_USERUSER)
1073 gsm48_encode_useruser(msg, 0, &progress->useruser);
1074
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001075 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001076}
1077
1078static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1079{
1080 struct gsm_mncc *connect = arg;
1081 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1082 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1083
1084 gh->msg_type = GSM48_MT_CC_CONNECT;
1085
1086 gsm48_stop_cc_timer(trans);
1087 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1088
1089 /* facility */
1090 if (connect->fields & MNCC_F_FACILITY)
1091 gsm48_encode_facility(msg, 0, &connect->facility);
1092 /* progress */
1093 if (connect->fields & MNCC_F_PROGRESS)
1094 gsm48_encode_progress(msg, 0, &connect->progress);
1095 /* connected number */
1096 if (connect->fields & MNCC_F_CONNECTED)
1097 gsm48_encode_connected(msg, &connect->connected);
1098 /* user-user */
1099 if (connect->fields & MNCC_F_USERUSER)
1100 gsm48_encode_useruser(msg, 0, &connect->useruser);
1101
1102 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1103
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001104 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
1105 if (connect->sdp[0]) {
1106 struct call_leg *cl = trans->msc_a->cc.call_leg;
1107 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1108 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, connect->sdp);
1109 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1110 get_mncc_name(connect->msg_type),
1111 codec_filter_to_str(&trans->cc.codecs));
1112 if (rtp_cn) {
1113 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1114 rtp_stream_commit(rtp_cn);
1115 }
1116 }
1117
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001118 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001119}
1120
1121static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1122{
1123 struct gsm48_hdr *gh = msgb_l3(msg);
1124 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1125 struct tlv_parsed tp;
1126 struct gsm_mncc connect;
1127
1128 gsm48_stop_cc_timer(trans);
1129
1130 memset(&connect, 0, sizeof(struct gsm_mncc));
1131 connect.callref = trans->callref;
1132 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1133 /* use subscriber as connected party number */
1134 connect.fields |= MNCC_F_CONNECTED;
1135 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1136 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1137
1138 /* facility */
1139 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1140 connect.fields |= MNCC_F_FACILITY;
1141 gsm48_decode_facility(&connect.facility,
1142 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1143 }
1144 /* user-user */
1145 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1146 connect.fields |= MNCC_F_USERUSER;
1147 gsm48_decode_useruser(&connect.useruser,
1148 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1149 }
1150 /* ss-version */
1151 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1152 connect.fields |= MNCC_F_SSVERSION;
1153 gsm48_decode_ssversion(&connect.ssversion,
1154 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1155 }
1156
1157 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001158 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001159
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001160 codec_filter_run(&trans->cc.codecs);
1161 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001162 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1163}
1164
1165
1166static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1167{
1168 struct gsm_mncc connect_ack;
1169
1170 gsm48_stop_cc_timer(trans);
1171
1172 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001173 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_CONNECT_ACK));
Harald Welte27989d42018-06-21 20:39:20 +02001174
1175 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1176 connect_ack.callref = trans->callref;
1177
1178 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1179 &connect_ack);
1180}
1181
1182static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1183{
1184 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1185 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1186
1187 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1188
1189 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1190
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001191 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001192}
1193
1194static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1195{
1196 struct gsm48_hdr *gh = msgb_l3(msg);
1197 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1198 struct tlv_parsed tp;
1199 struct gsm_mncc disc;
1200
1201 gsm48_stop_cc_timer(trans);
1202
1203 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1204
1205 memset(&disc, 0, sizeof(struct gsm_mncc));
1206 disc.callref = trans->callref;
1207 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1208 /* cause */
1209 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1210 disc.fields |= MNCC_F_CAUSE;
1211 gsm48_decode_cause(&disc.cause,
1212 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1213 }
1214 /* facility */
1215 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1216 disc.fields |= MNCC_F_FACILITY;
1217 gsm48_decode_facility(&disc.facility,
1218 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1219 }
1220 /* user-user */
1221 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1222 disc.fields |= MNCC_F_USERUSER;
1223 gsm48_decode_useruser(&disc.useruser,
1224 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1225 }
1226 /* ss-version */
1227 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1228 disc.fields |= MNCC_F_SSVERSION;
1229 gsm48_decode_ssversion(&disc.ssversion,
1230 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1231 }
1232
1233 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001234}
1235
1236static struct gsm_mncc_cause default_cause = {
1237 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1238 .coding = 0,
1239 .rec = 0,
1240 .rec_val = 0,
1241 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1242 .diag_len = 0,
1243 .diag = { 0 },
1244};
1245
1246static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1247{
1248 struct gsm_mncc *disc = arg;
1249 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1250 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1251
1252 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1253
1254 gsm48_stop_cc_timer(trans);
1255 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1256
1257 /* cause */
1258 if (disc->fields & MNCC_F_CAUSE)
1259 gsm48_encode_cause(msg, 1, &disc->cause);
1260 else
1261 gsm48_encode_cause(msg, 1, &default_cause);
1262
1263 /* facility */
1264 if (disc->fields & MNCC_F_FACILITY)
1265 gsm48_encode_facility(msg, 0, &disc->facility);
1266 /* progress */
1267 if (disc->fields & MNCC_F_PROGRESS)
1268 gsm48_encode_progress(msg, 0, &disc->progress);
1269 /* user-user */
1270 if (disc->fields & MNCC_F_USERUSER)
1271 gsm48_encode_useruser(msg, 0, &disc->useruser);
1272
1273 /* store disconnect cause for T306 expiry */
1274 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1275
1276 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1277
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001278 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001279}
1280
1281static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1282{
1283 struct gsm48_hdr *gh = msgb_l3(msg);
1284 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1285 struct tlv_parsed tp;
1286 struct gsm_mncc rel;
1287 int rc;
1288
1289 gsm48_stop_cc_timer(trans);
1290
1291 memset(&rel, 0, sizeof(struct gsm_mncc));
1292 rel.callref = trans->callref;
1293 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1294 /* cause */
1295 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1296 rel.fields |= MNCC_F_CAUSE;
1297 gsm48_decode_cause(&rel.cause,
1298 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1299 }
1300 /* facility */
1301 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1302 rel.fields |= MNCC_F_FACILITY;
1303 gsm48_decode_facility(&rel.facility,
1304 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1305 }
1306 /* user-user */
1307 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1308 rel.fields |= MNCC_F_USERUSER;
1309 gsm48_decode_useruser(&rel.useruser,
1310 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1311 }
1312 /* ss-version */
1313 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1314 rel.fields |= MNCC_F_SSVERSION;
1315 gsm48_decode_ssversion(&rel.ssversion,
1316 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1317 }
1318
1319 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1320 /* release collision 5.4.5 */
1321 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1322 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001323 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001324 GSM48_PDISC_CC | (trans->transaction_id << 4),
1325 GSM48_MT_CC_RELEASE_COMPL);
1326 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1327 }
1328
1329 new_cc_state(trans, GSM_CSTATE_NULL);
1330
1331 trans->callref = 0;
1332 trans_free(trans);
1333
1334 return rc;
1335}
1336
1337static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1338{
1339 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001340 struct msgb *msg;
1341 struct gsm48_hdr *gh;
1342
1343 if (!trans->msc_a) {
1344 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1345 return -EINVAL;
1346 }
1347
1348 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1349 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001350
1351 gh->msg_type = GSM48_MT_CC_RELEASE;
1352
1353 gsm48_stop_cc_timer(trans);
1354 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1355
1356 /* cause */
1357 if (rel->fields & MNCC_F_CAUSE)
1358 gsm48_encode_cause(msg, 0, &rel->cause);
1359 /* facility */
1360 if (rel->fields & MNCC_F_FACILITY)
1361 gsm48_encode_facility(msg, 0, &rel->facility);
1362 /* user-user */
1363 if (rel->fields & MNCC_F_USERUSER)
1364 gsm48_encode_useruser(msg, 0, &rel->useruser);
1365
1366 trans->cc.T308_second = 0;
1367 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1368
1369 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1370 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
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_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1376{
1377 struct gsm48_hdr *gh = msgb_l3(msg);
1378 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1379 struct tlv_parsed tp;
1380 struct gsm_mncc rel;
1381 int rc = 0;
1382
1383 gsm48_stop_cc_timer(trans);
1384
1385 memset(&rel, 0, sizeof(struct gsm_mncc));
1386 rel.callref = trans->callref;
1387 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1388 /* cause */
1389 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1390 rel.fields |= MNCC_F_CAUSE;
1391 gsm48_decode_cause(&rel.cause,
1392 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1393 }
1394 /* facility */
1395 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1396 rel.fields |= MNCC_F_FACILITY;
1397 gsm48_decode_facility(&rel.facility,
1398 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1399 }
1400 /* user-user */
1401 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1402 rel.fields |= MNCC_F_USERUSER;
1403 gsm48_decode_useruser(&rel.useruser,
1404 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1405 }
1406 /* ss-version */
1407 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1408 rel.fields |= MNCC_F_SSVERSION;
1409 gsm48_decode_ssversion(&rel.ssversion,
1410 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1411 }
1412
1413 if (trans->callref) {
1414 switch (trans->cc.state) {
1415 case GSM_CSTATE_CALL_PRESENT:
1416 rc = mncc_recvmsg(trans->net, trans,
1417 MNCC_REJ_IND, &rel);
1418 break;
1419 case GSM_CSTATE_RELEASE_REQ:
1420 rc = mncc_recvmsg(trans->net, trans,
1421 MNCC_REL_CNF, &rel);
1422 break;
1423 default:
1424 rc = mncc_recvmsg(trans->net, trans,
1425 MNCC_REL_IND, &rel);
1426 }
1427 }
1428
1429 trans->callref = 0;
1430 trans_free(trans);
1431
1432 return rc;
1433}
1434
1435static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1436{
1437 struct gsm_mncc *rel = arg;
1438 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1439 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1440 int ret;
1441
1442 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1443
1444 trans->callref = 0;
1445
1446 gsm48_stop_cc_timer(trans);
1447
1448 /* cause */
1449 if (rel->fields & MNCC_F_CAUSE)
1450 gsm48_encode_cause(msg, 0, &rel->cause);
1451 /* facility */
1452 if (rel->fields & MNCC_F_FACILITY)
1453 gsm48_encode_facility(msg, 0, &rel->facility);
1454 /* user-user */
1455 if (rel->fields & MNCC_F_USERUSER)
1456 gsm48_encode_useruser(msg, 0, &rel->useruser);
1457
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001458 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001459
1460 trans_free(trans);
1461
1462 return ret;
1463}
1464
1465static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1466{
1467 struct gsm48_hdr *gh = msgb_l3(msg);
1468 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1469 struct tlv_parsed tp;
1470 struct gsm_mncc fac;
1471
1472 memset(&fac, 0, sizeof(struct gsm_mncc));
1473 fac.callref = trans->callref;
1474 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1475 /* facility */
1476 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1477 fac.fields |= MNCC_F_FACILITY;
1478 gsm48_decode_facility(&fac.facility,
1479 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1480 }
1481 /* ss-version */
1482 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1483 fac.fields |= MNCC_F_SSVERSION;
1484 gsm48_decode_ssversion(&fac.ssversion,
1485 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1486 }
1487
1488 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1489}
1490
1491static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1492{
1493 struct gsm_mncc *fac = arg;
1494 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1495 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1496
1497 gh->msg_type = GSM48_MT_CC_FACILITY;
1498
1499 /* facility */
1500 gsm48_encode_facility(msg, 1, &fac->facility);
1501
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001502 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001503}
1504
1505static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1506{
1507 struct gsm_mncc hold;
1508
1509 memset(&hold, 0, sizeof(struct gsm_mncc));
1510 hold.callref = trans->callref;
1511 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1512}
1513
1514static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1515{
1516 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1517 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1518
1519 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1520
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001521 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001522}
1523
1524static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1525{
1526 struct gsm_mncc *hold_rej = arg;
1527 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1528 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1529
1530 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1531
1532 /* cause */
1533 if (hold_rej->fields & MNCC_F_CAUSE)
1534 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1535 else
1536 gsm48_encode_cause(msg, 1, &default_cause);
1537
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001538 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001539}
1540
1541static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1542{
1543 struct gsm_mncc retrieve;
1544
1545 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1546 retrieve.callref = trans->callref;
1547 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1548 &retrieve);
1549}
1550
1551static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1552{
1553 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1554 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1555
1556 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1557
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001558 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001559}
1560
1561static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1562{
1563 struct gsm_mncc *retrieve_rej = arg;
1564 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1565 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1566
1567 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1568
1569 /* cause */
1570 if (retrieve_rej->fields & MNCC_F_CAUSE)
1571 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1572 else
1573 gsm48_encode_cause(msg, 1, &default_cause);
1574
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001575 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001576}
1577
1578static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1579{
1580 struct gsm48_hdr *gh = msgb_l3(msg);
1581 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1582 struct tlv_parsed tp;
1583 struct gsm_mncc dtmf;
1584
1585 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1586 dtmf.callref = trans->callref;
1587 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1588 /* keypad facility */
1589 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1590 dtmf.fields |= MNCC_F_KEYPAD;
1591 gsm48_decode_keypad(&dtmf.keypad,
1592 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1593 }
1594
1595 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1596}
1597
1598static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1599{
1600 struct gsm_mncc *dtmf = arg;
1601 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1602 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1603
1604 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1605
1606 /* keypad */
1607 if (dtmf->fields & MNCC_F_KEYPAD)
1608 gsm48_encode_keypad(msg, dtmf->keypad);
1609
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001610 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001611}
1612
1613static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1614{
1615 struct gsm_mncc *dtmf = arg;
1616 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1618
1619 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1620
1621 /* cause */
1622 if (dtmf->fields & MNCC_F_CAUSE)
1623 gsm48_encode_cause(msg, 1, &dtmf->cause);
1624 else
1625 gsm48_encode_cause(msg, 1, &default_cause);
1626
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001627 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001628}
1629
1630static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1631{
1632 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1633 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1634
1635 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1636
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001637 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001638}
1639
1640static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1641{
1642 struct gsm_mncc dtmf;
1643
1644 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1645 dtmf.callref = trans->callref;
1646
1647 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1648}
1649
1650static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1651{
1652 struct gsm48_hdr *gh = msgb_l3(msg);
1653 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1654 struct tlv_parsed tp;
1655 struct gsm_mncc modify;
1656
1657 memset(&modify, 0, sizeof(struct gsm_mncc));
1658 modify.callref = trans->callref;
1659 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1660 /* bearer capability */
1661 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1662 modify.fields |= MNCC_F_BEARER_CAP;
1663 gsm48_decode_bearer_cap(&modify.bearer_cap,
1664 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1665
1666 /* Create a copy of the bearer capability
1667 * in the transaction struct, so we can use
1668 * this information later */
1669 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1670 sizeof(trans->bearer_cap));
1671 }
1672
1673 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1674
1675 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1676}
1677
1678static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1679{
1680 struct gsm_mncc *modify = arg;
1681 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1682 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1683
1684 gh->msg_type = GSM48_MT_CC_MODIFY;
1685
1686 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1687
1688 /* bearer capability */
1689 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1690 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1691
1692 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1693
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001694 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001695}
1696
1697static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1698{
1699 struct gsm48_hdr *gh = msgb_l3(msg);
1700 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1701 struct tlv_parsed tp;
1702 struct gsm_mncc modify;
1703
1704 gsm48_stop_cc_timer(trans);
1705
1706 memset(&modify, 0, sizeof(struct gsm_mncc));
1707 modify.callref = trans->callref;
1708 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1709 /* bearer capability */
1710 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1711 modify.fields |= MNCC_F_BEARER_CAP;
1712 gsm48_decode_bearer_cap(&modify.bearer_cap,
1713 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1714
1715 /* Create a copy of the bearer capability
1716 * in the transaction struct, so we can use
1717 * this information later */
1718 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1719 sizeof(trans->bearer_cap));
1720 }
1721
1722 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1723
1724 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1725}
1726
1727static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1728{
1729 struct gsm_mncc *modify = arg;
1730 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1732
1733 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1734
1735 /* bearer capability */
1736 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1737 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1738
1739 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1740
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001741 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001742}
1743
1744static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1745{
1746 struct gsm48_hdr *gh = msgb_l3(msg);
1747 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1748 struct tlv_parsed tp;
1749 struct gsm_mncc modify;
1750
1751 gsm48_stop_cc_timer(trans);
1752
1753 memset(&modify, 0, sizeof(struct gsm_mncc));
1754 modify.callref = trans->callref;
1755 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1756 /* bearer capability */
1757 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1758 modify.fields |= GSM48_IE_BEARER_CAP;
1759 gsm48_decode_bearer_cap(&modify.bearer_cap,
1760 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1761
1762 /* Create a copy of the bearer capability
1763 * in the transaction struct, so we can use
1764 * this information later */
1765 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1766 sizeof(trans->bearer_cap));
1767 }
1768 /* cause */
1769 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1770 modify.fields |= MNCC_F_CAUSE;
1771 gsm48_decode_cause(&modify.cause,
1772 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1773 }
1774
1775 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1776
1777 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1778}
1779
1780static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1781{
1782 struct gsm_mncc *modify = arg;
1783 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1784 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1785
1786 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1787
1788 /* bearer capability */
1789 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1790 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1791 /* cause */
1792 gsm48_encode_cause(msg, 1, &modify->cause);
1793
1794 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1795
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001796 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001797}
1798
1799static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1800{
1801 struct gsm_mncc *notify = arg;
1802 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1803 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1804
1805 gh->msg_type = GSM48_MT_CC_NOTIFY;
1806
1807 /* notify */
1808 gsm48_encode_notify(msg, notify->notify);
1809
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001810 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001811}
1812
1813static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1814{
1815 struct gsm48_hdr *gh = msgb_l3(msg);
1816 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1817// struct tlv_parsed tp;
1818 struct gsm_mncc notify;
1819
1820 memset(&notify, 0, sizeof(struct gsm_mncc));
1821 notify.callref = trans->callref;
1822// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1823 if (payload_len >= 1)
1824 gsm48_decode_notify(&notify.notify, gh->data);
1825
1826 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1827}
1828
1829static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1830{
1831 struct gsm_mncc *user = arg;
1832 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1833 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1834
1835 gh->msg_type = GSM48_MT_CC_USER_INFO;
1836
1837 /* user-user */
1838 if (user->fields & MNCC_F_USERUSER)
1839 gsm48_encode_useruser(msg, 1, &user->useruser);
1840 /* more data */
1841 if (user->more)
1842 gsm48_encode_more(msg);
1843
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001844 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001845}
1846
1847static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1848{
1849 struct gsm48_hdr *gh = msgb_l3(msg);
1850 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1851 struct tlv_parsed tp;
1852 struct gsm_mncc user;
1853
1854 memset(&user, 0, sizeof(struct gsm_mncc));
1855 user.callref = trans->callref;
1856 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1857 /* user-user */
1858 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1859 user.fields |= MNCC_F_USERUSER;
1860 gsm48_decode_useruser(&user.useruser,
1861 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1862 }
1863 /* more data */
1864 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1865 user.more = 1;
1866
1867 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1868}
1869
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001870static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1871 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001872 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001873{
1874 uint8_t data[sizeof(struct gsm_mncc)];
1875 struct gsm_mncc_rtp *rtp;
1876
1877 memset(&data, 0, sizeof(data));
1878 rtp = (struct gsm_mncc_rtp *) &data[0];
1879
1880 rtp->callref = callref;
1881 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001882 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001883 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1884 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001885 }
Harald Welte27989d42018-06-21 20:39:20 +02001886 rtp->payload_type = payload_type;
1887 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001888 if (sdp) {
1889 LOG_TRANS(trans, LOGL_DEBUG, "%s SDP: %s\n", get_mncc_name(rtp->msg_type), sdp_msg_to_str(sdp));
1890 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
1891 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001892 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001893}
1894
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001895static 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 +02001896{
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001897 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001898}
1899
1900static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1901{
1902 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001903
1904 /* Find callref */
1905 trans = trans_find_by_callref(net, callref);
1906 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001907 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001908 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001909 return -EIO;
1910 }
1911 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001912 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001913 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001914 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001915 return 0;
1916 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001917 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001918
Harald Welte27989d42018-06-21 20:39:20 +02001919 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001920 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001921}
1922
Neels Hofmeyrd338b1e2022-01-13 23:18:02 +01001923int cc_cn_local_rtp_port_known(struct gsm_trans *cc_trans)
1924{
1925 switch(cc_trans->cc.state) {
1926 case GSM_CSTATE_INITIATED:
1927 if (cc_trans->cc.msg.msg_type != MNCC_SETUP_IND) {
1928 LOG_TRANS(cc_trans, LOGL_ERROR, "Assuming MO call, expected MNCC_SETUP_IND to be prepared\n");
1929 return -EINVAL;
1930 }
1931 /* This is the MO call leg, waiting for a CN RTP be able to send initial MNCC_SETUP_IND. */
1932 gsm48_cc_rx_setup_cn_local_rtp_port_known(cc_trans);
1933 return 0;
1934
1935 case GSM_CSTATE_MO_TERM_CALL_CONF:
1936 /* This is the MT call leg, waiting for a CN RTP to be able to send MNCC_CALL_CONF_IND. */
1937 return gsm48_cc_mt_rtp_port_and_codec_known(cc_trans);
1938
1939 default:
1940 LOG_TRANS(cc_trans, LOGL_ERROR, "CN RTP address available, but in unexpected state %d\n",
1941 cc_trans->cc.state);
1942 return -EINVAL;
1943 }
1944}
1945
1946int cc_assignment_done(struct gsm_trans *trans)
1947{
1948 struct msc_a *msc_a = trans->msc_a;
1949
1950 switch (trans->cc.state) {
1951 case GSM_CSTATE_INITIATED:
1952 case GSM_CSTATE_MO_CALL_PROC:
1953 /* MO call */
1954 break;
1955
1956 case GSM_CSTATE_CALL_RECEIVED:
1957 case GSM_CSTATE_MO_TERM_CALL_CONF:
1958 /* MT call */
1959 break;
1960
1961 case GSM_CSTATE_ACTIVE:
1962 /* already active. MNCC finished before Abis completed the Assignment. */
1963 break;
1964
1965 default:
1966 LOG_TRANS(trans, LOGL_ERROR, "Assignment done in unexpected CC state: %d\n", trans->cc.state);
1967 return -EINVAL;
1968 }
1969
1970 if (!call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN)) {
1971 LOG_TRANS(trans, LOGL_DEBUG,
1972 "Assignment complete, but still waiting for the CRCX OK on the CN side RTP\n");
1973 return 0;
1974 }
1975 return gsm48_tch_rtp_create(trans);
1976}
1977
Harald Welte27989d42018-06-21 20:39:20 +02001978/* Trigger TCH_RTP_CREATE acknowledgement */
1979int gsm48_tch_rtp_create(struct gsm_trans *trans)
1980{
1981 /* This function is called as soon as the port, on which the
1982 * mgcp-gw expects the incoming RTP stream from the remote
1983 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001984 struct msc_a *msc_a = trans->msc_a;
1985 struct gsm_network *net = msc_a_net(msc_a);
1986 struct call_leg *cl = msc_a->cc.call_leg;
1987 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001988 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001989 int mncc_payload_msg_type;
1990 struct sdp_audio_codec *codec;
1991 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001992
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001993 if (!rtp_cn) {
1994 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1995 return -EINVAL;
1996 }
1997
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001998 codec_filter_run(&trans->cc.codecs);
1999 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
2000
2001 if (!trans->cc.codecs.result.audio_codecs.count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002002 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002003 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002004 return -EINVAL;
2005 }
2006
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002007 /* Modify the MGW endpoint if necessary, usually this should already match and not cause MGCP. */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02002008 rtp_stream_set_codecs(rtp_cn, &trans->cc.codecs.result.audio_codecs);
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002009 rtp_stream_commit(rtp_cn);
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02002010
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002011 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02002012 codec = &rtp_cn->codecs.codec[0];
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002013 m = codec_mapping_by_subtype_name(codec->subtype_name);
2014 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02002015
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002016 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
2017 if (!rtp_cn_local) {
2018 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
2019 return -EINVAL;
2020 }
2021
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01002022 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002023 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02002024}
2025
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002026static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02002027{
2028 struct gsm_trans *trans;
Keith Whyte24e5dc62022-10-15 01:32:02 +01002029 struct gsm_trans *trans_other;
2030 struct call_leg *cl, *ol;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002031 struct rtp_stream *rtps;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002032 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02002033
Philipp Maier8ad3dac2018-08-07 13:00:14 +02002034 /* FIXME: in *rtp we should get the codec information of the remote
2035 * leg. We will have to populate trans->conn->rtp.codec_cn with a
2036 * meaningful value based on this information but unfortunately we
2037 * can't do that yet because the mncc API can not signal dynamic
2038 * payload types yet. This must be fixed first. Also there may be
2039 * additional members necessary in trans->conn->rtp because we
2040 * somehow need to deal with dynamic payload types that do not
2041 * comply to 3gpp's assumptions of payload type numbers on the A
2042 * interface. See also related tickets: OS#3399 and OS1683 */
2043
Harald Welte27989d42018-06-21 20:39:20 +02002044 /* Find callref */
2045 trans = trans_find_by_callref(net, rtp->callref);
2046 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002047 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002048 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02002049 return -EIO;
2050 }
2051 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002052 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002053 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002054 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002055 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02002056 }
2057
Neels Hofmeyr90933d42022-01-13 20:10:52 +01002058 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s %s:%u\n", get_mncc_name(rtp->msg_type),
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002059 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
2060 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02002061
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002062 cl = trans->msc_a->cc.call_leg;
2063 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
2064
2065 if (!rtps) {
2066 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
2067 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2068 return -EINVAL;
2069 }
2070
Keith Whyte24e5dc62022-10-15 01:32:02 +01002071 trans_other = trans_find_by_same_gcr(net, trans);
2072
2073 if (trans_other) {
2074 ol = trans_other->msc_a->cc.call_leg;
2075 }
2076
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002077 if (rtp->sdp[0]) {
2078 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, rtp->sdp);
2079 LOG_TRANS(trans, LOGL_DEBUG, "%s contained SDP %s\n",
2080 get_mncc_name(rtp->msg_type),
2081 sdp_msg_to_str(&trans->cc.codecs.remote));
Keith Whyte24e5dc62022-10-15 01:32:02 +01002082
2083 if(trans_other) {
2084 if (!strcmp(ol->rtp[RTP_TO_CN]->local.ip, trans->cc.codecs.remote.rtp.ip) &&
2085 ol->rtp[RTP_TO_CN]->local.port == trans->cc.codecs.remote.rtp.port)
2086 {
2087 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "MNCC Wants out of the Loop.\n");
2088 trans->cc.lcls->control = GSM0808_LCLS_CSC_CONNECT;
2089 update_lcls(trans);
2090 } else {
2091 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "MNCC Wants into the Loop.\n");
2092 trans->cc.lcls->control = GSM0808_LCLS_CSC_RELEASE_LCLS;
2093 update_lcls(trans);
2094 }
2095 }
2096
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02002097 }
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002098
2099 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
2100
2101 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
2102 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
2103 struct osmo_sockaddr_str rtp_addr;
2104 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
2105 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
2106 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
2107 return -EINVAL;
2108 }
2109 rtp_stream_set_remote_addr(rtps, &rtp_addr);
2110 }
2111
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002112 rtp_stream_commit(rtps);
2113 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02002114}
2115
2116static struct downstate {
2117 uint32_t states;
2118 int type;
2119 int (*rout) (struct gsm_trans *trans, void *arg);
2120} downstatelist[] = {
2121 /* mobile originating call establishment */
2122 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2123 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2124 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2125 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2126 {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 */
2127 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2128 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2129 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2130 /* mobile terminating call establishment */
2131 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2132 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2133 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2134 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2135 /* signalling during call */
2136 {SBIT(GSM_CSTATE_ACTIVE),
2137 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2138 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2139 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2140 {ALL_STATES,
2141 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2142 {ALL_STATES,
2143 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2144 {ALL_STATES,
2145 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2146 {SBIT(GSM_CSTATE_ACTIVE),
2147 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2148 {SBIT(GSM_CSTATE_ACTIVE),
2149 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2150 {SBIT(GSM_CSTATE_ACTIVE),
2151 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2152 {SBIT(GSM_CSTATE_ACTIVE),
2153 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2154 {SBIT(GSM_CSTATE_ACTIVE),
2155 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2156 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2157 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2158 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2159 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2160 {SBIT(GSM_CSTATE_ACTIVE),
2161 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2162 /* clearing */
2163 {SBIT(GSM_CSTATE_INITIATED),
2164 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2165 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2166 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2167 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2168 MNCC_REL_REQ, gsm48_cc_tx_release},
2169};
2170
2171#define DOWNSLLEN \
2172 (sizeof(downstatelist) / sizeof(struct downstate))
2173
2174
Philipp Maiercd64af72019-08-01 09:46:40 +02002175static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002176{
2177 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002178 struct msc_a *msc_a = NULL;
2179 struct gsm_trans *trans = NULL;
2180 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002181
Harald Welte27989d42018-06-21 20:39:20 +02002182 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002183 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002184 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002185 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002186 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002187 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002188 return rc;
2189 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002190 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02002191 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002192 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002193 case MNCC_RTP_FREE:
2194 /* unused right now */
2195 return -EIO;
2196
2197 case MNCC_FRAME_DROP:
2198 case MNCC_FRAME_RECV:
2199 case GSM_TCHF_FRAME:
2200 case GSM_TCHF_FRAME_EFR:
2201 case GSM_TCHH_FRAME:
2202 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002203 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002204 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002205 return -ENOTSUP;
2206 }
2207
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002208 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002209
2210 /* Find callref */
2211 trans = trans_find_by_callref(net, data->callref);
2212
2213 /* Callref unknown */
2214 if (!trans) {
2215 struct vlr_subscr *vsub;
2216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002217 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002218 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002219 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002220 /* Invalid call reference */
2221 return mncc_release_ind(net, NULL, data->callref,
2222 GSM48_CAUSE_LOC_PRN_S_LU,
2223 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2224 }
2225 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002226 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002227 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002228 /* Invalid number */
2229 return mncc_release_ind(net, NULL, data->callref,
2230 GSM48_CAUSE_LOC_PRN_S_LU,
2231 GSM48_CC_CAUSE_INV_NR_FORMAT);
2232 }
2233 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002234 if (data->called.number[0]) {
2235 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2236 if (!vsub)
2237 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002238 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002239 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002240 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002241 if (!vsub)
2242 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002243 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002244 }
2245 if (!vsub)
2246 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002247 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002248 /* update the subscriber we deal with */
2249 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2250
Harald Welte27989d42018-06-21 20:39:20 +02002251 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002252 if (!vsub->lu_complete) {
2253 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002254 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002255 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002256 /* Temporarily out of order */
2257 return mncc_release_ind(net, NULL, data->callref,
2258 GSM48_CAUSE_LOC_PRN_S_LU,
2259 GSM48_CC_CAUSE_DEST_OOO);
2260 }
Keith Whyte991bb422019-08-08 15:43:40 +02002261
2262 /* Find valid conn */
2263 msc_a = msc_a_for_vsub(vsub, true);
2264
2265 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2266 if (!net->call_waiting && msc_a) {
2267 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2268 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2269 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2270 "rx '%s' for subscriber %s with trans state (%s)"
2271 " rejecting with USER_BUSY\n",
2272 get_mncc_name(msg->msg_type), data->called.number,
2273 gsm48_cc_state_name(existing_cc_trans->cc.state));
2274 return mncc_release_ind(net, NULL, data->callref,
2275 GSM48_CAUSE_LOC_PRN_S_LU,
2276 GSM48_CC_CAUSE_USER_BUSY);
2277 }
2278 }
2279
Harald Welte27989d42018-06-21 20:39:20 +02002280 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002281 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002282 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002283 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002284 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002285 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002286 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002287 mncc_release_ind(net, NULL, data->callref,
2288 GSM48_CAUSE_LOC_PRN_S_LU,
2289 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2290 return -ENOMEM;
2291 }
2292
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002293 /* Remember remote SDP, if any */
2294 if (data->sdp[0]) {
2295 if (sdp_msg_from_sdp_str(&trans->cc.codecs.remote, data->sdp)) {
2296 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse incoming SDP: %s\n",
2297 osmo_quote_str(data->sdp, -1));
2298 vlr_subscr_put(vsub, __func__);
2299 mncc_release_ind(net, NULL, data->callref,
2300 GSM48_CAUSE_LOC_PRN_S_LU,
2301 GSM48_CC_CAUSE_NORMAL_UNSPEC);
2302 return -EINVAL;
2303 }
2304 }
2305
Harald Welte27989d42018-06-21 20:39:20 +02002306 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002307 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002308 /* This condition will return before the common logging of the received MNCC message below, so
2309 * log it now. */
2310 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002311
Harald Welte27989d42018-06-21 20:39:20 +02002312 /* store setup information until paging succeeds */
2313 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2314
Keith Whyte0dbbec32022-10-15 01:18:43 +01002315 /* Store the Global Call Reference in the transaction if MNCC sent it */
2316 if (data->fields & MNCC_F_GCR) {
2317 /* The trans has no msc_a yet so compose will return NULL
2318 // trans->cc.lcls = trans_lcls_compose(trans, true); */
2319 trans->cc.lcls = talloc_zero(trans, struct osmo_lcls);
2320 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
2321 &data->gcr[0],
2322 sizeof(data->gcr));
2323 if (rc < 0) {
2324 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Failed to parse GCR\n");
2325 } else {
2326 trans->cc.lcls->config = GSM0808_LCLS_CFG_BOTH_WAY;
2327 trans->cc.lcls->control = GSM0808_LCLS_CSC_DO_NOT_CONNECT;
2328 trans->cc.lcls->corr_needed = true;
2329 trans->cc.lcls->gcr_available = true;
2330 }
2331 }
2332
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002333 /* Request a channel. If Paging already started, paging_request_start() will append the new
2334 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002335 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2336 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002337 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002338 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002339 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002340 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002341 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002342 return 0;
2343 }
2344
2345 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002346 trans->msc_a = msc_a;
2347 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002348 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002349 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002350 } else {
2351 /* update the subscriber we deal with */
2352 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2353 }
2354
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002355 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002356
Philipp Maier9ca7b312018-10-10 17:00:49 +02002357 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002358 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002359
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002360 if (trans->msc_a)
2361 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002362
2363 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002364 if (!msc_a) {
2365 struct gsm_mncc rel = {
2366 .callref = data->callref,
2367 };
2368 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002369 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2370 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002371 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002372 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2373 else
2374 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2375 trans->callref = 0;
2376 trans_free(trans);
2377 return rc;
2378 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002379 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002380 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002381 }
2382
2383 /* Find function for current state and message */
2384 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002385 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002386 && ((1 << trans->cc.state) & downstatelist[i].states))
2387 break;
2388 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002389 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002390 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002391 return 0;
2392 }
2393
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002394 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002395
2396 return rc;
2397}
2398
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002399struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2400{
2401 uint32_t callref;
2402
2403 switch (msg->msg_type) {
2404 case MNCC_BRIDGE:
2405 callref = msg->bridge.callref[0];
2406 break;
2407 case MNCC_RTP_CREATE:
2408 case MNCC_RTP_CONNECT:
2409 callref = msg->rtp.callref;
2410 break;
2411
2412 case MNCC_RTP_FREE:
2413 case MNCC_FRAME_DROP:
2414 case MNCC_FRAME_RECV:
2415 case GSM_TCHF_FRAME:
2416 case GSM_TCHF_FRAME_EFR:
2417 case GSM_TCHH_FRAME:
2418 case GSM_TCH_FRAME_AMR:
2419 return NULL;
2420
2421 default:
2422 callref = msg->signal.callref;
2423 break;
2424 }
2425
2426 return mncc_call_find_by_callref(callref);
2427}
2428
2429/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002430int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002431{
2432 const union mncc_msg *msg = arg;
2433 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002434
2435 if (msg->msg_type == MNCC_SETUP_REQ) {
2436 /* Incoming call to forward for inter-MSC Handover? */
2437 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2438 if (mncc_call)
2439 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2440 "Incoming call matches pending inter-MSC Handover Number\n");
2441 }
2442 if (!mncc_call) {
2443 /* Find already active MNCC FSM for this callref.
2444 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2445 * MNCC<->GSM-CC call handling. */
2446 mncc_call = mncc_find_by_callref_from_msg(msg);
2447 }
2448 if (mncc_call) {
2449 mncc_call_rx(mncc_call, msg);
2450 return 0;
2451 }
2452
2453 /* None of the above? Then it must be a normal GSM CC call related message. */
2454 return mncc_tx_to_gsm_cc(net, msg);
2455}
Harald Welte27989d42018-06-21 20:39:20 +02002456
2457static struct datastate {
2458 uint32_t states;
2459 int type;
2460 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2461} datastatelist[] = {
2462 /* mobile originating call establishment */
2463 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2464 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2465 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2466 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2467 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2468 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2469 /* mobile terminating call establishment */
2470 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2471 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2472 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2473 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2474 {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 */
2475 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2476 /* signalling during call */
2477 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2478 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2479 {SBIT(GSM_CSTATE_ACTIVE),
2480 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2481 {ALL_STATES,
2482 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2483 {ALL_STATES,
2484 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2485 {ALL_STATES,
2486 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2487 {SBIT(GSM_CSTATE_ACTIVE),
2488 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2489 {SBIT(GSM_CSTATE_ACTIVE),
2490 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2491 {SBIT(GSM_CSTATE_ACTIVE),
2492 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2493 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2494 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2495 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2496 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2497 {SBIT(GSM_CSTATE_ACTIVE),
2498 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2499 /* clearing */
2500 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2501 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2502 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2503 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2504 {ALL_STATES, /* 5.4.3.4 */
2505 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2506};
2507
2508#define DATASLLEN \
2509 (sizeof(datastatelist) / sizeof(struct datastate))
2510
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002511int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002512{
2513 struct gsm48_hdr *gh = msgb_l3(msg);
2514 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2515 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2516 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002517 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2518 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002519 int i, rc = 0;
2520
2521 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002522 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002523 return -EINVAL;
2524 }
2525
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002526 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002527 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002528 return -EINVAL;
2529 }
2530
2531 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002532 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002533
Harald Welte27989d42018-06-21 20:39:20 +02002534 /* Create transaction */
2535 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002536 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002537 trans = trans_alloc(net, vsub,
2538 TRANS_CC,
2539 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002540 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002541 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002542 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002543 GSM48_PDISC_CC | (transaction_id << 4),
2544 GSM48_MT_CC_RELEASE_COMPL);
2545 return -ENOMEM;
2546 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002547 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2548 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2549 trans_free(trans);
2550 return -EINVAL;
2551 }
2552
Harald Welte27989d42018-06-21 20:39:20 +02002553 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002554 msc_a_get(msc_a, MSC_A_USE_CC);
2555 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002556 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002557
2558 /* An earlier CM Service Request for this CC message now has concluded */
2559 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2560 LOG_MSC_A(msc_a, LOGL_ERROR,
2561 "Creating new CC transaction without prior CM Service Request\n");
2562 else
2563 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002564 }
2565
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002566 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2567 gsm48_cc_state_name(trans->cc.state));
2568
Harald Welte27989d42018-06-21 20:39:20 +02002569 /* find function for current state and message */
2570 for (i = 0; i < DATASLLEN; i++)
2571 if ((msg_type == datastatelist[i].type)
2572 && ((1 << trans->cc.state) & datastatelist[i].states))
2573 break;
2574 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002575 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002576
2577 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2578 * transaction right away. */
2579 if (trans->cc.state == GSM_CSTATE_NULL) {
2580 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2581 " -- disarding new CC transaction right away\n");
2582 trans_free(trans);
2583 }
Harald Welte27989d42018-06-21 20:39:20 +02002584 return 0;
2585 }
2586
2587 assert(trans->vsub);
2588
2589 rc = datastatelist[i].rout(trans, msg);
2590
Harald Welte27989d42018-06-21 20:39:20 +02002591 return rc;
2592}