blob: 214d81fe201f04fb7a35680b335ad4c1cc5c4bbd [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 Hofmeyr58f40882023-03-08 04:04:27 +010058#include <osmocom/msc/sdp_msg.h>
Neels Hofmeyra001a702022-10-31 17:57:30 +010059#include <osmocom/msc/codec_mapping.h>
Harald Welte27989d42018-06-21 20:39:20 +020060
61#include <osmocom/gsm/gsm48.h>
62#include <osmocom/gsm/gsm0480.h>
63#include <osmocom/gsm/gsm_utils.h>
64#include <osmocom/gsm/protocol/gsm_04_08.h>
65#include <osmocom/core/msgb.h>
66#include <osmocom/core/talloc.h>
67#include <osmocom/core/utils.h>
68#include <osmocom/core/byteswap.h>
69#include <osmocom/gsm/tlv.h>
70#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020071
72#include <assert.h>
73
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010074static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
75static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
76static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
77
78static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
79{
80 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
81 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
82 OMSC_LINKID_CB(msg) = trans->dlci;
83
84 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
85}
86
87uint32_t msc_cc_next_outgoing_callref() {
88 static uint32_t last_callref = 0x80000000;
89 last_callref++;
90 if (last_callref < 0x80000001)
91 last_callref = 0x80000001;
92 return last_callref;
93}
Harald Welte27989d42018-06-21 20:39:20 +020094
Philipp Maier9ca7b312018-10-10 17:00:49 +020095static void gsm48_cc_guard_timeout(void *arg)
96{
97 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010098 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020099 trans_free(trans);
100 return;
101}
102
103static void gsm48_stop_guard_timer(struct gsm_trans *trans)
104{
105 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100106 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200107 osmo_timer_del(&trans->cc.timer_guard);
108 }
109}
110
111static void gsm48_start_guard_timer(struct gsm_trans *trans)
112{
113 /* NOTE: The purpose of this timer is to prevent the cc state machine
114 * from hanging in cases where mncc, gsm48 or both become unresponsive
115 * for some reason. The timer is started initially with the setup from
116 * the gsm48 side and then re-started with every incoming mncc message.
117 * Once the mncc state reaches its active state the timer is stopped.
118 * So if the cc state machine does not show any activity for an
119 * extended amount of time during call setup or teardown the guard
120 * timer will time out and hard-clear the connection. */
121 if (osmo_timer_pending(&trans->cc.timer_guard))
122 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100123 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200124 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
125 osmo_timer_schedule(&trans->cc.timer_guard,
126 trans->net->mncc_guard_timeout, 0);
127}
Harald Welte27989d42018-06-21 20:39:20 +0200128
129/* Call Control */
130
131void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
132{
133 net->mncc_recv(net, msg);
134}
135
136int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
137{
138 struct gsm48_hdr *gh;
139 struct msgb *ss_notify;
140
141 ss_notify = gsm0480_create_notifySS(message);
142 if (!ss_notify)
143 return -1;
144
145 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
146 uint8_t *data = msgb_push(ss_notify, 1);
147 data[0] = ss_notify->len - 1;
148 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
149 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100150 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200151}
152
153/* FIXME: this count_statistics is a state machine behaviour. we should convert
154 * the complete call control into a state machine. Afterwards we can move this
155 * code into state transitions.
156 */
157static void count_statistics(struct gsm_trans *trans, int new_state)
158{
159 int old_state = trans->cc.state;
160 struct rate_ctr_group *msc = trans->net->msc_ctrs;
161
162 if (old_state == new_state)
163 return;
164
165 /* state incoming */
166 switch (new_state) {
167 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200168 osmo_stat_item_inc(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
169 1);
170 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_ACTIVE));
Harald Welte27989d42018-06-21 20:39:20 +0200171 break;
172 }
173
174 /* state outgoing */
175 switch (old_state) {
176 case GSM_CSTATE_ACTIVE:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200177 osmo_stat_item_dec(osmo_stat_item_group_get_item(trans->net->statg, MSC_STAT_ACTIVE_CALLS),
178 1);
Harald Welte27989d42018-06-21 20:39:20 +0200179 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
180 new_state == GSM_CSTATE_DISCONNECT_IND)
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200181 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_COMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200182 else
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200183 rate_ctr_inc(rate_ctr_group_get_ctr(msc, MSC_CTR_CALL_INCOMPLETE));
Harald Welte27989d42018-06-21 20:39:20 +0200184 break;
185 }
186}
187
Harald Welte27989d42018-06-21 20:39:20 +0200188static void new_cc_state(struct gsm_trans *trans, int state)
189{
190 if (state > 31 || state < 0)
191 return;
192
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100193 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
194 gsm48_cc_state_name(trans->cc.state),
195 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200196
197 count_statistics(trans, state);
198 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200199
200 /* Stop the guard timer when a call reaches the active state */
201 if (state == GSM_CSTATE_ACTIVE)
202 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200203}
204
205static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
206{
207 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
208 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
209 uint8_t *cause, *call_state;
210
211 gh->msg_type = GSM48_MT_CC_STATUS;
212
213 cause = msgb_put(msg, 3);
214 cause[0] = 2;
215 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
216 cause[2] = 0x80 | 30; /* response to status inquiry */
217
218 call_state = msgb_put(msg, 1);
219 call_state[0] = 0xc0 | 0x00;
220
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100221 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200222}
223
224static void gsm48_stop_cc_timer(struct gsm_trans *trans)
225{
226 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100227 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200228 osmo_timer_del(&trans->cc.timer);
229 trans->cc.Tcurrent = 0;
230 }
231}
232
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100233/* Log the MNCC tx and rx events.
234 * Depending on msg_type, also log whether RTP information is passed on.
235 * (This is particularly interesting for the doc/sequence_charts/msc_log_to_ladder.py)
236 */
237static void log_mncc_rx_tx(struct gsm_trans *trans, const char *rx_tx, const union mncc_msg *mncc)
238{
239 const char *sdp = NULL;
240 struct sdp_msg sdp_msg = {};
241 struct osmo_sockaddr addr = {};
242
243 if (!log_check_level(DMNCC, LOGL_DEBUG))
244 return;
245
246 switch (mncc->msg_type) {
247 case MNCC_RTP_CREATE:
248 case MNCC_RTP_CONNECT:
249 addr = (struct osmo_sockaddr){ .u.sas = mncc->rtp.addr };
250 sdp = mncc->rtp.sdp;
251 break;
252
253 case MNCC_SETUP_IND:
254 case MNCC_SETUP_REQ:
255 case MNCC_SETUP_COMPL_IND:
256 case MNCC_SETUP_COMPL_REQ:
257 case MNCC_SETUP_RSP:
258 case MNCC_SETUP_CNF:
259 case MNCC_CALL_CONF_IND:
260 case MNCC_CALL_PROC_REQ:
261 case MNCC_ALERT_IND:
262 case MNCC_ALERT_REQ:
263 sdp = mncc->signal.sdp;
264 break;
265
266 default:
267 break;
268 }
269
270 if (sdp && sdp[0] && (sdp_msg_from_sdp_str(&sdp_msg, sdp) == 0)) {
271 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s (RTP=%s)\n",
272 rx_tx,
273 get_mncc_name(mncc->msg_type),
274 sdp_msg_to_str(&sdp_msg));
275 return;
276 }
277
278 if (osmo_sockaddr_is_any(&addr) == 0) {
279 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s (RTP=%s)\n",
280 rx_tx,
281 get_mncc_name(mncc->msg_type),
282 osmo_sockaddr_to_str_c(OTC_SELECT, &addr));
283 return;
284 }
285
286 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "%s %s\n", rx_tx, get_mncc_name(mncc->msg_type));
287}
288
Harald Welte27989d42018-06-21 20:39:20 +0200289static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
290 int msg_type, struct gsm_mncc *mncc)
291{
292 struct msgb *msg;
293 unsigned char *data;
294
Harald Welte27989d42018-06-21 20:39:20 +0200295 mncc->msg_type = msg_type;
Neels Hofmeyr58f40882023-03-08 04:04:27 +0100296 log_mncc_rx_tx(trans, "tx", (union mncc_msg *)mncc);
Harald Welte27989d42018-06-21 20:39:20 +0200297
298 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
299 if (!msg)
300 return -ENOMEM;
301
302 data = msgb_put(msg, sizeof(struct gsm_mncc));
303 memcpy(data, mncc, sizeof(struct gsm_mncc));
304
305 cc_tx_to_mncc(net, msg);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +0200306 /* trans may be NULL when sending an MNCC error reply upon an invalid MNCC request */
307 if (trans)
308 trans->cc.mncc_initiated = true;
Harald Welte27989d42018-06-21 20:39:20 +0200309
310 return 0;
311}
312
313int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
314 uint32_t callref, int location, int value)
315{
316 struct gsm_mncc rel;
317
318 memset(&rel, 0, sizeof(rel));
319 rel.callref = callref;
320 mncc_set_cause(&rel, location, value);
321 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
322 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
323 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
324}
325
326/* Call Control Specific transaction release.
327 * gets called by trans_free, DO NOT CALL YOURSELF! */
328void _gsm48_cc_trans_free(struct gsm_trans *trans)
329{
330 gsm48_stop_cc_timer(trans);
331
Harald Welte27989d42018-06-21 20:39:20 +0200332 /* send release to L4, if callref still exists */
333 if (trans->callref) {
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100334 /* Send MNCC REL.ind (cause='Resource unavailable') */
335 if (trans->cc.mncc_initiated) {
336 mncc_release_ind(trans->net, trans, trans->callref,
337 GSM48_CAUSE_LOC_PRN_S_LU,
Keith Whyteba4d6822022-07-03 04:12:58 +0100338 (trans->cc.state == GSM_CSTATE_CALL_RECEIVED) ?
339 GSM48_CC_CAUSE_USER_NOTRESPOND :
Vadim Yanitskiydd466cf2021-02-05 19:17:31 +0100340 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
341 }
342
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100343 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
344 * CC Release to the MS if it gets freed here. Hack it to do so. */
345 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
346 struct gsm_mncc rel = {};
347 rel.callref = trans->callref;
348 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
349 gsm48_cc_tx_release(trans, &rel);
350 }
Harald Welte27989d42018-06-21 20:39:20 +0200351 /* This is a final freeing of the transaction. The MNCC release may have triggered the
352 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
353 gsm48_stop_cc_timer(trans);
354 }
355 if (trans->cc.state != GSM_CSTATE_NULL)
356 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200357
358 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359
360 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
361 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200362}
363
Harald Welte27989d42018-06-21 20:39:20 +0200364/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100365static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200366{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100367 if (trans->msc_a) {
368 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
369 "Handle paging error: transaction already associated with subscriber,"
370 " apparently it was already handled. Skip.\n");
371 return;
Harald Welte27989d42018-06-21 20:39:20 +0200372 }
373
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100374 if (msc_a) {
375 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
376 /* Assign conn */
377 msc_a_get(msc_a, MSC_A_USE_CC);
378 trans->msc_a = msc_a;
379 trans->paging_request = NULL;
Keith Whytea1a70be2021-05-16 02:59:52 +0200380
381 /* Get the GCR from the MO call leg (if any). */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300382 if (!trans->cc.lcls)
Keith Whytea1a70be2021-05-16 02:59:52 +0200383 trans->cc.lcls = trans_lcls_compose(trans, true);
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300384 if (trans->cc.lcls && trans->cc.msg.fields & MNCC_F_GCR) {
385 int rc = osmo_dec_gcr(&trans->cc.lcls->gcr,
386 &trans->cc.msg.gcr[0],
387 sizeof(trans->cc.msg.gcr));
388 if (rc < 0)
389 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse GCR\n");
390 else
Keith Whytea1a70be2021-05-16 02:59:52 +0200391 trans->cc.lcls->gcr_available = true;
Keith Whytea1a70be2021-05-16 02:59:52 +0200392 }
393
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100394 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
395 /* send SETUP request to called party */
396 gsm48_cc_tx_setup(trans, &trans->cc.msg);
397 } else {
398 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
399 /* Temporarily out of order */
400 mncc_release_ind(trans->net, trans,
401 trans->callref,
402 GSM48_CAUSE_LOC_PRN_S_LU,
403 GSM48_CC_CAUSE_DEST_OOO);
404 trans->callref = 0;
405 trans->paging_request = NULL;
406 trans_free(trans);
407 }
Harald Welte27989d42018-06-21 20:39:20 +0200408}
409
410/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100411static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200412{
413 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
414 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100415 struct call_leg *cl1;
416 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200417
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100418 if (!trans1 || !trans2) {
419 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200420 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100421 }
Harald Welte27989d42018-06-21 20:39:20 +0200422
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100423 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100424 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
425 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 +0200426 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100427 }
428
429 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
430 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200431
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100432 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
433 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
434 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
435 * If we can't, then we must give up. */
436 cl1 = trans1->msc_a->cc.call_leg;
437 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200438
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100439 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200440}
441
442static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
443{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100444 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200445 return gsm48_cc_tx_status(trans, msg);
446}
447
Harald Welte27989d42018-06-21 20:39:20 +0200448static void gsm48_cc_timeout(void *arg)
449{
450 struct gsm_trans *trans = arg;
451 int disconnect = 0, release = 0;
452 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
453 int mo_location = GSM48_CAUSE_LOC_USER;
454 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
455 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
456 struct gsm_mncc mo_rel, l4_rel;
457
Neels Hofmeyre29ee5a2022-08-06 14:16:55 +0200458 LOG_TRANS(trans, LOGL_INFO, "Timeout of T%x\n", trans->cc.Tcurrent);
459
Harald Welte27989d42018-06-21 20:39:20 +0200460 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
461 mo_rel.callref = trans->callref;
462 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
463 l4_rel.callref = trans->callref;
464
465 switch(trans->cc.Tcurrent) {
466 case 0x303:
467 release = 1;
468 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
469 break;
470 case 0x310:
471 disconnect = 1;
472 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
473 break;
474 case 0x313:
475 disconnect = 1;
476 /* unknown, did not find it in the specs */
477 break;
478 case 0x301:
479 disconnect = 1;
480 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
481 break;
482 case 0x308:
483 if (!trans->cc.T308_second) {
484 /* restart T308 a second time */
485 gsm48_cc_tx_release(trans, &trans->cc.msg);
486 trans->cc.T308_second = 1;
487 break; /* stay in release state */
488 }
489 trans_free(trans);
490 return;
491 case 0x306:
492 release = 1;
493 mo_cause = trans->cc.msg.cause.value;
494 mo_location = trans->cc.msg.cause.location;
495 break;
496 case 0x323:
497 disconnect = 1;
498 break;
499 default:
500 release = 1;
501 }
502
503 if (release && trans->callref) {
504 /* process release towards layer 4 */
505 mncc_release_ind(trans->net, trans, trans->callref,
506 l4_location, l4_cause);
507 trans->callref = 0;
508 }
509
510 if (disconnect && trans->callref) {
511 /* process disconnect towards layer 4 */
512 mncc_set_cause(&l4_rel, l4_location, l4_cause);
513 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
514 }
515
516 /* process disconnect towards mobile station */
517 if (disconnect || release) {
518 mncc_set_cause(&mo_rel, mo_location, mo_cause);
519 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
520 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
521 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
522 mo_rel.cause.diag_len = 3;
523
524 if (disconnect)
525 gsm48_cc_tx_disconnect(trans, &mo_rel);
526 if (release)
527 gsm48_cc_tx_release(trans, &mo_rel);
528 }
529
530}
531
532/* disconnect both calls from the bridge */
533static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100534 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200535{
536 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
537 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
538 struct gsm_mncc mx_rel;
539 if (!trans0 || !trans1)
540 return;
541
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100542 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
543 trans0->callref, trans1->callref, strerror(err));
544 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200545 trans0->callref, trans1->callref, strerror(err));
546
547 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
548 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
549 GSM48_CC_CAUSE_CHAN_UNACCEPT);
550
551 mx_rel.callref = trans0->callref;
552 gsm48_cc_tx_disconnect(trans0, &mx_rel);
553
554 mx_rel.callref = trans1->callref;
555 gsm48_cc_tx_disconnect(trans1, &mx_rel);
556}
557
558static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
559 int sec, int micro)
560{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100561 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200562 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
563 osmo_timer_schedule(&trans->cc.timer, sec, micro);
564 trans->cc.Tcurrent = current;
565}
566
567static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
568{
569 struct gsm48_hdr *gh = msgb_l3(msg);
570 uint8_t msg_type = gsm48_hdr_msg_type(gh);
571 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
572 struct tlv_parsed tp;
573 struct gsm_mncc setup;
574
Philipp Maier9ca7b312018-10-10 17:00:49 +0200575 gsm48_start_guard_timer(trans);
576
Harald Welte27989d42018-06-21 20:39:20 +0200577 memset(&setup, 0, sizeof(struct gsm_mncc));
578 setup.callref = trans->callref;
579
Keith Whytea1a70be2021-05-16 02:59:52 +0200580 /* New Global Call Reference */
581 if (!trans->cc.lcls)
582 trans->cc.lcls = trans_lcls_compose(trans, true);
583
584 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300585 if (trans->cc.lcls) {
586 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
587 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
588 int rc;
589
590 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
591 memcpy(&setup.gcr[0], gcr_msg->data, rc);
592 setup.fields |= MNCC_F_GCR;
593 } else
594 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
595 msgb_free(gcr_msg);
596 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200597
Harald Welte27989d42018-06-21 20:39:20 +0200598 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
599 /* emergency setup is identified by msg_type */
600 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
601 setup.fields |= MNCC_F_EMERGENCY;
602 setup.emergency = 1;
603 /* use destination number as configured by user (if any) */
604 if (trans->net->emergency.route_to_msisdn) {
605 setup.fields |= MNCC_F_CALLED;
606 setup.called.type = 0; /* unknown */
607 setup.called.plan = 0; /* unknown */
608 OSMO_STRLCPY_ARRAY(setup.called.number,
609 trans->net->emergency.route_to_msisdn);
610 }
611 }
612
613 /* use subscriber as calling party number */
614 setup.fields |= MNCC_F_CALLING;
615 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
616 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
617
618 /* bearer capability */
619 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
620 setup.fields |= MNCC_F_BEARER_CAP;
621 gsm48_decode_bearer_cap(&setup.bearer_cap,
622 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
623
624 /* Create a copy of the bearer capability
625 * in the transaction struct, so we can use
626 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100627 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200628 sizeof(trans->bearer_cap));
629 }
630 /* facility */
631 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
632 setup.fields |= MNCC_F_FACILITY;
633 gsm48_decode_facility(&setup.facility,
634 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
635 }
636 /* called party bcd number */
637 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
638 setup.fields |= MNCC_F_CALLED;
639 gsm48_decode_called(&setup.called,
640 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
641 }
642 /* user-user */
643 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
644 setup.fields |= MNCC_F_USERUSER;
645 gsm48_decode_useruser(&setup.useruser,
646 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
647 }
648 /* ss-version */
649 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
650 setup.fields |= MNCC_F_SSVERSION;
651 gsm48_decode_ssversion(&setup.ssversion,
652 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
653 }
654 /* CLIR suppression */
655 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
656 setup.clir.sup = 1;
657 /* CLIR invocation */
658 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
659 setup.clir.inv = 1;
660 /* cc cap */
661 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
662 setup.fields |= MNCC_F_CCCAP;
663 gsm48_decode_cccap(&setup.cccap,
664 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
665 }
666
667 new_cc_state(trans, GSM_CSTATE_INITIATED);
668
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100669 /* MO call leg starting, gather all codec information so far known: */
670 codec_filter_init(&trans->cc.codecs);
671 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
672 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
673 if (setup.fields & MNCC_F_BEARER_CAP)
674 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
675 codec_filter_run(&trans->cc.codecs);
676
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100677 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
678 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100679 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200680
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200681 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200682
683 /* indicate setup to MNCC */
684 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
685
686 /* MNCC code will modify the channel asynchronously, we should
687 * ipaccess-bind only after the modification has been made to the
688 * lchan->tch_mode */
689 return 0;
690}
691
692static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
693{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100694 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200695 struct gsm48_hdr *gh;
696 struct gsm_mncc *setup = arg;
697 int rc, trans_id;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100698 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200699
700 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
701
702 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700703 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100704 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200705 "This is not allowed!\n");
706 /* Temporarily out of order */
707 rc = mncc_release_ind(trans->net, trans, trans->callref,
708 GSM48_CAUSE_LOC_PRN_S_LU,
709 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
710 trans->callref = 0;
711 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200712 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200713 return rc;
714 }
715
716 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100717 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200718 if (trans_id < 0) {
719 /* no free transaction ID */
720 rc = mncc_release_ind(trans->net, trans, trans->callref,
721 GSM48_CAUSE_LOC_PRN_S_LU,
722 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
723 trans->callref = 0;
724 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200725 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200726 return rc;
727 }
728 trans->transaction_id = trans_id;
729
730 gh->msg_type = GSM48_MT_CC_SETUP;
731
732 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
733
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100734 /* MT call leg is starting. Gather all codecs information so far known.
735 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
736 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
737 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
738 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
739 * Capabilities. */
740 codec_filter_init(&trans->cc.codecs);
741 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
742 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100743 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
744 if (setup->sdp[0]) {
745 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, setup->sdp);
746 if (rc)
747 LOG_TRANS(trans, LOGL_ERROR, "Failed to parse remote call leg SDP: %d\n", rc);
748 }
749 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
750 * MNCC, if any. */
751 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
752 trans->cc.codecs.remote = (struct sdp_msg){};
753 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
754 &setup->bearer_cap);
755 }
756 if (!trans->cc.codecs.remote.audio_codecs.count)
757 LOG_TRANS(trans, LOGL_INFO,
758 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
759
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100760 codec_filter_run(&trans->cc.codecs);
761 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
762
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100763 /* Compose Bearer Capability information that reflects only the codecs (Speech Versions) remaining after
764 * intersecting MS, BSS and remote call leg restrictions. To store in trans for later use, and to include in
765 * the outgoing CC Setup message. */
766 bearer_cap = (struct gsm_mncc_bearer_cap){
767 .speech_ver = { -1 },
768 };
769 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.codecs.result.audio_codecs);
770 rc = bearer_cap_set_radio(&bearer_cap);
771 if (rc) {
772 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
773 trans_free(trans);
774 msgb_free(msg);
775 return rc;
Harald Welte27989d42018-06-21 20:39:20 +0200776 }
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100777 /* Create a copy of the bearer capability in the transaction struct, so we can use this information later */
778 /* TODO: we should be able to drop trans->bearer_cap, replaced by the codecs filter. Verify this.
779 * So far let's just store it there like previous code did. */
780 trans->bearer_cap = bearer_cap;
781 /* If no resulting codecs remain, error out. We cannot find a codec that matches both call legs. If the MGW were
782 * able to transcode, we could use non-identical codecs on each conn of the MGW endpoint, but we are aiming for
783 * finding a matching codec. */
784 if (bearer_cap.speech_ver[0] == -1) {
785 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
786 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
787
788 /* incompatible codecs */
789 rc = mncc_release_ind(trans->net, trans, trans->callref,
790 GSM48_CAUSE_LOC_PRN_S_LU,
791 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
792 trans->callref = 0;
793 trans_free(trans);
794 msgb_free(msg);
795 return rc;
796 }
797 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
798
Harald Welte27989d42018-06-21 20:39:20 +0200799 /* facility */
800 if (setup->fields & MNCC_F_FACILITY)
801 gsm48_encode_facility(msg, 0, &setup->facility);
802 /* progress */
803 if (setup->fields & MNCC_F_PROGRESS)
804 gsm48_encode_progress(msg, 0, &setup->progress);
805 /* calling party BCD number */
806 if (setup->fields & MNCC_F_CALLING)
807 gsm48_encode_calling(msg, &setup->calling);
808 /* called party BCD number */
809 if (setup->fields & MNCC_F_CALLED)
810 gsm48_encode_called(msg, &setup->called);
811 /* user-user */
812 if (setup->fields & MNCC_F_USERUSER)
813 gsm48_encode_useruser(msg, 0, &setup->useruser);
814 /* redirecting party BCD number */
815 if (setup->fields & MNCC_F_REDIRECTING)
816 gsm48_encode_redirecting(msg, &setup->redirecting);
817 /* signal */
818 if (setup->fields & MNCC_F_SIGNAL)
819 gsm48_encode_signal(msg, setup->signal);
820
821 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
822
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200823 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200824
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100825 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200826}
827
828static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
829{
830 struct gsm48_hdr *gh = msgb_l3(msg);
831 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
832 struct tlv_parsed tp;
833 struct gsm_mncc call_conf;
834 int rc;
835
836 gsm48_stop_cc_timer(trans);
837 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
838
839 memset(&call_conf, 0, sizeof(struct gsm_mncc));
840 call_conf.callref = trans->callref;
841
842 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
843#if 0
844 /* repeat */
845 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
846 call_conf.repeat = 1;
847 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
848 call_conf.repeat = 2;
849#endif
850 /* bearer capability */
851 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
852 call_conf.fields |= MNCC_F_BEARER_CAP;
853 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
854 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
855
856 /* Create a copy of the bearer capability
857 * in the transaction struct, so we can use
858 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100859 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200860 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +0100861
862 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
863 * Store in codecs filter. */
864 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200865 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100866
Harald Welte27989d42018-06-21 20:39:20 +0200867 /* cause */
868 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
869 call_conf.fields |= MNCC_F_CAUSE;
870 gsm48_decode_cause(&call_conf.cause,
871 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
872 }
873 /* cc cap */
874 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
875 call_conf.fields |= MNCC_F_CCCAP;
876 gsm48_decode_cccap(&call_conf.cccap,
877 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
878 }
879
880 /* IMSI of called subscriber */
881 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
882
883 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
884
885 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100886 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200887
888 /* don't continue, if there were problems with
889 * the call assignment. */
890 if (rc)
891 return rc;
892
893 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
894 &call_conf);
895}
896
897static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
898{
899 struct gsm_mncc *proceeding = arg;
900 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
901 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
902 int rc;
903
904 gh->msg_type = GSM48_MT_CC_CALL_PROC;
905
906 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
907
908 /* bearer capability */
909 if (proceeding->fields & MNCC_F_BEARER_CAP) {
910 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
911 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
912 }
913 /* facility */
914 if (proceeding->fields & MNCC_F_FACILITY)
915 gsm48_encode_facility(msg, 0, &proceeding->facility);
916 /* progress */
917 if (proceeding->fields & MNCC_F_PROGRESS)
918 gsm48_encode_progress(msg, 0, &proceeding->progress);
919
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100920 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200921 if (rc)
922 return rc;
923
924 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100925 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200926}
927
928static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
929{
930 struct gsm48_hdr *gh = msgb_l3(msg);
931 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
932 struct tlv_parsed tp;
933 struct gsm_mncc alerting;
934
935 gsm48_stop_cc_timer(trans);
936 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
937
938 memset(&alerting, 0, sizeof(struct gsm_mncc));
939 alerting.callref = trans->callref;
940 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
941 /* facility */
942 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
943 alerting.fields |= MNCC_F_FACILITY;
944 gsm48_decode_facility(&alerting.facility,
945 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
946 }
947
948 /* progress */
949 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
950 alerting.fields |= MNCC_F_PROGRESS;
951 gsm48_decode_progress(&alerting.progress,
952 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
953 }
954 /* ss-version */
955 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
956 alerting.fields |= MNCC_F_SSVERSION;
957 gsm48_decode_ssversion(&alerting.ssversion,
958 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
959 }
960
961 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
962
963 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
964 &alerting);
965}
966
967static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
968{
969 struct gsm_mncc *alerting = arg;
970 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
971 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
972
973 gh->msg_type = GSM48_MT_CC_ALERTING;
974
975 /* facility */
976 if (alerting->fields & MNCC_F_FACILITY)
977 gsm48_encode_facility(msg, 0, &alerting->facility);
978 /* progress */
979 if (alerting->fields & MNCC_F_PROGRESS)
980 gsm48_encode_progress(msg, 0, &alerting->progress);
981 /* user-user */
982 if (alerting->fields & MNCC_F_USERUSER)
983 gsm48_encode_useruser(msg, 0, &alerting->useruser);
984
985 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
986
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100987 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200988}
989
990static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
991{
992 struct gsm_mncc *progress = arg;
993 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
994 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
995
996 gh->msg_type = GSM48_MT_CC_PROGRESS;
997
998 /* progress */
999 gsm48_encode_progress(msg, 1, &progress->progress);
1000 /* user-user */
1001 if (progress->fields & MNCC_F_USERUSER)
1002 gsm48_encode_useruser(msg, 0, &progress->useruser);
1003
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001004 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001005}
1006
1007static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1008{
1009 struct gsm_mncc *connect = arg;
1010 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1011 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1012
1013 gh->msg_type = GSM48_MT_CC_CONNECT;
1014
1015 gsm48_stop_cc_timer(trans);
1016 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1017
1018 /* facility */
1019 if (connect->fields & MNCC_F_FACILITY)
1020 gsm48_encode_facility(msg, 0, &connect->facility);
1021 /* progress */
1022 if (connect->fields & MNCC_F_PROGRESS)
1023 gsm48_encode_progress(msg, 0, &connect->progress);
1024 /* connected number */
1025 if (connect->fields & MNCC_F_CONNECTED)
1026 gsm48_encode_connected(msg, &connect->connected);
1027 /* user-user */
1028 if (connect->fields & MNCC_F_USERUSER)
1029 gsm48_encode_useruser(msg, 0, &connect->useruser);
1030
1031 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1032
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001033 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001034}
1035
1036static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1037{
1038 struct gsm48_hdr *gh = msgb_l3(msg);
1039 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1040 struct tlv_parsed tp;
1041 struct gsm_mncc connect;
1042
1043 gsm48_stop_cc_timer(trans);
1044
1045 memset(&connect, 0, sizeof(struct gsm_mncc));
1046 connect.callref = trans->callref;
1047 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1048 /* use subscriber as connected party number */
1049 connect.fields |= MNCC_F_CONNECTED;
1050 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1051 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1052
1053 /* facility */
1054 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1055 connect.fields |= MNCC_F_FACILITY;
1056 gsm48_decode_facility(&connect.facility,
1057 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1058 }
1059 /* user-user */
1060 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1061 connect.fields |= MNCC_F_USERUSER;
1062 gsm48_decode_useruser(&connect.useruser,
1063 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1064 }
1065 /* ss-version */
1066 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1067 connect.fields |= MNCC_F_SSVERSION;
1068 gsm48_decode_ssversion(&connect.ssversion,
1069 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1070 }
1071
1072 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001073 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001074
1075 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1076}
1077
1078
1079static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1080{
1081 struct gsm_mncc connect_ack;
1082
1083 gsm48_stop_cc_timer(trans);
1084
1085 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001086 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 +02001087
1088 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1089 connect_ack.callref = trans->callref;
1090
1091 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1092 &connect_ack);
1093}
1094
1095static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1096{
1097 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1098 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1099
1100 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1101
1102 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1103
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001104 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001105}
1106
1107static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1108{
1109 struct gsm48_hdr *gh = msgb_l3(msg);
1110 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1111 struct tlv_parsed tp;
1112 struct gsm_mncc disc;
1113
1114 gsm48_stop_cc_timer(trans);
1115
1116 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1117
1118 memset(&disc, 0, sizeof(struct gsm_mncc));
1119 disc.callref = trans->callref;
1120 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1121 /* cause */
1122 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1123 disc.fields |= MNCC_F_CAUSE;
1124 gsm48_decode_cause(&disc.cause,
1125 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1126 }
1127 /* facility */
1128 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1129 disc.fields |= MNCC_F_FACILITY;
1130 gsm48_decode_facility(&disc.facility,
1131 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1132 }
1133 /* user-user */
1134 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1135 disc.fields |= MNCC_F_USERUSER;
1136 gsm48_decode_useruser(&disc.useruser,
1137 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1138 }
1139 /* ss-version */
1140 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1141 disc.fields |= MNCC_F_SSVERSION;
1142 gsm48_decode_ssversion(&disc.ssversion,
1143 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1144 }
1145
1146 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001147}
1148
1149static struct gsm_mncc_cause default_cause = {
1150 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1151 .coding = 0,
1152 .rec = 0,
1153 .rec_val = 0,
1154 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1155 .diag_len = 0,
1156 .diag = { 0 },
1157};
1158
1159static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1160{
1161 struct gsm_mncc *disc = arg;
1162 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1163 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1164
1165 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1166
1167 gsm48_stop_cc_timer(trans);
1168 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1169
1170 /* cause */
1171 if (disc->fields & MNCC_F_CAUSE)
1172 gsm48_encode_cause(msg, 1, &disc->cause);
1173 else
1174 gsm48_encode_cause(msg, 1, &default_cause);
1175
1176 /* facility */
1177 if (disc->fields & MNCC_F_FACILITY)
1178 gsm48_encode_facility(msg, 0, &disc->facility);
1179 /* progress */
1180 if (disc->fields & MNCC_F_PROGRESS)
1181 gsm48_encode_progress(msg, 0, &disc->progress);
1182 /* user-user */
1183 if (disc->fields & MNCC_F_USERUSER)
1184 gsm48_encode_useruser(msg, 0, &disc->useruser);
1185
1186 /* store disconnect cause for T306 expiry */
1187 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1188
1189 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1190
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001191 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001192}
1193
1194static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1195{
1196 struct gsm48_hdr *gh = msgb_l3(msg);
1197 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1198 struct tlv_parsed tp;
1199 struct gsm_mncc rel;
1200 int rc;
1201
1202 gsm48_stop_cc_timer(trans);
1203
1204 memset(&rel, 0, sizeof(struct gsm_mncc));
1205 rel.callref = trans->callref;
1206 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1207 /* cause */
1208 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1209 rel.fields |= MNCC_F_CAUSE;
1210 gsm48_decode_cause(&rel.cause,
1211 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1212 }
1213 /* facility */
1214 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1215 rel.fields |= MNCC_F_FACILITY;
1216 gsm48_decode_facility(&rel.facility,
1217 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1218 }
1219 /* user-user */
1220 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1221 rel.fields |= MNCC_F_USERUSER;
1222 gsm48_decode_useruser(&rel.useruser,
1223 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1224 }
1225 /* ss-version */
1226 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1227 rel.fields |= MNCC_F_SSVERSION;
1228 gsm48_decode_ssversion(&rel.ssversion,
1229 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1230 }
1231
1232 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1233 /* release collision 5.4.5 */
1234 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1235 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001236 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001237 GSM48_PDISC_CC | (trans->transaction_id << 4),
1238 GSM48_MT_CC_RELEASE_COMPL);
1239 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1240 }
1241
1242 new_cc_state(trans, GSM_CSTATE_NULL);
1243
1244 trans->callref = 0;
1245 trans_free(trans);
1246
1247 return rc;
1248}
1249
1250static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1251{
1252 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001253 struct msgb *msg;
1254 struct gsm48_hdr *gh;
1255
1256 if (!trans->msc_a) {
1257 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1258 return -EINVAL;
1259 }
1260
1261 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1262 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001263
1264 gh->msg_type = GSM48_MT_CC_RELEASE;
1265
1266 gsm48_stop_cc_timer(trans);
1267 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1268
1269 /* cause */
1270 if (rel->fields & MNCC_F_CAUSE)
1271 gsm48_encode_cause(msg, 0, &rel->cause);
1272 /* facility */
1273 if (rel->fields & MNCC_F_FACILITY)
1274 gsm48_encode_facility(msg, 0, &rel->facility);
1275 /* user-user */
1276 if (rel->fields & MNCC_F_USERUSER)
1277 gsm48_encode_useruser(msg, 0, &rel->useruser);
1278
1279 trans->cc.T308_second = 0;
1280 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1281
1282 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1283 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1284
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001285 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001286}
1287
1288static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1289{
1290 struct gsm48_hdr *gh = msgb_l3(msg);
1291 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1292 struct tlv_parsed tp;
1293 struct gsm_mncc rel;
1294 int rc = 0;
1295
1296 gsm48_stop_cc_timer(trans);
1297
1298 memset(&rel, 0, sizeof(struct gsm_mncc));
1299 rel.callref = trans->callref;
1300 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1301 /* cause */
1302 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1303 rel.fields |= MNCC_F_CAUSE;
1304 gsm48_decode_cause(&rel.cause,
1305 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1306 }
1307 /* facility */
1308 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1309 rel.fields |= MNCC_F_FACILITY;
1310 gsm48_decode_facility(&rel.facility,
1311 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1312 }
1313 /* user-user */
1314 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1315 rel.fields |= MNCC_F_USERUSER;
1316 gsm48_decode_useruser(&rel.useruser,
1317 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1318 }
1319 /* ss-version */
1320 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1321 rel.fields |= MNCC_F_SSVERSION;
1322 gsm48_decode_ssversion(&rel.ssversion,
1323 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1324 }
1325
1326 if (trans->callref) {
1327 switch (trans->cc.state) {
1328 case GSM_CSTATE_CALL_PRESENT:
1329 rc = mncc_recvmsg(trans->net, trans,
1330 MNCC_REJ_IND, &rel);
1331 break;
1332 case GSM_CSTATE_RELEASE_REQ:
1333 rc = mncc_recvmsg(trans->net, trans,
1334 MNCC_REL_CNF, &rel);
1335 break;
1336 default:
1337 rc = mncc_recvmsg(trans->net, trans,
1338 MNCC_REL_IND, &rel);
1339 }
1340 }
1341
1342 trans->callref = 0;
1343 trans_free(trans);
1344
1345 return rc;
1346}
1347
1348static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1349{
1350 struct gsm_mncc *rel = arg;
1351 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1352 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1353 int ret;
1354
1355 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1356
1357 trans->callref = 0;
1358
1359 gsm48_stop_cc_timer(trans);
1360
1361 /* cause */
1362 if (rel->fields & MNCC_F_CAUSE)
1363 gsm48_encode_cause(msg, 0, &rel->cause);
1364 /* facility */
1365 if (rel->fields & MNCC_F_FACILITY)
1366 gsm48_encode_facility(msg, 0, &rel->facility);
1367 /* user-user */
1368 if (rel->fields & MNCC_F_USERUSER)
1369 gsm48_encode_useruser(msg, 0, &rel->useruser);
1370
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001371 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001372
1373 trans_free(trans);
1374
1375 return ret;
1376}
1377
1378static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1379{
1380 struct gsm48_hdr *gh = msgb_l3(msg);
1381 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1382 struct tlv_parsed tp;
1383 struct gsm_mncc fac;
1384
1385 memset(&fac, 0, sizeof(struct gsm_mncc));
1386 fac.callref = trans->callref;
1387 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1388 /* facility */
1389 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1390 fac.fields |= MNCC_F_FACILITY;
1391 gsm48_decode_facility(&fac.facility,
1392 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1393 }
1394 /* ss-version */
1395 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1396 fac.fields |= MNCC_F_SSVERSION;
1397 gsm48_decode_ssversion(&fac.ssversion,
1398 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1399 }
1400
1401 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1402}
1403
1404static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1405{
1406 struct gsm_mncc *fac = arg;
1407 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1408 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1409
1410 gh->msg_type = GSM48_MT_CC_FACILITY;
1411
1412 /* facility */
1413 gsm48_encode_facility(msg, 1, &fac->facility);
1414
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001415 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001416}
1417
1418static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1419{
1420 struct gsm_mncc hold;
1421
1422 memset(&hold, 0, sizeof(struct gsm_mncc));
1423 hold.callref = trans->callref;
1424 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1425}
1426
1427static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1428{
1429 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1430 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1431
1432 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1433
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001434 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001435}
1436
1437static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1438{
1439 struct gsm_mncc *hold_rej = arg;
1440 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1442
1443 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1444
1445 /* cause */
1446 if (hold_rej->fields & MNCC_F_CAUSE)
1447 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1448 else
1449 gsm48_encode_cause(msg, 1, &default_cause);
1450
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001451 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001452}
1453
1454static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1455{
1456 struct gsm_mncc retrieve;
1457
1458 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1459 retrieve.callref = trans->callref;
1460 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1461 &retrieve);
1462}
1463
1464static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1465{
1466 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1468
1469 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1470
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001471 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001472}
1473
1474static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1475{
1476 struct gsm_mncc *retrieve_rej = arg;
1477 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1478 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1479
1480 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1481
1482 /* cause */
1483 if (retrieve_rej->fields & MNCC_F_CAUSE)
1484 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1485 else
1486 gsm48_encode_cause(msg, 1, &default_cause);
1487
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001488 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001489}
1490
1491static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1492{
1493 struct gsm48_hdr *gh = msgb_l3(msg);
1494 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1495 struct tlv_parsed tp;
1496 struct gsm_mncc dtmf;
1497
1498 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1499 dtmf.callref = trans->callref;
1500 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1501 /* keypad facility */
1502 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1503 dtmf.fields |= MNCC_F_KEYPAD;
1504 gsm48_decode_keypad(&dtmf.keypad,
1505 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1506 }
1507
1508 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1509}
1510
1511static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1512{
1513 struct gsm_mncc *dtmf = arg;
1514 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1515 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1516
1517 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1518
1519 /* keypad */
1520 if (dtmf->fields & MNCC_F_KEYPAD)
1521 gsm48_encode_keypad(msg, dtmf->keypad);
1522
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001523 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001524}
1525
1526static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1527{
1528 struct gsm_mncc *dtmf = arg;
1529 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1530 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1531
1532 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1533
1534 /* cause */
1535 if (dtmf->fields & MNCC_F_CAUSE)
1536 gsm48_encode_cause(msg, 1, &dtmf->cause);
1537 else
1538 gsm48_encode_cause(msg, 1, &default_cause);
1539
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001540 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001541}
1542
1543static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1544{
1545 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1546 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1547
1548 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1549
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001550 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001551}
1552
1553static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1554{
1555 struct gsm_mncc dtmf;
1556
1557 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1558 dtmf.callref = trans->callref;
1559
1560 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1561}
1562
1563static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1564{
1565 struct gsm48_hdr *gh = msgb_l3(msg);
1566 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1567 struct tlv_parsed tp;
1568 struct gsm_mncc modify;
1569
1570 memset(&modify, 0, sizeof(struct gsm_mncc));
1571 modify.callref = trans->callref;
1572 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1573 /* bearer capability */
1574 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1575 modify.fields |= MNCC_F_BEARER_CAP;
1576 gsm48_decode_bearer_cap(&modify.bearer_cap,
1577 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1578
1579 /* Create a copy of the bearer capability
1580 * in the transaction struct, so we can use
1581 * this information later */
1582 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1583 sizeof(trans->bearer_cap));
1584 }
1585
1586 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1587
1588 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1589}
1590
1591static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1592{
1593 struct gsm_mncc *modify = arg;
1594 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1595 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1596
1597 gh->msg_type = GSM48_MT_CC_MODIFY;
1598
1599 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1600
1601 /* bearer capability */
1602 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1603 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1604
1605 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1606
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001607 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001608}
1609
1610static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1611{
1612 struct gsm48_hdr *gh = msgb_l3(msg);
1613 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1614 struct tlv_parsed tp;
1615 struct gsm_mncc modify;
1616
1617 gsm48_stop_cc_timer(trans);
1618
1619 memset(&modify, 0, sizeof(struct gsm_mncc));
1620 modify.callref = trans->callref;
1621 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1622 /* bearer capability */
1623 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1624 modify.fields |= MNCC_F_BEARER_CAP;
1625 gsm48_decode_bearer_cap(&modify.bearer_cap,
1626 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1627
1628 /* Create a copy of the bearer capability
1629 * in the transaction struct, so we can use
1630 * this information later */
1631 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1632 sizeof(trans->bearer_cap));
1633 }
1634
1635 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1636
1637 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1638}
1639
1640static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1641{
1642 struct gsm_mncc *modify = arg;
1643 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1644 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1645
1646 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1647
1648 /* bearer capability */
1649 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1650 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1651
1652 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1653
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001654 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001655}
1656
1657static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1658{
1659 struct gsm48_hdr *gh = msgb_l3(msg);
1660 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1661 struct tlv_parsed tp;
1662 struct gsm_mncc modify;
1663
1664 gsm48_stop_cc_timer(trans);
1665
1666 memset(&modify, 0, sizeof(struct gsm_mncc));
1667 modify.callref = trans->callref;
1668 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1669 /* bearer capability */
1670 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1671 modify.fields |= GSM48_IE_BEARER_CAP;
1672 gsm48_decode_bearer_cap(&modify.bearer_cap,
1673 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1674
1675 /* Create a copy of the bearer capability
1676 * in the transaction struct, so we can use
1677 * this information later */
1678 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1679 sizeof(trans->bearer_cap));
1680 }
1681 /* cause */
1682 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1683 modify.fields |= MNCC_F_CAUSE;
1684 gsm48_decode_cause(&modify.cause,
1685 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1686 }
1687
1688 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1689
1690 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1691}
1692
1693static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1694{
1695 struct gsm_mncc *modify = arg;
1696 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1697 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1698
1699 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1700
1701 /* bearer capability */
1702 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1703 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1704 /* cause */
1705 gsm48_encode_cause(msg, 1, &modify->cause);
1706
1707 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1708
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001709 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001710}
1711
1712static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1713{
1714 struct gsm_mncc *notify = arg;
1715 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1716 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1717
1718 gh->msg_type = GSM48_MT_CC_NOTIFY;
1719
1720 /* notify */
1721 gsm48_encode_notify(msg, notify->notify);
1722
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001723 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001724}
1725
1726static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1727{
1728 struct gsm48_hdr *gh = msgb_l3(msg);
1729 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1730// struct tlv_parsed tp;
1731 struct gsm_mncc notify;
1732
1733 memset(&notify, 0, sizeof(struct gsm_mncc));
1734 notify.callref = trans->callref;
1735// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1736 if (payload_len >= 1)
1737 gsm48_decode_notify(&notify.notify, gh->data);
1738
1739 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1740}
1741
1742static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1743{
1744 struct gsm_mncc *user = arg;
1745 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1746 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1747
1748 gh->msg_type = GSM48_MT_CC_USER_INFO;
1749
1750 /* user-user */
1751 if (user->fields & MNCC_F_USERUSER)
1752 gsm48_encode_useruser(msg, 1, &user->useruser);
1753 /* more data */
1754 if (user->more)
1755 gsm48_encode_more(msg);
1756
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001757 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001758}
1759
1760static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1761{
1762 struct gsm48_hdr *gh = msgb_l3(msg);
1763 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1764 struct tlv_parsed tp;
1765 struct gsm_mncc user;
1766
1767 memset(&user, 0, sizeof(struct gsm_mncc));
1768 user.callref = trans->callref;
1769 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1770 /* user-user */
1771 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1772 user.fields |= MNCC_F_USERUSER;
1773 gsm48_decode_useruser(&user.useruser,
1774 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1775 }
1776 /* more data */
1777 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1778 user.more = 1;
1779
1780 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1781}
1782
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001783static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1784 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1785 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001786{
1787 uint8_t data[sizeof(struct gsm_mncc)];
1788 struct gsm_mncc_rtp *rtp;
1789
1790 memset(&data, 0, sizeof(data));
1791 rtp = (struct gsm_mncc_rtp *) &data[0];
1792
1793 rtp->callref = callref;
1794 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001795 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001796 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1797 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001798 }
Harald Welte27989d42018-06-21 20:39:20 +02001799 rtp->payload_type = payload_type;
1800 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001801 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001802}
1803
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001804static 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 +02001805{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001806 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001807}
1808
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001809static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001810{
1811 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001812
1813 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001814 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001815 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001816 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001817 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001818 return -EIO;
1819 }
1820 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001821 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001822 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001823 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001824 return 0;
1825 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001826 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001827
Harald Welte27989d42018-06-21 20:39:20 +02001828 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001829 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001830}
1831
1832/* Trigger TCH_RTP_CREATE acknowledgement */
1833int gsm48_tch_rtp_create(struct gsm_trans *trans)
1834{
1835 /* This function is called as soon as the port, on which the
1836 * mgcp-gw expects the incoming RTP stream from the remote
1837 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001838 struct msc_a *msc_a = trans->msc_a;
1839 struct gsm_network *net = msc_a_net(msc_a);
1840 struct call_leg *cl = msc_a->cc.call_leg;
1841 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001842 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001843 int mncc_payload_msg_type;
1844 struct sdp_audio_codec *codec;
Neels Hofmeyra001a702022-10-31 17:57:30 +01001845 const struct codec_mapping *m;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001846 struct sdp_audio_codecs *codecs;
Harald Welte27989d42018-06-21 20:39:20 +02001847
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001848 if (!rtp_cn) {
1849 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1850 return -EINVAL;
1851 }
1852
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001853 codec_filter_run(&trans->cc.codecs);
1854 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1855 codecs = &trans->cc.codecs.result.audio_codecs;
1856 if (!codecs->count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001857 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001858 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001859 return -EINVAL;
1860 }
1861
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001862 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
1863 codec = &codecs->codec[0];
1864 m = codec_mapping_by_subtype_name(codec->subtype_name);
1865 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02001866
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001867 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1868 if (!rtp_cn_local) {
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001869 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port to CN set up\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001870 return -EINVAL;
1871 }
1872
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001873 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
1874 codec->payload_type, mncc_payload_msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001875}
1876
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001877static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001878{
1879 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001880 struct call_leg *cl;
1881 struct rtp_stream *rtps;
1882 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001883
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001884 /* FIXME: in *rtp we should get the codec information of the remote
1885 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1886 * meaningful value based on this information but unfortunately we
1887 * can't do that yet because the mncc API can not signal dynamic
1888 * payload types yet. This must be fixed first. Also there may be
1889 * additional members necessary in trans->conn->rtp because we
1890 * somehow need to deal with dynamic payload types that do not
1891 * comply to 3gpp's assumptions of payload type numbers on the A
1892 * interface. See also related tickets: OS#3399 and OS1683 */
1893
Harald Welte27989d42018-06-21 20:39:20 +02001894 /* Find callref */
1895 trans = trans_find_by_callref(net, rtp->callref);
1896 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001897 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001898 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001899 return -EIO;
1900 }
1901 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001903 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001904 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001905 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001906 }
1907
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001908 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001909
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001910 cl = trans->msc_a->cc.call_leg;
1911 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1912
1913 if (!rtps) {
1914 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1915 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1916 return -EINVAL;
1917 }
1918
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001919 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1920 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1921 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1922 return -EINVAL;
1923 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001924 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1925 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 Hofmeyr58f40882023-03-08 04:04:27 +01002003 return tch_rtp_create(net, &msg->rtp);
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
Harald Welte27989d42018-06-21 20:39:20 +02002106 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002107 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002108 /* This condition will return before the common logging of the received MNCC message below, so
2109 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002110 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002111
Harald Welte27989d42018-06-21 20:39:20 +02002112 /* store setup information until paging succeeds */
2113 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2114
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002115 /* Request a channel. If Paging already started, paging_request_start() will append the new
2116 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002117 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2118 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002119 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002120 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002121 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002122 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002123 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002124 return 0;
2125 }
2126
2127 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002128 trans->msc_a = msc_a;
2129 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002130 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002131 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002132 } else {
2133 /* update the subscriber we deal with */
2134 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2135 }
2136
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002137 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002138
Philipp Maier9ca7b312018-10-10 17:00:49 +02002139 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002140 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002141
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002142 if (trans->msc_a)
2143 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002144
2145 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002146 if (!msc_a) {
2147 struct gsm_mncc rel = {
2148 .callref = data->callref,
2149 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002150 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002151 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2152 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002153 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002154 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2155 else
2156 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2157 trans->callref = 0;
2158 trans_free(trans);
2159 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002160 }
2161
2162 /* Find function for current state and message */
2163 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002164 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002165 && ((1 << trans->cc.state) & downstatelist[i].states))
2166 break;
2167 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002168 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002169 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002170 return 0;
2171 }
2172
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002173 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002174
2175 return rc;
2176}
2177
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002178struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2179{
2180 uint32_t callref;
2181
2182 switch (msg->msg_type) {
2183 case MNCC_BRIDGE:
2184 callref = msg->bridge.callref[0];
2185 break;
2186 case MNCC_RTP_CREATE:
2187 case MNCC_RTP_CONNECT:
2188 callref = msg->rtp.callref;
2189 break;
2190
2191 case MNCC_RTP_FREE:
2192 case MNCC_FRAME_DROP:
2193 case MNCC_FRAME_RECV:
2194 case GSM_TCHF_FRAME:
2195 case GSM_TCHF_FRAME_EFR:
2196 case GSM_TCHH_FRAME:
2197 case GSM_TCH_FRAME_AMR:
2198 return NULL;
2199
2200 default:
2201 callref = msg->signal.callref;
2202 break;
2203 }
2204
2205 return mncc_call_find_by_callref(callref);
2206}
2207
2208/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002209int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002210{
2211 const union mncc_msg *msg = arg;
2212 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002213
2214 if (msg->msg_type == MNCC_SETUP_REQ) {
2215 /* Incoming call to forward for inter-MSC Handover? */
2216 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2217 if (mncc_call)
2218 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2219 "Incoming call matches pending inter-MSC Handover Number\n");
2220 }
2221 if (!mncc_call) {
2222 /* Find already active MNCC FSM for this callref.
2223 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2224 * MNCC<->GSM-CC call handling. */
2225 mncc_call = mncc_find_by_callref_from_msg(msg);
2226 }
2227 if (mncc_call) {
2228 mncc_call_rx(mncc_call, msg);
2229 return 0;
2230 }
2231
2232 /* None of the above? Then it must be a normal GSM CC call related message. */
2233 return mncc_tx_to_gsm_cc(net, msg);
2234}
Harald Welte27989d42018-06-21 20:39:20 +02002235
2236static struct datastate {
2237 uint32_t states;
2238 int type;
2239 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2240} datastatelist[] = {
2241 /* mobile originating call establishment */
2242 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2243 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2244 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2245 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2246 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2247 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2248 /* mobile terminating call establishment */
2249 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2250 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2251 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2252 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2253 {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 */
2254 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2255 /* signalling during call */
2256 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2257 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2258 {SBIT(GSM_CSTATE_ACTIVE),
2259 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2260 {ALL_STATES,
2261 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2262 {ALL_STATES,
2263 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2264 {ALL_STATES,
2265 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2266 {SBIT(GSM_CSTATE_ACTIVE),
2267 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2268 {SBIT(GSM_CSTATE_ACTIVE),
2269 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2270 {SBIT(GSM_CSTATE_ACTIVE),
2271 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2272 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2273 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2274 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2275 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2276 {SBIT(GSM_CSTATE_ACTIVE),
2277 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2278 /* clearing */
2279 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2280 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2281 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2282 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2283 {ALL_STATES, /* 5.4.3.4 */
2284 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2285};
2286
2287#define DATASLLEN \
2288 (sizeof(datastatelist) / sizeof(struct datastate))
2289
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002290int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002291{
2292 struct gsm48_hdr *gh = msgb_l3(msg);
2293 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2294 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2295 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002296 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2297 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002298 int i, rc = 0;
2299
2300 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002301 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002302 return -EINVAL;
2303 }
2304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002305 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002306 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002307 return -EINVAL;
2308 }
2309
2310 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002311 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002312
Harald Welte27989d42018-06-21 20:39:20 +02002313 /* Create transaction */
2314 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002315 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002316 trans = trans_alloc(net, vsub,
2317 TRANS_CC,
2318 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002319 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002320 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002321 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002322 GSM48_PDISC_CC | (transaction_id << 4),
2323 GSM48_MT_CC_RELEASE_COMPL);
2324 return -ENOMEM;
2325 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002326 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2327 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2328 trans_free(trans);
2329 return -EINVAL;
2330 }
2331
Harald Welte27989d42018-06-21 20:39:20 +02002332 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002333 msc_a_get(msc_a, MSC_A_USE_CC);
2334 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002335 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002336
2337 /* An earlier CM Service Request for this CC message now has concluded */
2338 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2339 LOG_MSC_A(msc_a, LOGL_ERROR,
2340 "Creating new CC transaction without prior CM Service Request\n");
2341 else
2342 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002343 }
2344
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002345 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2346 gsm48_cc_state_name(trans->cc.state));
2347
Harald Welte27989d42018-06-21 20:39:20 +02002348 /* find function for current state and message */
2349 for (i = 0; i < DATASLLEN; i++)
2350 if ((msg_type == datastatelist[i].type)
2351 && ((1 << trans->cc.state) & datastatelist[i].states))
2352 break;
2353 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002354 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002355
2356 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2357 * transaction right away. */
2358 if (trans->cc.state == GSM_CSTATE_NULL) {
2359 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2360 " -- disarding new CC transaction right away\n");
2361 trans_free(trans);
2362 }
Harald Welte27989d42018-06-21 20:39:20 +02002363 return 0;
2364 }
2365
2366 assert(trans->vsub);
2367
2368 rc = datastatelist[i].rout(trans, msg);
2369
Harald Welte27989d42018-06-21 20:39:20 +02002370 return rc;
2371}