blob: 328f1cf949e7690c424ed2eeb9f9458f4daedbdd [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;
667
668 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
669
670 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700671 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100672 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200673 "This is not allowed!\n");
674 /* Temporarily out of order */
675 rc = mncc_release_ind(trans->net, trans, trans->callref,
676 GSM48_CAUSE_LOC_PRN_S_LU,
677 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
678 trans->callref = 0;
679 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200680 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200681 return rc;
682 }
683
684 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100685 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200686 if (trans_id < 0) {
687 /* no free transaction ID */
688 rc = mncc_release_ind(trans->net, trans, trans->callref,
689 GSM48_CAUSE_LOC_PRN_S_LU,
690 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
691 trans->callref = 0;
692 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200693 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200694 return rc;
695 }
696 trans->transaction_id = trans_id;
697
698 gh->msg_type = GSM48_MT_CC_SETUP;
699
700 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
701
Neels Hofmeyra4195db2022-01-13 21:40:58 +0100702 /* MT call leg is starting. Gather all codecs information so far known.
703 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
704 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
705 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
706 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
707 * Capabilities. */
708 codec_filter_init(&trans->cc.codecs);
709 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
710 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
711 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
712 if (setup->sdp[0]) {
713 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
714 if (rc)
715 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
716 }
717 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
718 * MNCC, if any. */
719 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
720 trans->cc.codecs.remote = (struct sdp_msg){};
721 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
722 &setup->bearer_cap);
723 }
724 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
725 if (!trans->cc.codecs.remote.audio_codecs.count)
726 LOG_TRANS(trans, LOGL_ERROR,
727 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
728
Harald Welte27989d42018-06-21 20:39:20 +0200729 /* bearer capability */
730 if (setup->fields & MNCC_F_BEARER_CAP) {
731 /* Create a copy of the bearer capability in the transaction struct, so we
732 * can use this information later */
733 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
734 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
735 }
736 /* facility */
737 if (setup->fields & MNCC_F_FACILITY)
738 gsm48_encode_facility(msg, 0, &setup->facility);
739 /* progress */
740 if (setup->fields & MNCC_F_PROGRESS)
741 gsm48_encode_progress(msg, 0, &setup->progress);
742 /* calling party BCD number */
743 if (setup->fields & MNCC_F_CALLING)
744 gsm48_encode_calling(msg, &setup->calling);
745 /* called party BCD number */
746 if (setup->fields & MNCC_F_CALLED)
747 gsm48_encode_called(msg, &setup->called);
748 /* user-user */
749 if (setup->fields & MNCC_F_USERUSER)
750 gsm48_encode_useruser(msg, 0, &setup->useruser);
751 /* redirecting party BCD number */
752 if (setup->fields & MNCC_F_REDIRECTING)
753 gsm48_encode_redirecting(msg, &setup->redirecting);
754 /* signal */
755 if (setup->fields & MNCC_F_SIGNAL)
756 gsm48_encode_signal(msg, setup->signal);
757
758 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
759
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200760 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200761
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100762 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200763}
764
765static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
766{
767 struct gsm48_hdr *gh = msgb_l3(msg);
768 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
769 struct tlv_parsed tp;
770 struct gsm_mncc call_conf;
771 int rc;
772
773 gsm48_stop_cc_timer(trans);
774 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
775
776 memset(&call_conf, 0, sizeof(struct gsm_mncc));
777 call_conf.callref = trans->callref;
778
779 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
780#if 0
781 /* repeat */
782 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
783 call_conf.repeat = 1;
784 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
785 call_conf.repeat = 2;
786#endif
787 /* bearer capability */
788 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
789 call_conf.fields |= MNCC_F_BEARER_CAP;
790 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
791 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
792
793 /* Create a copy of the bearer capability
794 * in the transaction struct, so we can use
795 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100796 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200797 sizeof(trans->bearer_cap));
Neels Hofmeyrdb61f732022-01-13 19:59:02 +0100798
799 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
800 * Store in codecs filter. */
801 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200802 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100803
Harald Welte27989d42018-06-21 20:39:20 +0200804 /* cause */
805 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
806 call_conf.fields |= MNCC_F_CAUSE;
807 gsm48_decode_cause(&call_conf.cause,
808 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
809 }
810 /* cc cap */
811 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
812 call_conf.fields |= MNCC_F_CCCAP;
813 gsm48_decode_cccap(&call_conf.cccap,
814 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
815 }
816
817 /* IMSI of called subscriber */
818 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
819
820 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
821
822 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100823 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200824
825 /* don't continue, if there were problems with
826 * the call assignment. */
827 if (rc)
828 return rc;
829
830 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
831 &call_conf);
832}
833
834static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
835{
836 struct gsm_mncc *proceeding = arg;
837 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
838 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
839 int rc;
840
841 gh->msg_type = GSM48_MT_CC_CALL_PROC;
842
843 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
844
845 /* bearer capability */
846 if (proceeding->fields & MNCC_F_BEARER_CAP) {
847 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
848 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
849 }
850 /* facility */
851 if (proceeding->fields & MNCC_F_FACILITY)
852 gsm48_encode_facility(msg, 0, &proceeding->facility);
853 /* progress */
854 if (proceeding->fields & MNCC_F_PROGRESS)
855 gsm48_encode_progress(msg, 0, &proceeding->progress);
856
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100857 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200858 if (rc)
859 return rc;
860
861 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100862 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200863}
864
865static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
866{
867 struct gsm48_hdr *gh = msgb_l3(msg);
868 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
869 struct tlv_parsed tp;
870 struct gsm_mncc alerting;
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100871 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200872
873 gsm48_stop_cc_timer(trans);
874 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
875
876 memset(&alerting, 0, sizeof(struct gsm_mncc));
877 alerting.callref = trans->callref;
878 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
879 /* facility */
880 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
881 alerting.fields |= MNCC_F_FACILITY;
882 gsm48_decode_facility(&alerting.facility,
883 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
884 }
885
886 /* progress */
887 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
888 alerting.fields |= MNCC_F_PROGRESS;
889 gsm48_decode_progress(&alerting.progress,
890 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
891 }
892 /* ss-version */
893 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
894 alerting.fields |= MNCC_F_SSVERSION;
895 gsm48_decode_ssversion(&alerting.ssversion,
896 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
897 }
898
899 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
900
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100901 codec_filter_run(&trans->cc.codecs);
902 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
903 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
904 if (rc >= sizeof(alerting.sdp)) {
905 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
906 rc, sizeof(alerting.sdp));
907 trans_free(trans);
908 return -EINVAL;
909 }
910
Harald Welte27989d42018-06-21 20:39:20 +0200911 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
912 &alerting);
913}
914
915static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
916{
917 struct gsm_mncc *alerting = arg;
918 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
919 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
920
921 gh->msg_type = GSM48_MT_CC_ALERTING;
922
923 /* facility */
924 if (alerting->fields & MNCC_F_FACILITY)
925 gsm48_encode_facility(msg, 0, &alerting->facility);
926 /* progress */
927 if (alerting->fields & MNCC_F_PROGRESS)
928 gsm48_encode_progress(msg, 0, &alerting->progress);
929 /* user-user */
930 if (alerting->fields & MNCC_F_USERUSER)
931 gsm48_encode_useruser(msg, 0, &alerting->useruser);
932
933 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
934
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100935 if (alerting->sdp[0]) {
936 struct call_leg *cl = trans->msc_a->cc.call_leg;
937 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
938 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
939 codec_filter_run(&trans->cc.codecs);
940 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
941 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
942 if (rtp_cn) {
943 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
944 rtp_stream_commit(rtp_cn);
945 }
946 }
947
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100948 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200949}
950
951static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
952{
953 struct gsm_mncc *progress = arg;
954 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
955 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
956
957 gh->msg_type = GSM48_MT_CC_PROGRESS;
958
959 /* progress */
960 gsm48_encode_progress(msg, 1, &progress->progress);
961 /* user-user */
962 if (progress->fields & MNCC_F_USERUSER)
963 gsm48_encode_useruser(msg, 0, &progress->useruser);
964
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100965 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200966}
967
968static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
969{
970 struct gsm_mncc *connect = arg;
971 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
972 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
973
974 gh->msg_type = GSM48_MT_CC_CONNECT;
975
976 gsm48_stop_cc_timer(trans);
977 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
978
979 /* facility */
980 if (connect->fields & MNCC_F_FACILITY)
981 gsm48_encode_facility(msg, 0, &connect->facility);
982 /* progress */
983 if (connect->fields & MNCC_F_PROGRESS)
984 gsm48_encode_progress(msg, 0, &connect->progress);
985 /* connected number */
986 if (connect->fields & MNCC_F_CONNECTED)
987 gsm48_encode_connected(msg, &connect->connected);
988 /* user-user */
989 if (connect->fields & MNCC_F_USERUSER)
990 gsm48_encode_useruser(msg, 0, &connect->useruser);
991
992 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
993
Neels Hofmeyrc8111712022-01-13 20:04:12 +0100994 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
995 if (connect->sdp[0]) {
996 struct call_leg *cl = trans->msc_a->cc.call_leg;
997 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
998 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, connect->sdp);
999 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1000 get_mncc_name(connect->msg_type),
1001 codec_filter_to_str(&trans->cc.codecs));
1002 if (rtp_cn) {
1003 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1004 rtp_stream_commit(rtp_cn);
1005 }
1006 }
1007
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001008 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001009}
1010
1011static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1012{
1013 struct gsm48_hdr *gh = msgb_l3(msg);
1014 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1015 struct tlv_parsed tp;
1016 struct gsm_mncc connect;
1017
1018 gsm48_stop_cc_timer(trans);
1019
1020 memset(&connect, 0, sizeof(struct gsm_mncc));
1021 connect.callref = trans->callref;
1022 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1023 /* use subscriber as connected party number */
1024 connect.fields |= MNCC_F_CONNECTED;
1025 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1026 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1027
1028 /* facility */
1029 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1030 connect.fields |= MNCC_F_FACILITY;
1031 gsm48_decode_facility(&connect.facility,
1032 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1033 }
1034 /* user-user */
1035 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1036 connect.fields |= MNCC_F_USERUSER;
1037 gsm48_decode_useruser(&connect.useruser,
1038 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1039 }
1040 /* ss-version */
1041 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1042 connect.fields |= MNCC_F_SSVERSION;
1043 gsm48_decode_ssversion(&connect.ssversion,
1044 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1045 }
1046
1047 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001048 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001049
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001050 codec_filter_run(&trans->cc.codecs);
1051 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001052 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1053}
1054
1055
1056static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1057{
1058 struct gsm_mncc connect_ack;
1059
1060 gsm48_stop_cc_timer(trans);
1061
1062 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001063 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 +02001064
1065 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1066 connect_ack.callref = trans->callref;
1067
1068 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1069 &connect_ack);
1070}
1071
1072static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1073{
1074 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1075 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1076
1077 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1078
1079 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1080
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001081 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001082}
1083
1084static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1085{
1086 struct gsm48_hdr *gh = msgb_l3(msg);
1087 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1088 struct tlv_parsed tp;
1089 struct gsm_mncc disc;
1090
1091 gsm48_stop_cc_timer(trans);
1092
1093 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1094
1095 memset(&disc, 0, sizeof(struct gsm_mncc));
1096 disc.callref = trans->callref;
1097 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1098 /* cause */
1099 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1100 disc.fields |= MNCC_F_CAUSE;
1101 gsm48_decode_cause(&disc.cause,
1102 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1103 }
1104 /* facility */
1105 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1106 disc.fields |= MNCC_F_FACILITY;
1107 gsm48_decode_facility(&disc.facility,
1108 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1109 }
1110 /* user-user */
1111 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1112 disc.fields |= MNCC_F_USERUSER;
1113 gsm48_decode_useruser(&disc.useruser,
1114 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1115 }
1116 /* ss-version */
1117 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1118 disc.fields |= MNCC_F_SSVERSION;
1119 gsm48_decode_ssversion(&disc.ssversion,
1120 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1121 }
1122
1123 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001124}
1125
1126static struct gsm_mncc_cause default_cause = {
1127 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1128 .coding = 0,
1129 .rec = 0,
1130 .rec_val = 0,
1131 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1132 .diag_len = 0,
1133 .diag = { 0 },
1134};
1135
1136static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1137{
1138 struct gsm_mncc *disc = arg;
1139 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1140 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1141
1142 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1143
1144 gsm48_stop_cc_timer(trans);
1145 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1146
1147 /* cause */
1148 if (disc->fields & MNCC_F_CAUSE)
1149 gsm48_encode_cause(msg, 1, &disc->cause);
1150 else
1151 gsm48_encode_cause(msg, 1, &default_cause);
1152
1153 /* facility */
1154 if (disc->fields & MNCC_F_FACILITY)
1155 gsm48_encode_facility(msg, 0, &disc->facility);
1156 /* progress */
1157 if (disc->fields & MNCC_F_PROGRESS)
1158 gsm48_encode_progress(msg, 0, &disc->progress);
1159 /* user-user */
1160 if (disc->fields & MNCC_F_USERUSER)
1161 gsm48_encode_useruser(msg, 0, &disc->useruser);
1162
1163 /* store disconnect cause for T306 expiry */
1164 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1165
1166 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1167
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001168 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001169}
1170
1171static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1172{
1173 struct gsm48_hdr *gh = msgb_l3(msg);
1174 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1175 struct tlv_parsed tp;
1176 struct gsm_mncc rel;
1177 int rc;
1178
1179 gsm48_stop_cc_timer(trans);
1180
1181 memset(&rel, 0, sizeof(struct gsm_mncc));
1182 rel.callref = trans->callref;
1183 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1184 /* cause */
1185 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1186 rel.fields |= MNCC_F_CAUSE;
1187 gsm48_decode_cause(&rel.cause,
1188 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1189 }
1190 /* facility */
1191 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1192 rel.fields |= MNCC_F_FACILITY;
1193 gsm48_decode_facility(&rel.facility,
1194 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1195 }
1196 /* user-user */
1197 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1198 rel.fields |= MNCC_F_USERUSER;
1199 gsm48_decode_useruser(&rel.useruser,
1200 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1201 }
1202 /* ss-version */
1203 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1204 rel.fields |= MNCC_F_SSVERSION;
1205 gsm48_decode_ssversion(&rel.ssversion,
1206 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1207 }
1208
1209 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1210 /* release collision 5.4.5 */
1211 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1212 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001213 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001214 GSM48_PDISC_CC | (trans->transaction_id << 4),
1215 GSM48_MT_CC_RELEASE_COMPL);
1216 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1217 }
1218
1219 new_cc_state(trans, GSM_CSTATE_NULL);
1220
1221 trans->callref = 0;
1222 trans_free(trans);
1223
1224 return rc;
1225}
1226
1227static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1228{
1229 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001230 struct msgb *msg;
1231 struct gsm48_hdr *gh;
1232
1233 if (!trans->msc_a) {
1234 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1235 return -EINVAL;
1236 }
1237
1238 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1239 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001240
1241 gh->msg_type = GSM48_MT_CC_RELEASE;
1242
1243 gsm48_stop_cc_timer(trans);
1244 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1245
1246 /* cause */
1247 if (rel->fields & MNCC_F_CAUSE)
1248 gsm48_encode_cause(msg, 0, &rel->cause);
1249 /* facility */
1250 if (rel->fields & MNCC_F_FACILITY)
1251 gsm48_encode_facility(msg, 0, &rel->facility);
1252 /* user-user */
1253 if (rel->fields & MNCC_F_USERUSER)
1254 gsm48_encode_useruser(msg, 0, &rel->useruser);
1255
1256 trans->cc.T308_second = 0;
1257 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1258
1259 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1260 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1261
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001262 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001263}
1264
1265static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1266{
1267 struct gsm48_hdr *gh = msgb_l3(msg);
1268 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1269 struct tlv_parsed tp;
1270 struct gsm_mncc rel;
1271 int rc = 0;
1272
1273 gsm48_stop_cc_timer(trans);
1274
1275 memset(&rel, 0, sizeof(struct gsm_mncc));
1276 rel.callref = trans->callref;
1277 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1278 /* cause */
1279 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1280 rel.fields |= MNCC_F_CAUSE;
1281 gsm48_decode_cause(&rel.cause,
1282 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1283 }
1284 /* facility */
1285 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1286 rel.fields |= MNCC_F_FACILITY;
1287 gsm48_decode_facility(&rel.facility,
1288 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1289 }
1290 /* user-user */
1291 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1292 rel.fields |= MNCC_F_USERUSER;
1293 gsm48_decode_useruser(&rel.useruser,
1294 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1295 }
1296 /* ss-version */
1297 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1298 rel.fields |= MNCC_F_SSVERSION;
1299 gsm48_decode_ssversion(&rel.ssversion,
1300 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1301 }
1302
1303 if (trans->callref) {
1304 switch (trans->cc.state) {
1305 case GSM_CSTATE_CALL_PRESENT:
1306 rc = mncc_recvmsg(trans->net, trans,
1307 MNCC_REJ_IND, &rel);
1308 break;
1309 case GSM_CSTATE_RELEASE_REQ:
1310 rc = mncc_recvmsg(trans->net, trans,
1311 MNCC_REL_CNF, &rel);
1312 break;
1313 default:
1314 rc = mncc_recvmsg(trans->net, trans,
1315 MNCC_REL_IND, &rel);
1316 }
1317 }
1318
1319 trans->callref = 0;
1320 trans_free(trans);
1321
1322 return rc;
1323}
1324
1325static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1326{
1327 struct gsm_mncc *rel = arg;
1328 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1329 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1330 int ret;
1331
1332 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1333
1334 trans->callref = 0;
1335
1336 gsm48_stop_cc_timer(trans);
1337
1338 /* cause */
1339 if (rel->fields & MNCC_F_CAUSE)
1340 gsm48_encode_cause(msg, 0, &rel->cause);
1341 /* facility */
1342 if (rel->fields & MNCC_F_FACILITY)
1343 gsm48_encode_facility(msg, 0, &rel->facility);
1344 /* user-user */
1345 if (rel->fields & MNCC_F_USERUSER)
1346 gsm48_encode_useruser(msg, 0, &rel->useruser);
1347
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001348 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001349
1350 trans_free(trans);
1351
1352 return ret;
1353}
1354
1355static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1356{
1357 struct gsm48_hdr *gh = msgb_l3(msg);
1358 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1359 struct tlv_parsed tp;
1360 struct gsm_mncc fac;
1361
1362 memset(&fac, 0, sizeof(struct gsm_mncc));
1363 fac.callref = trans->callref;
1364 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1365 /* facility */
1366 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1367 fac.fields |= MNCC_F_FACILITY;
1368 gsm48_decode_facility(&fac.facility,
1369 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1370 }
1371 /* ss-version */
1372 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1373 fac.fields |= MNCC_F_SSVERSION;
1374 gsm48_decode_ssversion(&fac.ssversion,
1375 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1376 }
1377
1378 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1379}
1380
1381static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1382{
1383 struct gsm_mncc *fac = arg;
1384 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1385 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1386
1387 gh->msg_type = GSM48_MT_CC_FACILITY;
1388
1389 /* facility */
1390 gsm48_encode_facility(msg, 1, &fac->facility);
1391
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001392 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001393}
1394
1395static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1396{
1397 struct gsm_mncc hold;
1398
1399 memset(&hold, 0, sizeof(struct gsm_mncc));
1400 hold.callref = trans->callref;
1401 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1402}
1403
1404static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1405{
1406 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1407 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1408
1409 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1410
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001411 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001412}
1413
1414static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1415{
1416 struct gsm_mncc *hold_rej = arg;
1417 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1418 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1419
1420 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1421
1422 /* cause */
1423 if (hold_rej->fields & MNCC_F_CAUSE)
1424 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1425 else
1426 gsm48_encode_cause(msg, 1, &default_cause);
1427
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001428 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001429}
1430
1431static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1432{
1433 struct gsm_mncc retrieve;
1434
1435 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1436 retrieve.callref = trans->callref;
1437 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1438 &retrieve);
1439}
1440
1441static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1442{
1443 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1444 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1445
1446 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1447
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001448 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001449}
1450
1451static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1452{
1453 struct gsm_mncc *retrieve_rej = arg;
1454 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1455 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1456
1457 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1458
1459 /* cause */
1460 if (retrieve_rej->fields & MNCC_F_CAUSE)
1461 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1462 else
1463 gsm48_encode_cause(msg, 1, &default_cause);
1464
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001465 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001466}
1467
1468static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1469{
1470 struct gsm48_hdr *gh = msgb_l3(msg);
1471 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1472 struct tlv_parsed tp;
1473 struct gsm_mncc dtmf;
1474
1475 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1476 dtmf.callref = trans->callref;
1477 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1478 /* keypad facility */
1479 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1480 dtmf.fields |= MNCC_F_KEYPAD;
1481 gsm48_decode_keypad(&dtmf.keypad,
1482 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1483 }
1484
1485 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1486}
1487
1488static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1489{
1490 struct gsm_mncc *dtmf = arg;
1491 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1492 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1493
1494 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1495
1496 /* keypad */
1497 if (dtmf->fields & MNCC_F_KEYPAD)
1498 gsm48_encode_keypad(msg, dtmf->keypad);
1499
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001500 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001501}
1502
1503static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1504{
1505 struct gsm_mncc *dtmf = arg;
1506 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1507 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1508
1509 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1510
1511 /* cause */
1512 if (dtmf->fields & MNCC_F_CAUSE)
1513 gsm48_encode_cause(msg, 1, &dtmf->cause);
1514 else
1515 gsm48_encode_cause(msg, 1, &default_cause);
1516
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001517 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001518}
1519
1520static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1521{
1522 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1524
1525 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1526
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001527 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001528}
1529
1530static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1531{
1532 struct gsm_mncc dtmf;
1533
1534 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1535 dtmf.callref = trans->callref;
1536
1537 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1538}
1539
1540static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1541{
1542 struct gsm48_hdr *gh = msgb_l3(msg);
1543 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1544 struct tlv_parsed tp;
1545 struct gsm_mncc modify;
1546
1547 memset(&modify, 0, sizeof(struct gsm_mncc));
1548 modify.callref = trans->callref;
1549 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1550 /* bearer capability */
1551 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1552 modify.fields |= MNCC_F_BEARER_CAP;
1553 gsm48_decode_bearer_cap(&modify.bearer_cap,
1554 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1555
1556 /* Create a copy of the bearer capability
1557 * in the transaction struct, so we can use
1558 * this information later */
1559 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1560 sizeof(trans->bearer_cap));
1561 }
1562
1563 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1564
1565 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1566}
1567
1568static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1569{
1570 struct gsm_mncc *modify = arg;
1571 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1572 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1573
1574 gh->msg_type = GSM48_MT_CC_MODIFY;
1575
1576 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1577
1578 /* bearer capability */
1579 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1580 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1581
1582 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1583
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001584 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001585}
1586
1587static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1588{
1589 struct gsm48_hdr *gh = msgb_l3(msg);
1590 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1591 struct tlv_parsed tp;
1592 struct gsm_mncc modify;
1593
1594 gsm48_stop_cc_timer(trans);
1595
1596 memset(&modify, 0, sizeof(struct gsm_mncc));
1597 modify.callref = trans->callref;
1598 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1599 /* bearer capability */
1600 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1601 modify.fields |= MNCC_F_BEARER_CAP;
1602 gsm48_decode_bearer_cap(&modify.bearer_cap,
1603 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1604
1605 /* Create a copy of the bearer capability
1606 * in the transaction struct, so we can use
1607 * this information later */
1608 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1609 sizeof(trans->bearer_cap));
1610 }
1611
1612 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1613
1614 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1615}
1616
1617static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1618{
1619 struct gsm_mncc *modify = arg;
1620 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1621 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1622
1623 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1624
1625 /* bearer capability */
1626 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1627 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1628
1629 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1630
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001631 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001632}
1633
1634static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1635{
1636 struct gsm48_hdr *gh = msgb_l3(msg);
1637 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1638 struct tlv_parsed tp;
1639 struct gsm_mncc modify;
1640
1641 gsm48_stop_cc_timer(trans);
1642
1643 memset(&modify, 0, sizeof(struct gsm_mncc));
1644 modify.callref = trans->callref;
1645 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1646 /* bearer capability */
1647 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1648 modify.fields |= GSM48_IE_BEARER_CAP;
1649 gsm48_decode_bearer_cap(&modify.bearer_cap,
1650 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1651
1652 /* Create a copy of the bearer capability
1653 * in the transaction struct, so we can use
1654 * this information later */
1655 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1656 sizeof(trans->bearer_cap));
1657 }
1658 /* cause */
1659 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1660 modify.fields |= MNCC_F_CAUSE;
1661 gsm48_decode_cause(&modify.cause,
1662 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1663 }
1664
1665 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1666
1667 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1668}
1669
1670static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1671{
1672 struct gsm_mncc *modify = arg;
1673 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1674 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1675
1676 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1677
1678 /* bearer capability */
1679 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1680 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1681 /* cause */
1682 gsm48_encode_cause(msg, 1, &modify->cause);
1683
1684 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1685
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001686 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001687}
1688
1689static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1690{
1691 struct gsm_mncc *notify = arg;
1692 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1693 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1694
1695 gh->msg_type = GSM48_MT_CC_NOTIFY;
1696
1697 /* notify */
1698 gsm48_encode_notify(msg, notify->notify);
1699
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001700 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001701}
1702
1703static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1704{
1705 struct gsm48_hdr *gh = msgb_l3(msg);
1706 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1707// struct tlv_parsed tp;
1708 struct gsm_mncc notify;
1709
1710 memset(&notify, 0, sizeof(struct gsm_mncc));
1711 notify.callref = trans->callref;
1712// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1713 if (payload_len >= 1)
1714 gsm48_decode_notify(&notify.notify, gh->data);
1715
1716 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1717}
1718
1719static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1720{
1721 struct gsm_mncc *user = arg;
1722 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1723 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1724
1725 gh->msg_type = GSM48_MT_CC_USER_INFO;
1726
1727 /* user-user */
1728 if (user->fields & MNCC_F_USERUSER)
1729 gsm48_encode_useruser(msg, 1, &user->useruser);
1730 /* more data */
1731 if (user->more)
1732 gsm48_encode_more(msg);
1733
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001734 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001735}
1736
1737static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1738{
1739 struct gsm48_hdr *gh = msgb_l3(msg);
1740 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1741 struct tlv_parsed tp;
1742 struct gsm_mncc user;
1743
1744 memset(&user, 0, sizeof(struct gsm_mncc));
1745 user.callref = trans->callref;
1746 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1747 /* user-user */
1748 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1749 user.fields |= MNCC_F_USERUSER;
1750 gsm48_decode_useruser(&user.useruser,
1751 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1752 }
1753 /* more data */
1754 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1755 user.more = 1;
1756
1757 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1758}
1759
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001760static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1761 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001762 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001763{
1764 uint8_t data[sizeof(struct gsm_mncc)];
1765 struct gsm_mncc_rtp *rtp;
1766
1767 memset(&data, 0, sizeof(data));
1768 rtp = (struct gsm_mncc_rtp *) &data[0];
1769
1770 rtp->callref = callref;
1771 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001772 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001773 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1774 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001775 }
Harald Welte27989d42018-06-21 20:39:20 +02001776 rtp->payload_type = payload_type;
1777 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001778 if (sdp) {
1779 LOG_TRANS(trans, LOGL_DEBUG, "%s SDP: %s\n", get_mncc_name(rtp->msg_type), sdp_msg_to_str(sdp));
1780 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
1781 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001782 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001783}
1784
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001785static 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 +02001786{
Neels Hofmeyr714a28a2022-01-13 20:06:53 +01001787 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001788}
1789
1790static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1791{
1792 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001793
1794 /* Find callref */
1795 trans = trans_find_by_callref(net, callref);
1796 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001797 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001798 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001799 return -EIO;
1800 }
1801 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001802 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001803 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001804 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001805 return 0;
1806 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001807 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001808
Harald Welte27989d42018-06-21 20:39:20 +02001809 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001810 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001811}
1812
1813/* Trigger TCH_RTP_CREATE acknowledgement */
1814int gsm48_tch_rtp_create(struct gsm_trans *trans)
1815{
1816 /* This function is called as soon as the port, on which the
1817 * mgcp-gw expects the incoming RTP stream from the remote
1818 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001819 struct msc_a *msc_a = trans->msc_a;
1820 struct gsm_network *net = msc_a_net(msc_a);
1821 struct call_leg *cl = msc_a->cc.call_leg;
1822 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001823 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001824 int mncc_payload_msg_type;
1825 struct sdp_audio_codec *codec;
1826 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001827
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001828 if (!rtp_cn) {
1829 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1830 return -EINVAL;
1831 }
1832
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001833 codec_filter_run(&trans->cc.codecs);
1834 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1835
1836 if (!trans->cc.codecs.result.audio_codecs.count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001837 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001838 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001839 return -EINVAL;
1840 }
1841
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001842 /* Modify the MGW endpoint if necessary, usually this should already match and not cause MGCP. */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001843 rtp_stream_set_codecs(rtp_cn, &trans->cc.codecs.result.audio_codecs);
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001844 rtp_stream_commit(rtp_cn);
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001845
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001846 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
Neels Hofmeyr882c6692022-07-26 13:31:46 +02001847 codec = &rtp_cn->codecs.codec[0];
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001848 m = codec_mapping_by_subtype_name(codec->subtype_name);
1849 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02001850
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001851 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1852 if (!rtp_cn_local) {
1853 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1854 return -EINVAL;
1855 }
1856
Neels Hofmeyrca90cc92022-01-13 18:48:32 +01001857 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001858 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001859}
1860
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001861static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001862{
1863 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001864 struct call_leg *cl;
1865 struct rtp_stream *rtps;
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001866 char ipbuf[INET6_ADDRSTRLEN];
Harald Welte27989d42018-06-21 20:39:20 +02001867
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001868 /* FIXME: in *rtp we should get the codec information of the remote
1869 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1870 * meaningful value based on this information but unfortunately we
1871 * can't do that yet because the mncc API can not signal dynamic
1872 * payload types yet. This must be fixed first. Also there may be
1873 * additional members necessary in trans->conn->rtp because we
1874 * somehow need to deal with dynamic payload types that do not
1875 * comply to 3gpp's assumptions of payload type numbers on the A
1876 * interface. See also related tickets: OS#3399 and OS1683 */
1877
Harald Welte27989d42018-06-21 20:39:20 +02001878 /* Find callref */
1879 trans = trans_find_by_callref(net, rtp->callref);
1880 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001881 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001882 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001883 return -EIO;
1884 }
1885 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001886 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001887 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001888 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001889 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001890 }
1891
Neels Hofmeyr90933d42022-01-13 20:10:52 +01001892 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 +02001893 osmo_sockaddr_ntop((const struct sockaddr*)&rtp->addr, ipbuf),
1894 osmo_sockaddr_port((const struct sockaddr*)&rtp->addr));
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001895
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001896 cl = trans->msc_a->cc.call_leg;
1897 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1898
1899 if (!rtps) {
1900 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1901 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1902 return -EINVAL;
1903 }
1904
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001905 if (rtp->sdp[0]) {
1906 sdp_msg_from_sdp_str(&trans->cc.codecs.remote, rtp->sdp);
1907 LOG_TRANS(trans, LOGL_DEBUG, "%s contained SDP %s\n",
1908 get_mncc_name(rtp->msg_type),
1909 sdp_msg_to_str(&trans->cc.codecs.remote));
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001910 }
Neels Hofmeyrc8111712022-01-13 20:04:12 +01001911
1912 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
1913
1914 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
1915 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
1916 struct osmo_sockaddr_str rtp_addr;
1917 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1918 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1919 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1920 return -EINVAL;
1921 }
1922 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1923 }
1924
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001925 rtp_stream_commit(rtps);
1926 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001927}
1928
1929static struct downstate {
1930 uint32_t states;
1931 int type;
1932 int (*rout) (struct gsm_trans *trans, void *arg);
1933} downstatelist[] = {
1934 /* mobile originating call establishment */
1935 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1936 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1937 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1938 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1939 {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 */
1940 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1941 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1942 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1943 /* mobile terminating call establishment */
1944 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1945 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1946 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1947 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1948 /* signalling during call */
1949 {SBIT(GSM_CSTATE_ACTIVE),
1950 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1951 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1952 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1953 {ALL_STATES,
1954 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1955 {ALL_STATES,
1956 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1957 {ALL_STATES,
1958 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1959 {SBIT(GSM_CSTATE_ACTIVE),
1960 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1961 {SBIT(GSM_CSTATE_ACTIVE),
1962 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1963 {SBIT(GSM_CSTATE_ACTIVE),
1964 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1965 {SBIT(GSM_CSTATE_ACTIVE),
1966 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1967 {SBIT(GSM_CSTATE_ACTIVE),
1968 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1969 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1970 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1971 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1972 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1973 {SBIT(GSM_CSTATE_ACTIVE),
1974 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1975 /* clearing */
1976 {SBIT(GSM_CSTATE_INITIATED),
1977 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1978 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1979 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1980 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1981 MNCC_REL_REQ, gsm48_cc_tx_release},
1982};
1983
1984#define DOWNSLLEN \
1985 (sizeof(downstatelist) / sizeof(struct downstate))
1986
1987
Philipp Maiercd64af72019-08-01 09:46:40 +02001988static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001989{
1990 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001991 struct msc_a *msc_a = NULL;
1992 struct gsm_trans *trans = NULL;
1993 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001994
Harald Welte27989d42018-06-21 20:39:20 +02001995 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001996 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001997 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001998 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001999 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002000 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002001 return rc;
2002 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002003 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02002004 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002005 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002006 case MNCC_RTP_FREE:
2007 /* unused right now */
2008 return -EIO;
2009
2010 case MNCC_FRAME_DROP:
2011 case MNCC_FRAME_RECV:
2012 case GSM_TCHF_FRAME:
2013 case GSM_TCHF_FRAME_EFR:
2014 case GSM_TCHH_FRAME:
2015 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002016 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002017 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002018 return -ENOTSUP;
2019 }
2020
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002021 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002022
2023 /* Find callref */
2024 trans = trans_find_by_callref(net, data->callref);
2025
2026 /* Callref unknown */
2027 if (!trans) {
2028 struct vlr_subscr *vsub;
2029
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002030 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002031 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002032 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002033 /* Invalid call reference */
2034 return mncc_release_ind(net, NULL, data->callref,
2035 GSM48_CAUSE_LOC_PRN_S_LU,
2036 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2037 }
2038 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002039 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002040 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002041 /* Invalid number */
2042 return mncc_release_ind(net, NULL, data->callref,
2043 GSM48_CAUSE_LOC_PRN_S_LU,
2044 GSM48_CC_CAUSE_INV_NR_FORMAT);
2045 }
2046 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002047 if (data->called.number[0]) {
2048 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2049 if (!vsub)
2050 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002051 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002052 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002053 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002054 if (!vsub)
2055 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002056 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002057 }
2058 if (!vsub)
2059 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002060 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002061 /* update the subscriber we deal with */
2062 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2063
Harald Welte27989d42018-06-21 20:39:20 +02002064 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002065 if (!vsub->lu_complete) {
2066 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002067 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002068 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002069 /* Temporarily out of order */
2070 return mncc_release_ind(net, NULL, data->callref,
2071 GSM48_CAUSE_LOC_PRN_S_LU,
2072 GSM48_CC_CAUSE_DEST_OOO);
2073 }
Keith Whyte991bb422019-08-08 15:43:40 +02002074
2075 /* Find valid conn */
2076 msc_a = msc_a_for_vsub(vsub, true);
2077
2078 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2079 if (!net->call_waiting && msc_a) {
2080 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2081 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2082 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2083 "rx '%s' for subscriber %s with trans state (%s)"
2084 " rejecting with USER_BUSY\n",
2085 get_mncc_name(msg->msg_type), data->called.number,
2086 gsm48_cc_state_name(existing_cc_trans->cc.state));
2087 return mncc_release_ind(net, NULL, data->callref,
2088 GSM48_CAUSE_LOC_PRN_S_LU,
2089 GSM48_CC_CAUSE_USER_BUSY);
2090 }
2091 }
2092
Harald Welte27989d42018-06-21 20:39:20 +02002093 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002094 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002095 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002096 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002097 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002098 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002099 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002100 mncc_release_ind(net, NULL, data->callref,
2101 GSM48_CAUSE_LOC_PRN_S_LU,
2102 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2103 return -ENOMEM;
2104 }
2105
Neels Hofmeyrc8111712022-01-13 20:04:12 +01002106 /* Remember remote SDP, if any */
2107 if (data->sdp[0]) {
2108 if (sdp_msg_from_sdp_str(&trans->cc.codecs.remote, data->sdp)) {
2109 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse incoming SDP: %s\n",
2110 osmo_quote_str(data->sdp, -1));
2111 vlr_subscr_put(vsub, __func__);
2112 mncc_release_ind(net, NULL, data->callref,
2113 GSM48_CAUSE_LOC_PRN_S_LU,
2114 GSM48_CC_CAUSE_NORMAL_UNSPEC);
2115 return -EINVAL;
2116 }
2117 }
2118
Harald Welte27989d42018-06-21 20:39:20 +02002119 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002120 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002121 /* This condition will return before the common logging of the received MNCC message below, so
2122 * log it now. */
2123 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002124
Harald Welte27989d42018-06-21 20:39:20 +02002125 /* store setup information until paging succeeds */
2126 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2127
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002128 /* Request a channel. If Paging already started, paging_request_start() will append the new
2129 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002130 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2131 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002132 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002133 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002134 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002135 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002136 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002137 return 0;
2138 }
2139
2140 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002141 trans->msc_a = msc_a;
2142 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002143 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002144 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002145 } else {
2146 /* update the subscriber we deal with */
2147 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2148 }
2149
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002150 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002151
Philipp Maier9ca7b312018-10-10 17:00:49 +02002152 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002153 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002154
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002155 if (trans->msc_a)
2156 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002157
2158 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002159 if (!msc_a) {
2160 struct gsm_mncc rel = {
2161 .callref = data->callref,
2162 };
2163 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002164 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2165 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002166 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002167 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2168 else
2169 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2170 trans->callref = 0;
2171 trans_free(trans);
2172 return rc;
2173 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002174 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002175 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002176 }
2177
2178 /* Find function for current state and message */
2179 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002180 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002181 && ((1 << trans->cc.state) & downstatelist[i].states))
2182 break;
2183 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002184 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002185 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002186 return 0;
2187 }
2188
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002189 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002190
2191 return rc;
2192}
2193
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002194struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2195{
2196 uint32_t callref;
2197
2198 switch (msg->msg_type) {
2199 case MNCC_BRIDGE:
2200 callref = msg->bridge.callref[0];
2201 break;
2202 case MNCC_RTP_CREATE:
2203 case MNCC_RTP_CONNECT:
2204 callref = msg->rtp.callref;
2205 break;
2206
2207 case MNCC_RTP_FREE:
2208 case MNCC_FRAME_DROP:
2209 case MNCC_FRAME_RECV:
2210 case GSM_TCHF_FRAME:
2211 case GSM_TCHF_FRAME_EFR:
2212 case GSM_TCHH_FRAME:
2213 case GSM_TCH_FRAME_AMR:
2214 return NULL;
2215
2216 default:
2217 callref = msg->signal.callref;
2218 break;
2219 }
2220
2221 return mncc_call_find_by_callref(callref);
2222}
2223
2224/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002225int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002226{
2227 const union mncc_msg *msg = arg;
2228 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002229
2230 if (msg->msg_type == MNCC_SETUP_REQ) {
2231 /* Incoming call to forward for inter-MSC Handover? */
2232 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2233 if (mncc_call)
2234 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2235 "Incoming call matches pending inter-MSC Handover Number\n");
2236 }
2237 if (!mncc_call) {
2238 /* Find already active MNCC FSM for this callref.
2239 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2240 * MNCC<->GSM-CC call handling. */
2241 mncc_call = mncc_find_by_callref_from_msg(msg);
2242 }
2243 if (mncc_call) {
2244 mncc_call_rx(mncc_call, msg);
2245 return 0;
2246 }
2247
2248 /* None of the above? Then it must be a normal GSM CC call related message. */
2249 return mncc_tx_to_gsm_cc(net, msg);
2250}
Harald Welte27989d42018-06-21 20:39:20 +02002251
2252static struct datastate {
2253 uint32_t states;
2254 int type;
2255 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2256} datastatelist[] = {
2257 /* mobile originating call establishment */
2258 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2259 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2260 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2261 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2262 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2263 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2264 /* mobile terminating call establishment */
2265 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2266 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2267 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2268 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2269 {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 */
2270 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2271 /* signalling during call */
2272 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2273 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2274 {SBIT(GSM_CSTATE_ACTIVE),
2275 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2276 {ALL_STATES,
2277 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2278 {ALL_STATES,
2279 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2280 {ALL_STATES,
2281 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2282 {SBIT(GSM_CSTATE_ACTIVE),
2283 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2284 {SBIT(GSM_CSTATE_ACTIVE),
2285 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2286 {SBIT(GSM_CSTATE_ACTIVE),
2287 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2288 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2289 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2290 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2291 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2292 {SBIT(GSM_CSTATE_ACTIVE),
2293 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2294 /* clearing */
2295 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2296 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2297 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2298 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2299 {ALL_STATES, /* 5.4.3.4 */
2300 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2301};
2302
2303#define DATASLLEN \
2304 (sizeof(datastatelist) / sizeof(struct datastate))
2305
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002306int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002307{
2308 struct gsm48_hdr *gh = msgb_l3(msg);
2309 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2310 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2311 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002312 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2313 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002314 int i, rc = 0;
2315
2316 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002317 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002318 return -EINVAL;
2319 }
2320
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002321 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002322 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002323 return -EINVAL;
2324 }
2325
2326 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002327 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002328
Harald Welte27989d42018-06-21 20:39:20 +02002329 /* Create transaction */
2330 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002331 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002332 trans = trans_alloc(net, vsub,
2333 TRANS_CC,
2334 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002335 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002336 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002337 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002338 GSM48_PDISC_CC | (transaction_id << 4),
2339 GSM48_MT_CC_RELEASE_COMPL);
2340 return -ENOMEM;
2341 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002342 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2343 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2344 trans_free(trans);
2345 return -EINVAL;
2346 }
2347
Harald Welte27989d42018-06-21 20:39:20 +02002348 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002349 msc_a_get(msc_a, MSC_A_USE_CC);
2350 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002351 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002352
2353 /* An earlier CM Service Request for this CC message now has concluded */
2354 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2355 LOG_MSC_A(msc_a, LOGL_ERROR,
2356 "Creating new CC transaction without prior CM Service Request\n");
2357 else
2358 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002359 }
2360
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002361 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2362 gsm48_cc_state_name(trans->cc.state));
2363
Harald Welte27989d42018-06-21 20:39:20 +02002364 /* find function for current state and message */
2365 for (i = 0; i < DATASLLEN; i++)
2366 if ((msg_type == datastatelist[i].type)
2367 && ((1 << trans->cc.state) & datastatelist[i].states))
2368 break;
2369 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002370 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002371
2372 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2373 * transaction right away. */
2374 if (trans->cc.state == GSM_CSTATE_NULL) {
2375 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2376 " -- disarding new CC transaction right away\n");
2377 trans_free(trans);
2378 }
Harald Welte27989d42018-06-21 20:39:20 +02002379 return 0;
2380 }
2381
2382 assert(trans->vsub);
2383
2384 rc = datastatelist[i].rout(trans, msg);
2385
Harald Welte27989d42018-06-21 20:39:20 +02002386 return rc;
2387}