blob: 8eb76b3f41e5ab8ec96c81f04b71496942fa53a3 [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
Harald Welte27989d42018-06-21 20:39:20 +0200187static void new_cc_state(struct gsm_trans *trans, int state)
188{
189 if (state > 31 || state < 0)
190 return;
191
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100192 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
193 gsm48_cc_state_name(trans->cc.state),
194 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200195
196 count_statistics(trans, state);
197 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200198
199 /* Stop the guard timer when a call reaches the active state */
200 if (state == GSM_CSTATE_ACTIVE)
201 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200202}
203
204static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
205{
206 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
207 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
208 uint8_t *cause, *call_state;
209
210 gh->msg_type = GSM48_MT_CC_STATUS;
211
212 cause = msgb_put(msg, 3);
213 cause[0] = 2;
214 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
215 cause[2] = 0x80 | 30; /* response to status inquiry */
216
217 call_state = msgb_put(msg, 1);
218 call_state[0] = 0xc0 | 0x00;
219
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100220 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200221}
222
223static void gsm48_stop_cc_timer(struct gsm_trans *trans)
224{
225 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100226 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200227 osmo_timer_del(&trans->cc.timer);
228 trans->cc.Tcurrent = 0;
229 }
230}
231
232static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
233 int msg_type, struct gsm_mncc *mncc)
234{
235 struct msgb *msg;
236 unsigned char *data;
237
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100238 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200239
240 mncc->msg_type = msg_type;
241
242 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
243 if (!msg)
244 return -ENOMEM;
245
246 data = msgb_put(msg, sizeof(struct gsm_mncc));
247 memcpy(data, mncc, sizeof(struct gsm_mncc));
248
249 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200250 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
251 if (trans)
252 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200253
254 return 0;
255}
256
257int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
258 uint32_t callref, int location, int value)
259{
260 struct gsm_mncc rel;
261
262 memset(&rel, 0, sizeof(rel));
263 rel.callref = callref;
264 mncc_set_cause(&rel, location, value);
265 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
266 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
267 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
268}
269
270/* Call Control Specific transaction release.
271 * gets called by trans_free, DO NOT CALL YOURSELF! */
272void _gsm48_cc_trans_free(struct gsm_trans *trans)
273{
274 gsm48_stop_cc_timer(trans);
275
Harald Welte27989d42018-06-21 20:39:20 +0200276 /* send release to L4, if callref still exists */
277 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100278 /* Send MNCC REL.ind (cause='Resource unavailable') */
279 if (trans->cc.mncc_initiated) {
280 mncc_release_ind(trans->net, trans, trans->callref,
281 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100282 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
283 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100284 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
285 }
286
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100287 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
288 * CC Release to the MS if it gets freed here. Hack it to do so. */
289 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
290 struct gsm_mncc rel = {};
291 rel.callref = trans->callref;
292 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
293 gsm48_cc_tx_release(trans, &rel);
294 }
Harald Welte27989d42018-06-21 20:39:20 +0200295 /* This is a final freeing of the transaction. The MNCC release may have triggered the
296 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
297 gsm48_stop_cc_timer(trans);
298 }
299 if (trans->cc.state != GSM_CSTATE_NULL)
300 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200301
302 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100303
304 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
305 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200306}
307
Harald Welte27989d42018-06-21 20:39:20 +0200308/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100309static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200310{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100311 if (trans->msc_a) {
312 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
313 "Handle paging error: transaction already associated with subscriber,"
314 " apparently it was already handled. Skip.\n");
315 return;
Harald Welte27989d42018-06-21 20:39:20 +0200316 }
317
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100318 if (msc_a) {
319 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
320 /* Assign conn */
321 msc_a_get(msc_a, MSC_A_USE_CC);
322 trans->msc_a = msc_a;
323 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200324
325 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300326 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200327 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300328 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
329 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
330 &trans->cc.msg.gcr[0],
331 sizeof(trans->cc.msg.gcr));
332 if (rc < 0)
333 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
334 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200335 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200336 }
337
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100338 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
339 /* send SETUP request to called party */
340 gsm48_cc_tx_setup(trans, &trans->cc.msg);
341 } else {
342 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
343 /* Temporarily out of order */
344 mncc_release_ind(trans->net, trans,
345 trans->callref,
346 GSM48_CAUSE_LOC_PRN_S_LU,
347 GSM48_CC_CAUSE_DEST_OOO);
348 trans->callref = 0;
349 trans->paging_request = NULL;
350 trans_free(trans);
351 }
Harald Welte27989d42018-06-21 20:39:20 +0200352}
353
354/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100355static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200356{
357 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
358 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359 struct call_leg *cl1;
360 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200361
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100362 if (!trans1 || !trans2) {
363 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200364 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100365 }
Harald Welte27989d42018-06-21 20:39:20 +0200366
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100367 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100368 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
369 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 +0200370 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100371 }
372
373 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
374 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200375
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100376 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
377 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
378 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
379 * If we can't, then we must give up. */
380 cl1 = trans1->msc_a->cc.call_leg;
381 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200382
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100383 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200384}
385
386static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
387{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100388 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200389 return gsm48_cc_tx_status(trans, msg);
390}
391
Harald Welte27989d42018-06-21 20:39:20 +0200392static void gsm48_cc_timeout(void *arg)
393{
394 struct gsm_trans *trans = arg;
395 int disconnect = 0, release = 0;
396 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
397 int mo_location = GSM48_CAUSE_LOC_USER;
398 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
399 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
400 struct gsm_mncc mo_rel, l4_rel;
401
Neels Hofmeyrac7d9a62022-08-06 14:16:55 +0200402 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
403
Harald Welte27989d42018-06-21 20:39:20 +0200404 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
405 mo_rel.callref = trans->callref;
406 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
407 l4_rel.callref = trans->callref;
408
409 switch(trans->cc.Tcurrent) {
410 case 0x303:
411 release = 1;
412 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
413 break;
414 case 0x310:
415 disconnect = 1;
416 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
417 break;
418 case 0x313:
419 disconnect = 1;
420 /* unknown, did not find it in the specs */
421 break;
422 case 0x301:
423 disconnect = 1;
424 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
425 break;
426 case 0x308:
427 if (!trans->cc.T308_second) {
428 /* restart T308 a second time */
429 gsm48_cc_tx_release(trans, &trans->cc.msg);
430 trans->cc.T308_second = 1;
431 break; /* stay in release state */
432 }
433 trans_free(trans);
434 return;
435 case 0x306:
436 release = 1;
437 mo_cause = trans->cc.msg.cause.value;
438 mo_location = trans->cc.msg.cause.location;
439 break;
440 case 0x323:
441 disconnect = 1;
442 break;
443 default:
444 release = 1;
445 }
446
447 if (release && trans->callref) {
448 /* process release towards layer 4 */
449 mncc_release_ind(trans->net, trans, trans->callref,
450 l4_location, l4_cause);
451 trans->callref = 0;
452 }
453
454 if (disconnect && trans->callref) {
455 /* process disconnect towards layer 4 */
456 mncc_set_cause(&l4_rel, l4_location, l4_cause);
457 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
458 }
459
460 /* process disconnect towards mobile station */
461 if (disconnect || release) {
462 mncc_set_cause(&mo_rel, mo_location, mo_cause);
463 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
464 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
465 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
466 mo_rel.cause.diag_len = 3;
467
468 if (disconnect)
469 gsm48_cc_tx_disconnect(trans, &mo_rel);
470 if (release)
471 gsm48_cc_tx_release(trans, &mo_rel);
472 }
473
474}
475
476/* disconnect both calls from the bridge */
477static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100478 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200479{
480 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
481 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
482 struct gsm_mncc mx_rel;
483 if (!trans0 || !trans1)
484 return;
485
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100486 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
487 trans0->callref, trans1->callref, strerror(err));
488 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200489 trans0->callref, trans1->callref, strerror(err));
490
491 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
492 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
493 GSM48_CC_CAUSE_CHAN_UNACCEPT);
494
495 mx_rel.callref = trans0->callref;
496 gsm48_cc_tx_disconnect(trans0, &mx_rel);
497
498 mx_rel.callref = trans1->callref;
499 gsm48_cc_tx_disconnect(trans1, &mx_rel);
500}
501
502static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
503 int sec, int micro)
504{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100505 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200506 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
507 osmo_timer_schedule(&trans->cc.timer, sec, micro);
508 trans->cc.Tcurrent = current;
509}
510
511static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
512{
513 struct gsm48_hdr *gh = msgb_l3(msg);
514 uint8_t msg_type = gsm48_hdr_msg_type(gh);
515 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
516 struct tlv_parsed tp;
517 struct gsm_mncc setup;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100518 struct osmo_sockaddr_str *rtp_cn_local;
519 struct sdp_msg *sdp;
520 struct msc_a *msc_a = trans->msc_a;
521 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200522
Philipp Maier9ca7b312018-10-10 17:00:49 +0200523 gsm48_start_guard_timer(trans);
524
Harald Welte27989d42018-06-21 20:39:20 +0200525 memset(&setup, 0, sizeof(struct gsm_mncc));
526 setup.callref = trans->callref;
527
Keith Whytea1a70be2021-05-16 02:59:52 +0200528 /* New Global Call Reference */
529 if (!trans->cc.lcls)
530 trans->cc.lcls = trans_lcls_compose(trans, true);
531
532 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300533 if (trans->cc.lcls) {
534 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
535 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
536 int rc;
537
538 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
539 memcpy(&setup.gcr[0], gcr_msg->data, rc);
540 setup.fields |= MNCC_F_GCR;
541 } else
542 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
543 msgb_free(gcr_msg);
544 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200545
Harald Welte27989d42018-06-21 20:39:20 +0200546 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
547 /* emergency setup is identified by msg_type */
548 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
549 setup.fields |= MNCC_F_EMERGENCY;
550 setup.emergency = 1;
551 /* use destination number as configured by user (if any) */
552 if (trans->net->emergency.route_to_msisdn) {
553 setup.fields |= MNCC_F_CALLED;
554 setup.called.type = 0; /* unknown */
555 setup.called.plan = 0; /* unknown */
556 OSMO_STRLCPY_ARRAY(setup.called.number,
557 trans->net->emergency.route_to_msisdn);
558 }
559 }
560
561 /* use subscriber as calling party number */
562 setup.fields |= MNCC_F_CALLING;
563 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
564 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
565
566 /* bearer capability */
567 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
568 setup.fields |= MNCC_F_BEARER_CAP;
569 gsm48_decode_bearer_cap(&setup.bearer_cap,
570 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
571
572 /* Create a copy of the bearer capability
573 * in the transaction struct, so we can use
574 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100575 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200576 sizeof(trans->bearer_cap));
577 }
578 /* facility */
579 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
580 setup.fields |= MNCC_F_FACILITY;
581 gsm48_decode_facility(&setup.facility,
582 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
583 }
584 /* called party bcd number */
585 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
586 setup.fields |= MNCC_F_CALLED;
587 gsm48_decode_called(&setup.called,
588 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
589 }
590 /* user-user */
591 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
592 setup.fields |= MNCC_F_USERUSER;
593 gsm48_decode_useruser(&setup.useruser,
594 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
595 }
596 /* ss-version */
597 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
598 setup.fields |= MNCC_F_SSVERSION;
599 gsm48_decode_ssversion(&setup.ssversion,
600 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
601 }
602 /* CLIR suppression */
603 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
604 setup.clir.sup = 1;
605 /* CLIR invocation */
606 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
607 setup.clir.inv = 1;
608 /* cc cap */
609 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
610 setup.fields |= MNCC_F_CCCAP;
611 gsm48_decode_cccap(&setup.cccap,
612 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
613 }
614
615 new_cc_state(trans, GSM_CSTATE_INITIATED);
616
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100617 /* MO call leg starting, gather all codec information so far known: */
618 codec_filter_init(&trans->cc.codecs);
619 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
620 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
621 if (setup.fields & MNCC_F_BEARER_CAP)
622 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
623 codec_filter_run(&trans->cc.codecs);
624
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100625 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
626 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyr9a539f32022-01-13 21:39:11 +0100627 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200628
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200629 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200630
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100631 /* Insert the CN side RTP port now available into SDP and compose SDP string */
632 rtp_cn_local = call_leg_local_ip(msc_a->cc.call_leg, RTP_TO_CN);
633 if (!osmo_sockaddr_str_is_nonzero(rtp_cn_local)) {
634 LOG_TRANS(trans, LOGL_ERROR, "Cannot compose SDP for MNCC_SETUP_IND: no RTP set up for the CN side\n");
635 /* FIXME: re-add below two lines as soon as CN CRCX is done first
636 * (see Change-Id Ie433db1ba0c46d4b97538a969233c155cefac21c).
637 * keeping it non-fatal for the moment, for tests to continue succeeding. */
638 //trans_free(trans);
639 //return -EINVAL;
640 }
641
642 codec_filter_set_local_rtp(&trans->cc.codecs, rtp_cn_local);
643 codec_filter_run(&trans->cc.codecs);
644 sdp = trans->cc.codecs.result.audio_codecs.count ? &trans->cc.codecs.result : NULL;
645 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
646 if (rc >= sizeof(setup.sdp)) {
647 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
648 trans_free(trans);
649 return -EINVAL;
650 }
651
Harald Welte27989d42018-06-21 20:39:20 +0200652 /* indicate setup to MNCC */
653 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
654
655 /* MNCC code will modify the channel asynchronously, we should
656 * ipaccess-bind only after the modification has been made to the
657 * lchan->tch_mode */
658 return 0;
659}
660
661static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
662{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100663 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200664 struct gsm48_hdr *gh;
665 struct gsm_mncc *setup = arg;
666 int rc, trans_id;
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100667 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200668
669 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
670
671 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700672 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100673 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200674 "This is not allowed!\n");
675 /* Temporarily out of order */
676 rc = mncc_release_ind(trans->net, trans, trans->callref,
677 GSM48_CAUSE_LOC_PRN_S_LU,
678 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
679 trans->callref = 0;
680 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200681 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200682 return rc;
683 }
684
685 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100686 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200687 if (trans_id < 0) {
688 /* no free transaction ID */
689 rc = mncc_release_ind(trans->net, trans, trans->callref,
690 GSM48_CAUSE_LOC_PRN_S_LU,
691 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
692 trans->callref = 0;
693 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200694 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200695 return rc;
696 }
697 trans->transaction_id = trans_id;
698
699 gh->msg_type = GSM48_MT_CC_SETUP;
700
701 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
702
Neels Hofmeyra4195db2022-01-13 21:40:58 +0100703 /* MT call leg is starting. Gather all codecs information so far known.
704 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
705 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
706 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
707 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
708 * Capabilities. */
709 codec_filter_init(&trans->cc.codecs);
710 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
711 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
712 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
713 if (setup->sdp[0]) {
714 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
715 if (rc)
716 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
717 }
718 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
719 * MNCC, if any. */
720 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
721 trans->cc.codecs.remote = (struct sdp_msg){};
722 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
723 &setup->bearer_cap);
724 }
725 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
726 if (!trans->cc.codecs.remote.audio_codecs.count)
727 LOG_TRANS(trans, LOGL_ERROR,
728 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
729
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100730 /* Compose outgoing Bearer Capabilities: translate SDP to bearer capability Speech Version entries.
731 * Send only codecs that remain according to the codec filter. */
Harald Welte27989d42018-06-21 20:39:20 +0200732 /* Create a copy of the bearer capability in the transaction struct, so we
733 * can use this information later */
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100734 codec_filter_run(&trans->cc.codecs);
735 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
736 bearer_cap = (struct gsm_mncc_bearer_cap){
737 .speech_ver = { -1 },
738 };
739 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.codecs.result.audio_codecs);
740 rc = bearer_cap_set_radio(&bearer_cap);
741 if (rc) {
742 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
743 trans_free(trans);
744 msgb_free(msg);
745 return rc;
Harald Welte27989d42018-06-21 20:39:20 +0200746 }
Neels Hofmeyr3d4f3452022-01-14 02:41:55 +0100747 /* (As earlier code did, keep a copy in trans->bearer_cap) */
748 trans->bearer_cap = bearer_cap;
749 /* If no resulting codecs remain, error out. If the MGW were able to transcode, we would just use unidentical
750 * codecs on each conn of the MGW endpoint. */
751 if (bearer_cap.speech_ver[0] == -1) {
752 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
753 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
754
755 /* incompatible codecs */
756 rc = mncc_release_ind(trans->net, trans, trans->callref,
757 GSM48_CAUSE_LOC_PRN_S_LU,
758 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
759 trans_free(trans);
760 msgb_free(msg);
761 return rc;
762 }
763 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
764
Harald Welte27989d42018-06-21 20:39:20 +0200765 /* facility */
766 if (setup->fields & MNCC_F_FACILITY)
767 gsm48_encode_facility(msg, 0, &setup->facility);
768 /* progress */
769 if (setup->fields & MNCC_F_PROGRESS)
770 gsm48_encode_progress(msg, 0, &setup->progress);
771 /* calling party BCD number */
772 if (setup->fields & MNCC_F_CALLING)
773 gsm48_encode_calling(msg, &setup->calling);
774 /* called party BCD number */
775 if (setup->fields & MNCC_F_CALLED)
776 gsm48_encode_called(msg, &setup->called);
777 /* user-user */
778 if (setup->fields & MNCC_F_USERUSER)
779 gsm48_encode_useruser(msg, 0, &setup->useruser);
780 /* redirecting party BCD number */
781 if (setup->fields & MNCC_F_REDIRECTING)
782 gsm48_encode_redirecting(msg, &setup->redirecting);
783 /* signal */
784 if (setup->fields & MNCC_F_SIGNAL)
785 gsm48_encode_signal(msg, setup->signal);
786
787 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
788
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200789 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200790
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100791 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200792}
793
794static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
795{
796 struct gsm48_hdr *gh = msgb_l3(msg);
797 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
798 struct tlv_parsed tp;
799 struct gsm_mncc call_conf;
800 int rc;
801
802 gsm48_stop_cc_timer(trans);
803 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
804
805 memset(&call_conf, 0, sizeof(struct gsm_mncc));
806 call_conf.callref = trans->callref;
807
808 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
809#if 0
810 /* repeat */
811 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
812 call_conf.repeat = 1;
813 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
814 call_conf.repeat = 2;
815#endif
816 /* bearer capability */
817 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
818 call_conf.fields |= MNCC_F_BEARER_CAP;
819 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
820 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
821
822 /* Create a copy of the bearer capability
823 * in the transaction struct, so we can use
824 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100825 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200826 sizeof(trans->bearer_cap));
Neels Hofmeyrdb61f732022-01-13 19:59:02 +0100827
828 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
829 * Store in codecs filter. */
830 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200831 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100832
Harald Welte27989d42018-06-21 20:39:20 +0200833 /* cause */
834 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
835 call_conf.fields |= MNCC_F_CAUSE;
836 gsm48_decode_cause(&call_conf.cause,
837 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
838 }
839 /* cc cap */
840 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
841 call_conf.fields |= MNCC_F_CCCAP;
842 gsm48_decode_cccap(&call_conf.cccap,
843 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
844 }
845
846 /* IMSI of called subscriber */
847 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
848
849 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
850
851 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100852 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200853
854 /* don't continue, if there were problems with
855 * the call assignment. */
856 if (rc)
857 return rc;
858
859 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
860 &call_conf);
861}
862
863static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
864{
865 struct gsm_mncc *proceeding = arg;
866 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
867 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
868 int rc;
869
870 gh->msg_type = GSM48_MT_CC_CALL_PROC;
871
872 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
873
874 /* bearer capability */
875 if (proceeding->fields & MNCC_F_BEARER_CAP) {
876 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
877 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
878 }
879 /* facility */
880 if (proceeding->fields & MNCC_F_FACILITY)
881 gsm48_encode_facility(msg, 0, &proceeding->facility);
882 /* progress */
883 if (proceeding->fields & MNCC_F_PROGRESS)
884 gsm48_encode_progress(msg, 0, &proceeding->progress);
885
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100886 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200887 if (rc)
888 return rc;
889
890 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100891 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200892}
893
894static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
895{
896 struct gsm48_hdr *gh = msgb_l3(msg);
897 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
898 struct tlv_parsed tp;
899 struct gsm_mncc alerting;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100900 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200901
902 gsm48_stop_cc_timer(trans);
903 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
904
905 memset(&alerting, 0, sizeof(struct gsm_mncc));
906 alerting.callref = trans->callref;
907 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
908 /* facility */
909 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
910 alerting.fields |= MNCC_F_FACILITY;
911 gsm48_decode_facility(&alerting.facility,
912 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
913 }
914
915 /* progress */
916 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
917 alerting.fields |= MNCC_F_PROGRESS;
918 gsm48_decode_progress(&alerting.progress,
919 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
920 }
921 /* ss-version */
922 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
923 alerting.fields |= MNCC_F_SSVERSION;
924 gsm48_decode_ssversion(&alerting.ssversion,
925 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
926 }
927
928 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
929
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100930 codec_filter_run(&trans->cc.codecs);
931 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
932 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
933 if (rc >= sizeof(alerting.sdp)) {
934 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
935 rc, sizeof(alerting.sdp));
936 trans_free(trans);
937 return -EINVAL;
938 }
939
Harald Welte27989d42018-06-21 20:39:20 +0200940 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
941 &alerting);
942}
943
944static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
945{
946 struct gsm_mncc *alerting = arg;
947 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
948 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
949
950 gh->msg_type = GSM48_MT_CC_ALERTING;
951
952 /* facility */
953 if (alerting->fields & MNCC_F_FACILITY)
954 gsm48_encode_facility(msg, 0, &alerting->facility);
955 /* progress */
956 if (alerting->fields & MNCC_F_PROGRESS)
957 gsm48_encode_progress(msg, 0, &alerting->progress);
958 /* user-user */
959 if (alerting->fields & MNCC_F_USERUSER)
960 gsm48_encode_useruser(msg, 0, &alerting->useruser);
961
962 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
963
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100964 if (alerting->sdp[0]) {
965 struct call_leg *cl = trans->msc_a->cc.call_leg;
966 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
967 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
968 codec_filter_run(&trans->cc.codecs);
969 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
970 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
971 if (rtp_cn) {
972 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
973 rtp_stream_commit(rtp_cn);
974 }
975 }
976
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100977 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200978}
979
980static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
981{
982 struct gsm_mncc *progress = arg;
983 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
984 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
985
986 gh->msg_type = GSM48_MT_CC_PROGRESS;
987
988 /* progress */
989 gsm48_encode_progress(msg, 1, &progress->progress);
990 /* user-user */
991 if (progress->fields & MNCC_F_USERUSER)
992 gsm48_encode_useruser(msg, 0, &progress->useruser);
993
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100994 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200995}
996
997static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
998{
999 struct gsm_mncc *connect = arg;
1000 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1001 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1002
1003 gh->msg_type = GSM48_MT_CC_CONNECT;
1004
1005 gsm48_stop_cc_timer(trans);
1006 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1007
1008 /* facility */
1009 if (connect->fields & MNCC_F_FACILITY)
1010 gsm48_encode_facility(msg, 0, &connect->facility);
1011 /* progress */
1012 if (connect->fields & MNCC_F_PROGRESS)
1013 gsm48_encode_progress(msg, 0, &connect->progress);
1014 /* connected number */
1015 if (connect->fields & MNCC_F_CONNECTED)
1016 gsm48_encode_connected(msg, &connect->connected);
1017 /* user-user */
1018 if (connect->fields & MNCC_F_USERUSER)
1019 gsm48_encode_useruser(msg, 0, &connect->useruser);
1020
1021 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1022
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001023 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
1024 if (connect->sdp[0]) {
1025 struct call_leg *cl = trans->msc_a->cc.call_leg;
1026 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1027 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, connect->sdp);
1028 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1029 get_mncc_name(connect->msg_type),
1030 codec_filter_to_str(&trans->cc.codecs));
1031 if (rtp_cn) {
1032 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1033 rtp_stream_commit(rtp_cn);
1034 }
1035 }
1036
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001037 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001038}
1039
1040static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1041{
1042 struct gsm48_hdr *gh = msgb_l3(msg);
1043 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1044 struct tlv_parsed tp;
1045 struct gsm_mncc connect;
1046
1047 gsm48_stop_cc_timer(trans);
1048
1049 memset(&connect, 0, sizeof(struct gsm_mncc));
1050 connect.callref = trans->callref;
1051 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1052 /* use subscriber as connected party number */
1053 connect.fields |= MNCC_F_CONNECTED;
1054 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1055 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1056
1057 /* facility */
1058 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1059 connect.fields |= MNCC_F_FACILITY;
1060 gsm48_decode_facility(&connect.facility,
1061 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1062 }
1063 /* user-user */
1064 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1065 connect.fields |= MNCC_F_USERUSER;
1066 gsm48_decode_useruser(&connect.useruser,
1067 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1068 }
1069 /* ss-version */
1070 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1071 connect.fields |= MNCC_F_SSVERSION;
1072 gsm48_decode_ssversion(&connect.ssversion,
1073 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1074 }
1075
1076 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001077 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001078
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001079 codec_filter_run(&trans->cc.codecs);
1080 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001081 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1082}
1083
1084
1085static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1086{
1087 struct gsm_mncc connect_ack;
1088
1089 gsm48_stop_cc_timer(trans);
1090
1091 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001092 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 +02001093
1094 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1095 connect_ack.callref = trans->callref;
1096
1097 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1098 &connect_ack);
1099}
1100
1101static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1102{
1103 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1104 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1105
1106 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1107
1108 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1109
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001110 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001111}
1112
1113static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1114{
1115 struct gsm48_hdr *gh = msgb_l3(msg);
1116 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1117 struct tlv_parsed tp;
1118 struct gsm_mncc disc;
1119
1120 gsm48_stop_cc_timer(trans);
1121
1122 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1123
1124 memset(&disc, 0, sizeof(struct gsm_mncc));
1125 disc.callref = trans->callref;
1126 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1127 /* cause */
1128 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1129 disc.fields |= MNCC_F_CAUSE;
1130 gsm48_decode_cause(&disc.cause,
1131 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1132 }
1133 /* facility */
1134 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1135 disc.fields |= MNCC_F_FACILITY;
1136 gsm48_decode_facility(&disc.facility,
1137 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1138 }
1139 /* user-user */
1140 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1141 disc.fields |= MNCC_F_USERUSER;
1142 gsm48_decode_useruser(&disc.useruser,
1143 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1144 }
1145 /* ss-version */
1146 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1147 disc.fields |= MNCC_F_SSVERSION;
1148 gsm48_decode_ssversion(&disc.ssversion,
1149 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1150 }
1151
1152 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001153}
1154
1155static struct gsm_mncc_cause default_cause = {
1156 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1157 .coding = 0,
1158 .rec = 0,
1159 .rec_val = 0,
1160 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1161 .diag_len = 0,
1162 .diag = { 0 },
1163};
1164
1165static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1166{
1167 struct gsm_mncc *disc = arg;
1168 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1169 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1170
1171 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1172
1173 gsm48_stop_cc_timer(trans);
1174 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1175
1176 /* cause */
1177 if (disc->fields & MNCC_F_CAUSE)
1178 gsm48_encode_cause(msg, 1, &disc->cause);
1179 else
1180 gsm48_encode_cause(msg, 1, &default_cause);
1181
1182 /* facility */
1183 if (disc->fields & MNCC_F_FACILITY)
1184 gsm48_encode_facility(msg, 0, &disc->facility);
1185 /* progress */
1186 if (disc->fields & MNCC_F_PROGRESS)
1187 gsm48_encode_progress(msg, 0, &disc->progress);
1188 /* user-user */
1189 if (disc->fields & MNCC_F_USERUSER)
1190 gsm48_encode_useruser(msg, 0, &disc->useruser);
1191
1192 /* store disconnect cause for T306 expiry */
1193 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1194
1195 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1196
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001197 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001198}
1199
1200static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1201{
1202 struct gsm48_hdr *gh = msgb_l3(msg);
1203 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1204 struct tlv_parsed tp;
1205 struct gsm_mncc rel;
1206 int rc;
1207
1208 gsm48_stop_cc_timer(trans);
1209
1210 memset(&rel, 0, sizeof(struct gsm_mncc));
1211 rel.callref = trans->callref;
1212 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1213 /* cause */
1214 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1215 rel.fields |= MNCC_F_CAUSE;
1216 gsm48_decode_cause(&rel.cause,
1217 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1218 }
1219 /* facility */
1220 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1221 rel.fields |= MNCC_F_FACILITY;
1222 gsm48_decode_facility(&rel.facility,
1223 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1224 }
1225 /* user-user */
1226 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1227 rel.fields |= MNCC_F_USERUSER;
1228 gsm48_decode_useruser(&rel.useruser,
1229 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1230 }
1231 /* ss-version */
1232 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1233 rel.fields |= MNCC_F_SSVERSION;
1234 gsm48_decode_ssversion(&rel.ssversion,
1235 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1236 }
1237
1238 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1239 /* release collision 5.4.5 */
1240 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1241 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001242 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001243 GSM48_PDISC_CC | (trans->transaction_id << 4),
1244 GSM48_MT_CC_RELEASE_COMPL);
1245 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1246 }
1247
1248 new_cc_state(trans, GSM_CSTATE_NULL);
1249
1250 trans->callref = 0;
1251 trans_free(trans);
1252
1253 return rc;
1254}
1255
1256static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1257{
1258 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001259 struct msgb *msg;
1260 struct gsm48_hdr *gh;
1261
1262 if (!trans->msc_a) {
1263 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1264 return -EINVAL;
1265 }
1266
1267 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1268 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001269
1270 gh->msg_type = GSM48_MT_CC_RELEASE;
1271
1272 gsm48_stop_cc_timer(trans);
1273 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1274
1275 /* cause */
1276 if (rel->fields & MNCC_F_CAUSE)
1277 gsm48_encode_cause(msg, 0, &rel->cause);
1278 /* facility */
1279 if (rel->fields & MNCC_F_FACILITY)
1280 gsm48_encode_facility(msg, 0, &rel->facility);
1281 /* user-user */
1282 if (rel->fields & MNCC_F_USERUSER)
1283 gsm48_encode_useruser(msg, 0, &rel->useruser);
1284
1285 trans->cc.T308_second = 0;
1286 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1287
1288 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1289 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1290
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001291 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001292}
1293
1294static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1295{
1296 struct gsm48_hdr *gh = msgb_l3(msg);
1297 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1298 struct tlv_parsed tp;
1299 struct gsm_mncc rel;
1300 int rc = 0;
1301
1302 gsm48_stop_cc_timer(trans);
1303
1304 memset(&rel, 0, sizeof(struct gsm_mncc));
1305 rel.callref = trans->callref;
1306 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1307 /* cause */
1308 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1309 rel.fields |= MNCC_F_CAUSE;
1310 gsm48_decode_cause(&rel.cause,
1311 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1312 }
1313 /* facility */
1314 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1315 rel.fields |= MNCC_F_FACILITY;
1316 gsm48_decode_facility(&rel.facility,
1317 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1318 }
1319 /* user-user */
1320 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1321 rel.fields |= MNCC_F_USERUSER;
1322 gsm48_decode_useruser(&rel.useruser,
1323 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1324 }
1325 /* ss-version */
1326 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1327 rel.fields |= MNCC_F_SSVERSION;
1328 gsm48_decode_ssversion(&rel.ssversion,
1329 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1330 }
1331
1332 if (trans->callref) {
1333 switch (trans->cc.state) {
1334 case GSM_CSTATE_CALL_PRESENT:
1335 rc = mncc_recvmsg(trans->net, trans,
1336 MNCC_REJ_IND, &rel);
1337 break;
1338 case GSM_CSTATE_RELEASE_REQ:
1339 rc = mncc_recvmsg(trans->net, trans,
1340 MNCC_REL_CNF, &rel);
1341 break;
1342 default:
1343 rc = mncc_recvmsg(trans->net, trans,
1344 MNCC_REL_IND, &rel);
1345 }
1346 }
1347
1348 trans->callref = 0;
1349 trans_free(trans);
1350
1351 return rc;
1352}
1353
1354static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1355{
1356 struct gsm_mncc *rel = arg;
1357 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1358 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1359 int ret;
1360
1361 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1362
1363 trans->callref = 0;
1364
1365 gsm48_stop_cc_timer(trans);
1366
1367 /* cause */
1368 if (rel->fields & MNCC_F_CAUSE)
1369 gsm48_encode_cause(msg, 0, &rel->cause);
1370 /* facility */
1371 if (rel->fields & MNCC_F_FACILITY)
1372 gsm48_encode_facility(msg, 0, &rel->facility);
1373 /* user-user */
1374 if (rel->fields & MNCC_F_USERUSER)
1375 gsm48_encode_useruser(msg, 0, &rel->useruser);
1376
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001377 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001378
1379 trans_free(trans);
1380
1381 return ret;
1382}
1383
1384static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1385{
1386 struct gsm48_hdr *gh = msgb_l3(msg);
1387 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1388 struct tlv_parsed tp;
1389 struct gsm_mncc fac;
1390
1391 memset(&fac, 0, sizeof(struct gsm_mncc));
1392 fac.callref = trans->callref;
1393 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1394 /* facility */
1395 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1396 fac.fields |= MNCC_F_FACILITY;
1397 gsm48_decode_facility(&fac.facility,
1398 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1399 }
1400 /* ss-version */
1401 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1402 fac.fields |= MNCC_F_SSVERSION;
1403 gsm48_decode_ssversion(&fac.ssversion,
1404 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1405 }
1406
1407 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1408}
1409
1410static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1411{
1412 struct gsm_mncc *fac = arg;
1413 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1414 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1415
1416 gh->msg_type = GSM48_MT_CC_FACILITY;
1417
1418 /* facility */
1419 gsm48_encode_facility(msg, 1, &fac->facility);
1420
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001421 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001422}
1423
1424static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1425{
1426 struct gsm_mncc hold;
1427
1428 memset(&hold, 0, sizeof(struct gsm_mncc));
1429 hold.callref = trans->callref;
1430 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1431}
1432
1433static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1434{
1435 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1436 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1437
1438 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1439
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001440 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001441}
1442
1443static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1444{
1445 struct gsm_mncc *hold_rej = arg;
1446 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1447 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1448
1449 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1450
1451 /* cause */
1452 if (hold_rej->fields & MNCC_F_CAUSE)
1453 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1454 else
1455 gsm48_encode_cause(msg, 1, &default_cause);
1456
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001457 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001458}
1459
1460static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1461{
1462 struct gsm_mncc retrieve;
1463
1464 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1465 retrieve.callref = trans->callref;
1466 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1467 &retrieve);
1468}
1469
1470static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1471{
1472 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1473 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1474
1475 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1476
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001477 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001478}
1479
1480static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1481{
1482 struct gsm_mncc *retrieve_rej = arg;
1483 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1484 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1485
1486 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1487
1488 /* cause */
1489 if (retrieve_rej->fields & MNCC_F_CAUSE)
1490 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1491 else
1492 gsm48_encode_cause(msg, 1, &default_cause);
1493
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001494 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001495}
1496
1497static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1498{
1499 struct gsm48_hdr *gh = msgb_l3(msg);
1500 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1501 struct tlv_parsed tp;
1502 struct gsm_mncc dtmf;
1503
1504 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1505 dtmf.callref = trans->callref;
1506 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1507 /* keypad facility */
1508 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1509 dtmf.fields |= MNCC_F_KEYPAD;
1510 gsm48_decode_keypad(&dtmf.keypad,
1511 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1512 }
1513
1514 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1515}
1516
1517static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1518{
1519 struct gsm_mncc *dtmf = arg;
1520 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1521 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1522
1523 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1524
1525 /* keypad */
1526 if (dtmf->fields & MNCC_F_KEYPAD)
1527 gsm48_encode_keypad(msg, dtmf->keypad);
1528
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001529 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001530}
1531
1532static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1533{
1534 struct gsm_mncc *dtmf = arg;
1535 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1536 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1537
1538 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1539
1540 /* cause */
1541 if (dtmf->fields & MNCC_F_CAUSE)
1542 gsm48_encode_cause(msg, 1, &dtmf->cause);
1543 else
1544 gsm48_encode_cause(msg, 1, &default_cause);
1545
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001546 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001547}
1548
1549static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1550{
1551 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1552 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1553
1554 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1555
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001556 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001557}
1558
1559static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1560{
1561 struct gsm_mncc dtmf;
1562
1563 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1564 dtmf.callref = trans->callref;
1565
1566 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1567}
1568
1569static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1570{
1571 struct gsm48_hdr *gh = msgb_l3(msg);
1572 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1573 struct tlv_parsed tp;
1574 struct gsm_mncc modify;
1575
1576 memset(&modify, 0, sizeof(struct gsm_mncc));
1577 modify.callref = trans->callref;
1578 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1579 /* bearer capability */
1580 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1581 modify.fields |= MNCC_F_BEARER_CAP;
1582 gsm48_decode_bearer_cap(&modify.bearer_cap,
1583 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1584
1585 /* Create a copy of the bearer capability
1586 * in the transaction struct, so we can use
1587 * this information later */
1588 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1589 sizeof(trans->bearer_cap));
1590 }
1591
1592 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1593
1594 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1595}
1596
1597static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1598{
1599 struct gsm_mncc *modify = arg;
1600 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1601 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1602
1603 gh->msg_type = GSM48_MT_CC_MODIFY;
1604
1605 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1606
1607 /* bearer capability */
1608 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1609 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1610
1611 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1612
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001613 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001614}
1615
1616static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1617{
1618 struct gsm48_hdr *gh = msgb_l3(msg);
1619 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1620 struct tlv_parsed tp;
1621 struct gsm_mncc modify;
1622
1623 gsm48_stop_cc_timer(trans);
1624
1625 memset(&modify, 0, sizeof(struct gsm_mncc));
1626 modify.callref = trans->callref;
1627 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1628 /* bearer capability */
1629 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1630 modify.fields |= MNCC_F_BEARER_CAP;
1631 gsm48_decode_bearer_cap(&modify.bearer_cap,
1632 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1633
1634 /* Create a copy of the bearer capability
1635 * in the transaction struct, so we can use
1636 * this information later */
1637 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1638 sizeof(trans->bearer_cap));
1639 }
1640
1641 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1642
1643 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1644}
1645
1646static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1647{
1648 struct gsm_mncc *modify = arg;
1649 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1650 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1651
1652 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1653
1654 /* bearer capability */
1655 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1656 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1657
1658 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1659
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001660 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001661}
1662
1663static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1664{
1665 struct gsm48_hdr *gh = msgb_l3(msg);
1666 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1667 struct tlv_parsed tp;
1668 struct gsm_mncc modify;
1669
1670 gsm48_stop_cc_timer(trans);
1671
1672 memset(&modify, 0, sizeof(struct gsm_mncc));
1673 modify.callref = trans->callref;
1674 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1675 /* bearer capability */
1676 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1677 modify.fields |= GSM48_IE_BEARER_CAP;
1678 gsm48_decode_bearer_cap(&modify.bearer_cap,
1679 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1680
1681 /* Create a copy of the bearer capability
1682 * in the transaction struct, so we can use
1683 * this information later */
1684 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1685 sizeof(trans->bearer_cap));
1686 }
1687 /* cause */
1688 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1689 modify.fields |= MNCC_F_CAUSE;
1690 gsm48_decode_cause(&modify.cause,
1691 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1692 }
1693
1694 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1695
1696 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1697}
1698
1699static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1700{
1701 struct gsm_mncc *modify = arg;
1702 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1703 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1704
1705 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1706
1707 /* bearer capability */
1708 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1709 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1710 /* cause */
1711 gsm48_encode_cause(msg, 1, &modify->cause);
1712
1713 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1714
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001715 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001716}
1717
1718static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1719{
1720 struct gsm_mncc *notify = arg;
1721 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1722 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1723
1724 gh->msg_type = GSM48_MT_CC_NOTIFY;
1725
1726 /* notify */
1727 gsm48_encode_notify(msg, notify->notify);
1728
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001729 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001730}
1731
1732static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1733{
1734 struct gsm48_hdr *gh = msgb_l3(msg);
1735 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1736// struct tlv_parsed tp;
1737 struct gsm_mncc notify;
1738
1739 memset(&notify, 0, sizeof(struct gsm_mncc));
1740 notify.callref = trans->callref;
1741// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1742 if (payload_len >= 1)
1743 gsm48_decode_notify(&notify.notify, gh->data);
1744
1745 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1746}
1747
1748static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1749{
1750 struct gsm_mncc *user = arg;
1751 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1752 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1753
1754 gh->msg_type = GSM48_MT_CC_USER_INFO;
1755
1756 /* user-user */
1757 if (user->fields & MNCC_F_USERUSER)
1758 gsm48_encode_useruser(msg, 1, &user->useruser);
1759 /* more data */
1760 if (user->more)
1761 gsm48_encode_more(msg);
1762
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001763 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001764}
1765
1766static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1767{
1768 struct gsm48_hdr *gh = msgb_l3(msg);
1769 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1770 struct tlv_parsed tp;
1771 struct gsm_mncc user;
1772
1773 memset(&user, 0, sizeof(struct gsm_mncc));
1774 user.callref = trans->callref;
1775 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1776 /* user-user */
1777 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1778 user.fields |= MNCC_F_USERUSER;
1779 gsm48_decode_useruser(&user.useruser,
1780 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1781 }
1782 /* more data */
1783 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1784 user.more = 1;
1785
1786 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1787}
1788
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001789static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1790 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001791 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001792{
1793 uint8_t data[sizeof(struct gsm_mncc)];
1794 struct gsm_mncc_rtp *rtp;
1795
1796 memset(&data, 0, sizeof(data));
1797 rtp = (struct gsm_mncc_rtp *) &data[0];
1798
1799 rtp->callref = callref;
1800 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001801 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001802 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1803 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001804 }
Harald Welte27989d42018-06-21 20:39:20 +02001805 rtp->payload_type = payload_type;
1806 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001807 if (sdp) {
1808 LOG_TRANS(trans, LOGL_DEBUG, "%s SDP: %s\n", get_mncc_name(rtp->msg_type), sdp_msg_to_str(sdp));
1809 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
1810 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001811 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001812}
1813
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001814static 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 +02001815{
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001816 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001817}
1818
1819static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1820{
1821 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001822
1823 /* Find callref */
1824 trans = trans_find_by_callref(net, callref);
1825 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001826 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001827 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001828 return -EIO;
1829 }
1830 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001831 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001832 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001833 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001834 return 0;
1835 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001836 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001837
Harald Welte27989d42018-06-21 20:39:20 +02001838 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001839 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001840}
1841
1842/* Trigger TCH_RTP_CREATE acknowledgement */
1843int gsm48_tch_rtp_create(struct gsm_trans *trans)
1844{
1845 /* This function is called as soon as the port, on which the
1846 * mgcp-gw expects the incoming RTP stream from the remote
1847 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001848 struct msc_a *msc_a = trans->msc_a;
1849 struct gsm_network *net = msc_a_net(msc_a);
1850 struct call_leg *cl = msc_a->cc.call_leg;
1851 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001852 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001853 int mncc_payload_msg_type;
1854 struct sdp_audio_codec *codec;
1855 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001856
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001857 if (!rtp_cn) {
1858 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1859 return -EINVAL;
1860 }
1861
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001862 codec_filter_run(&trans->cc.codecs);
1863 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1864
1865 if (!trans->cc.codecs.result.audio_codecs.count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001866 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001867 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001868 return -EINVAL;
1869 }
1870
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001871 /* Modify the MGW endpoint if necessary, usually this should already match and not cause MGCP. */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001872 rtp_stream_set_codecs(rtp_cn, &trans->cc.codecs.result.audio_codecs);
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001873 rtp_stream_commit(rtp_cn);
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001874
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001875 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001876 codec = &rtp_cn->codecs.codec[0];
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001877 m = codec_mapping_by_subtype_name(codec->subtype_name);
1878 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02001879
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001880 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1881 if (!rtp_cn_local) {
1882 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1883 return -EINVAL;
1884 }
1885
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001886 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001887 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001888}
1889
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001890static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001891{
1892 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001893 struct call_leg *cl;
1894 struct rtp_stream *rtps;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001895 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001896
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001897 /* FIXME: in *rtp we should get the codec information of the remote
1898 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1899 * meaningful value based on this information but unfortunately we
1900 * can't do that yet because the mncc API can not signal dynamic
1901 * payload types yet. This must be fixed first. Also there may be
1902 * additional members necessary in trans->conn->rtp because we
1903 * somehow need to deal with dynamic payload types that do not
1904 * comply to 3gpp's assumptions of payload type numbers on the A
1905 * interface. See also related tickets: OS#3399 and OS1683 */
1906
Harald Welte27989d42018-06-21 20:39:20 +02001907 /* Find callref */
1908 trans = trans_find_by_callref(net, rtp->callref);
1909 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001910 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001911 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001912 return -EIO;
1913 }
1914 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001915 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001916 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001917 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001918 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001919 }
1920
Neels Hofmeyr90933d42022-01-13 20:10:52 +01001921 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 +02001922 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1923 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001924
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001925 cl = trans->msc_a->cc.call_leg;
1926 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1927
1928 if (!rtps) {
1929 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1930 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1931 return -EINVAL;
1932 }
1933
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001934 if (rtp->sdp[0]) {
1935 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, rtp->sdp);
1936 LOG_TRANS(trans, LOGL_DEBUG, "%s contained SDP %s\n",
1937 get_mncc_name(rtp->msg_type),
1938 sdp_msg_to_str(&trans->cc.codecs.remote));
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001939 }
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001940
1941 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
1942
1943 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
1944 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
1945 struct osmo_sockaddr_str rtp_addr;
1946 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1947 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1948 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1949 return -EINVAL;
1950 }
1951 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1952 }
1953
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001954 rtp_stream_commit(rtps);
1955 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001956}
1957
1958static struct downstate {
1959 uint32_t states;
1960 int type;
1961 int (*rout) (struct gsm_trans *trans, void *arg);
1962} downstatelist[] = {
1963 /* mobile originating call establishment */
1964 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1965 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1966 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1967 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1968 {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 */
1969 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1970 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1971 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1972 /* mobile terminating call establishment */
1973 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1974 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1975 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1976 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1977 /* signalling during call */
1978 {SBIT(GSM_CSTATE_ACTIVE),
1979 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1980 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1981 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1982 {ALL_STATES,
1983 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1984 {ALL_STATES,
1985 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1986 {ALL_STATES,
1987 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1988 {SBIT(GSM_CSTATE_ACTIVE),
1989 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1990 {SBIT(GSM_CSTATE_ACTIVE),
1991 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1992 {SBIT(GSM_CSTATE_ACTIVE),
1993 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1994 {SBIT(GSM_CSTATE_ACTIVE),
1995 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1996 {SBIT(GSM_CSTATE_ACTIVE),
1997 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1998 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1999 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2000 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2001 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2002 {SBIT(GSM_CSTATE_ACTIVE),
2003 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2004 /* clearing */
2005 {SBIT(GSM_CSTATE_INITIATED),
2006 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2007 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2008 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2009 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2010 MNCC_REL_REQ, gsm48_cc_tx_release},
2011};
2012
2013#define DOWNSLLEN \
2014 (sizeof(downstatelist) / sizeof(struct downstate))
2015
2016
Philipp Maiercd64af72019-08-01 09:46:40 +02002017static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002018{
2019 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002020 struct msc_a *msc_a = NULL;
2021 struct gsm_trans *trans = NULL;
2022 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002023
Harald Welte27989d42018-06-21 20:39:20 +02002024 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002025 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002026 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002027 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002028 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002029 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002030 return rc;
2031 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002032 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02002033 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002034 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002035 case MNCC_RTP_FREE:
2036 /* unused right now */
2037 return -EIO;
2038
2039 case MNCC_FRAME_DROP:
2040 case MNCC_FRAME_RECV:
2041 case GSM_TCHF_FRAME:
2042 case GSM_TCHF_FRAME_EFR:
2043 case GSM_TCHH_FRAME:
2044 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002045 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002046 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002047 return -ENOTSUP;
2048 }
2049
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002050 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002051
2052 /* Find callref */
2053 trans = trans_find_by_callref(net, data->callref);
2054
2055 /* Callref unknown */
2056 if (!trans) {
2057 struct vlr_subscr *vsub;
2058
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002059 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002060 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002061 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002062 /* Invalid call reference */
2063 return mncc_release_ind(net, NULL, data->callref,
2064 GSM48_CAUSE_LOC_PRN_S_LU,
2065 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2066 }
2067 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002068 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002069 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002070 /* Invalid number */
2071 return mncc_release_ind(net, NULL, data->callref,
2072 GSM48_CAUSE_LOC_PRN_S_LU,
2073 GSM48_CC_CAUSE_INV_NR_FORMAT);
2074 }
2075 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002076 if (data->called.number[0]) {
2077 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2078 if (!vsub)
2079 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002080 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002081 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002082 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002083 if (!vsub)
2084 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002085 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002086 }
2087 if (!vsub)
2088 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002089 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002090 /* update the subscriber we deal with */
2091 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2092
Harald Welte27989d42018-06-21 20:39:20 +02002093 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002094 if (!vsub->lu_complete) {
2095 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002096 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002097 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002098 /* Temporarily out of order */
2099 return mncc_release_ind(net, NULL, data->callref,
2100 GSM48_CAUSE_LOC_PRN_S_LU,
2101 GSM48_CC_CAUSE_DEST_OOO);
2102 }
Keith Whyte991bb422019-08-08 15:43:40 +02002103
2104 /* Find valid conn */
2105 msc_a = msc_a_for_vsub(vsub, true);
2106
2107 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2108 if (!net->call_waiting && msc_a) {
2109 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2110 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2111 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2112 "rx '%s' for subscriber %s with trans state (%s)"
2113 " rejecting with USER_BUSY\n",
2114 get_mncc_name(msg->msg_type), data->called.number,
2115 gsm48_cc_state_name(existing_cc_trans->cc.state));
2116 return mncc_release_ind(net, NULL, data->callref,
2117 GSM48_CAUSE_LOC_PRN_S_LU,
2118 GSM48_CC_CAUSE_USER_BUSY);
2119 }
2120 }
2121
Harald Welte27989d42018-06-21 20:39:20 +02002122 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002123 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002124 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002125 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002126 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002127 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002128 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002129 mncc_release_ind(net, NULL, data->callref,
2130 GSM48_CAUSE_LOC_PRN_S_LU,
2131 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2132 return -ENOMEM;
2133 }
2134
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002135 /* Remember remote SDP, if any */
2136 if (data->sdp[0]) {
2137 if (sdp_msg_from_sdp_str(&trans->cc.codecs.remote, data->sdp)) {
2138 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse incoming SDP: %s\n",
2139 osmo_quote_str(data->sdp, -1));
2140 vlr_subscr_put(vsub, __func__);
2141 mncc_release_ind(net, NULL, data->callref,
2142 GSM48_CAUSE_LOC_PRN_S_LU,
2143 GSM48_CC_CAUSE_NORMAL_UNSPEC);
2144 return -EINVAL;
2145 }
2146 }
2147
Harald Welte27989d42018-06-21 20:39:20 +02002148 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002149 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002150 /* This condition will return before the common logging of the received MNCC message below, so
2151 * log it now. */
2152 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002153
Harald Welte27989d42018-06-21 20:39:20 +02002154 /* store setup information until paging succeeds */
2155 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2156
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002157 /* Request a channel. If Paging already started, paging_request_start() will append the new
2158 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002159 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2160 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002161 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002162 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002163 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002164 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002165 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002166 return 0;
2167 }
2168
2169 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002170 trans->msc_a = msc_a;
2171 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002172 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002173 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002174 } else {
2175 /* update the subscriber we deal with */
2176 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2177 }
2178
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002179 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002180
Philipp Maier9ca7b312018-10-10 17:00:49 +02002181 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002182 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002183
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002184 if (trans->msc_a)
2185 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002186
2187 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002188 if (!msc_a) {
2189 struct gsm_mncc rel = {
2190 .callref = data->callref,
2191 };
2192 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002193 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2194 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002195 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002196 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2197 else
2198 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2199 trans->callref = 0;
2200 trans_free(trans);
2201 return rc;
2202 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002203 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002204 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002205 }
2206
2207 /* Find function for current state and message */
2208 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002209 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002210 && ((1 << trans->cc.state) & downstatelist[i].states))
2211 break;
2212 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002213 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002214 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002215 return 0;
2216 }
2217
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002218 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002219
2220 return rc;
2221}
2222
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002223struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2224{
2225 uint32_t callref;
2226
2227 switch (msg->msg_type) {
2228 case MNCC_BRIDGE:
2229 callref = msg->bridge.callref[0];
2230 break;
2231 case MNCC_RTP_CREATE:
2232 case MNCC_RTP_CONNECT:
2233 callref = msg->rtp.callref;
2234 break;
2235
2236 case MNCC_RTP_FREE:
2237 case MNCC_FRAME_DROP:
2238 case MNCC_FRAME_RECV:
2239 case GSM_TCHF_FRAME:
2240 case GSM_TCHF_FRAME_EFR:
2241 case GSM_TCHH_FRAME:
2242 case GSM_TCH_FRAME_AMR:
2243 return NULL;
2244
2245 default:
2246 callref = msg->signal.callref;
2247 break;
2248 }
2249
2250 return mncc_call_find_by_callref(callref);
2251}
2252
2253/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002254int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002255{
2256 const union mncc_msg *msg = arg;
2257 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002258
2259 if (msg->msg_type == MNCC_SETUP_REQ) {
2260 /* Incoming call to forward for inter-MSC Handover? */
2261 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2262 if (mncc_call)
2263 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2264 "Incoming call matches pending inter-MSC Handover Number\n");
2265 }
2266 if (!mncc_call) {
2267 /* Find already active MNCC FSM for this callref.
2268 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2269 * MNCC<->GSM-CC call handling. */
2270 mncc_call = mncc_find_by_callref_from_msg(msg);
2271 }
2272 if (mncc_call) {
2273 mncc_call_rx(mncc_call, msg);
2274 return 0;
2275 }
2276
2277 /* None of the above? Then it must be a normal GSM CC call related message. */
2278 return mncc_tx_to_gsm_cc(net, msg);
2279}
Harald Welte27989d42018-06-21 20:39:20 +02002280
2281static struct datastate {
2282 uint32_t states;
2283 int type;
2284 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2285} datastatelist[] = {
2286 /* mobile originating call establishment */
2287 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2288 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2289 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2290 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2291 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2292 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2293 /* mobile terminating call establishment */
2294 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2295 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2296 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2297 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2298 {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 */
2299 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2300 /* signalling during call */
2301 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2302 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2303 {SBIT(GSM_CSTATE_ACTIVE),
2304 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2305 {ALL_STATES,
2306 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2307 {ALL_STATES,
2308 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2309 {ALL_STATES,
2310 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2311 {SBIT(GSM_CSTATE_ACTIVE),
2312 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2313 {SBIT(GSM_CSTATE_ACTIVE),
2314 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2315 {SBIT(GSM_CSTATE_ACTIVE),
2316 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2317 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2318 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2319 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2320 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2321 {SBIT(GSM_CSTATE_ACTIVE),
2322 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2323 /* clearing */
2324 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2325 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2326 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2327 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2328 {ALL_STATES, /* 5.4.3.4 */
2329 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2330};
2331
2332#define DATASLLEN \
2333 (sizeof(datastatelist) / sizeof(struct datastate))
2334
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002335int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002336{
2337 struct gsm48_hdr *gh = msgb_l3(msg);
2338 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2339 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2340 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002341 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2342 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002343 int i, rc = 0;
2344
2345 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002346 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002347 return -EINVAL;
2348 }
2349
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002350 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002351 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002352 return -EINVAL;
2353 }
2354
2355 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002356 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002357
Harald Welte27989d42018-06-21 20:39:20 +02002358 /* Create transaction */
2359 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002360 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002361 trans = trans_alloc(net, vsub,
2362 TRANS_CC,
2363 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002364 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002365 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002366 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002367 GSM48_PDISC_CC | (transaction_id << 4),
2368 GSM48_MT_CC_RELEASE_COMPL);
2369 return -ENOMEM;
2370 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002371 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2372 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2373 trans_free(trans);
2374 return -EINVAL;
2375 }
2376
Harald Welte27989d42018-06-21 20:39:20 +02002377 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002378 msc_a_get(msc_a, MSC_A_USE_CC);
2379 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002380 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002381
2382 /* An earlier CM Service Request for this CC message now has concluded */
2383 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2384 LOG_MSC_A(msc_a, LOGL_ERROR,
2385 "Creating new CC transaction without prior CM Service Request\n");
2386 else
2387 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002388 }
2389
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002390 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2391 gsm48_cc_state_name(trans->cc.state));
2392
Harald Welte27989d42018-06-21 20:39:20 +02002393 /* find function for current state and message */
2394 for (i = 0; i < DATASLLEN; i++)
2395 if ((msg_type == datastatelist[i].type)
2396 && ((1 << trans->cc.state) & datastatelist[i].states))
2397 break;
2398 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002399 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002400
2401 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2402 * transaction right away. */
2403 if (trans->cc.state == GSM_CSTATE_NULL) {
2404 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2405 " -- disarding new CC transaction right away\n");
2406 trans_free(trans);
2407 }
Harald Welte27989d42018-06-21 20:39:20 +02002408 return 0;
2409 }
2410
2411 assert(trans->vsub);
2412
2413 rc = datastatelist[i].rout(trans, msg);
2414
Harald Welte27989d42018-06-21 20:39:20 +02002415 return rc;
2416}