blob: 685bf608f303c62f9d89032964842eeae0a8faf3 [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 Hofmeyrff7074a2019-02-28 05:50:06 +0100669 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
670 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200671
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200672 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200673
674 /* indicate setup to MNCC */
675 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
676
677 /* MNCC code will modify the channel asynchronously, we should
678 * ipaccess-bind only after the modification has been made to the
679 * lchan->tch_mode */
680 return 0;
681}
682
683static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
684{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100685 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200686 struct gsm48_hdr *gh;
687 struct gsm_mncc *setup = arg;
688 int rc, trans_id;
689
690 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
691
692 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700693 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100694 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200695 "This is not allowed!\n");
696 /* Temporarily out of order */
697 rc = mncc_release_ind(trans->net, trans, trans->callref,
698 GSM48_CAUSE_LOC_PRN_S_LU,
699 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
700 trans->callref = 0;
701 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200702 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200703 return rc;
704 }
705
706 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100707 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200708 if (trans_id < 0) {
709 /* no free transaction ID */
710 rc = mncc_release_ind(trans->net, trans, trans->callref,
711 GSM48_CAUSE_LOC_PRN_S_LU,
712 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
713 trans->callref = 0;
714 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200715 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200716 return rc;
717 }
718 trans->transaction_id = trans_id;
719
720 gh->msg_type = GSM48_MT_CC_SETUP;
721
722 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
723
724 /* bearer capability */
725 if (setup->fields & MNCC_F_BEARER_CAP) {
726 /* Create a copy of the bearer capability in the transaction struct, so we
727 * can use this information later */
728 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
729 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
730 }
731 /* facility */
732 if (setup->fields & MNCC_F_FACILITY)
733 gsm48_encode_facility(msg, 0, &setup->facility);
734 /* progress */
735 if (setup->fields & MNCC_F_PROGRESS)
736 gsm48_encode_progress(msg, 0, &setup->progress);
737 /* calling party BCD number */
738 if (setup->fields & MNCC_F_CALLING)
739 gsm48_encode_calling(msg, &setup->calling);
740 /* called party BCD number */
741 if (setup->fields & MNCC_F_CALLED)
742 gsm48_encode_called(msg, &setup->called);
743 /* user-user */
744 if (setup->fields & MNCC_F_USERUSER)
745 gsm48_encode_useruser(msg, 0, &setup->useruser);
746 /* redirecting party BCD number */
747 if (setup->fields & MNCC_F_REDIRECTING)
748 gsm48_encode_redirecting(msg, &setup->redirecting);
749 /* signal */
750 if (setup->fields & MNCC_F_SIGNAL)
751 gsm48_encode_signal(msg, setup->signal);
752
753 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
754
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200755 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200756
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100757 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200758}
759
760static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
761{
762 struct gsm48_hdr *gh = msgb_l3(msg);
763 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
764 struct tlv_parsed tp;
765 struct gsm_mncc call_conf;
766 int rc;
767
768 gsm48_stop_cc_timer(trans);
769 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
770
771 memset(&call_conf, 0, sizeof(struct gsm_mncc));
772 call_conf.callref = trans->callref;
773
774 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
775#if 0
776 /* repeat */
777 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
778 call_conf.repeat = 1;
779 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
780 call_conf.repeat = 2;
781#endif
782 /* bearer capability */
783 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
784 call_conf.fields |= MNCC_F_BEARER_CAP;
785 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
786 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
787
788 /* Create a copy of the bearer capability
789 * in the transaction struct, so we can use
790 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100791 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200792 sizeof(trans->bearer_cap));
793 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100794
Harald Welte27989d42018-06-21 20:39:20 +0200795 /* cause */
796 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
797 call_conf.fields |= MNCC_F_CAUSE;
798 gsm48_decode_cause(&call_conf.cause,
799 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
800 }
801 /* cc cap */
802 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
803 call_conf.fields |= MNCC_F_CCCAP;
804 gsm48_decode_cccap(&call_conf.cccap,
805 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
806 }
807
808 /* IMSI of called subscriber */
809 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
810
811 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
812
813 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100814 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200815
816 /* don't continue, if there were problems with
817 * the call assignment. */
818 if (rc)
819 return rc;
820
821 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
822 &call_conf);
823}
824
825static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
826{
827 struct gsm_mncc *proceeding = arg;
828 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
829 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
830 int rc;
831
832 gh->msg_type = GSM48_MT_CC_CALL_PROC;
833
834 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
835
836 /* bearer capability */
837 if (proceeding->fields & MNCC_F_BEARER_CAP) {
838 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
839 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
840 }
841 /* facility */
842 if (proceeding->fields & MNCC_F_FACILITY)
843 gsm48_encode_facility(msg, 0, &proceeding->facility);
844 /* progress */
845 if (proceeding->fields & MNCC_F_PROGRESS)
846 gsm48_encode_progress(msg, 0, &proceeding->progress);
847
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100848 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200849 if (rc)
850 return rc;
851
852 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100853 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200854}
855
856static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
857{
858 struct gsm48_hdr *gh = msgb_l3(msg);
859 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
860 struct tlv_parsed tp;
861 struct gsm_mncc alerting;
862
863 gsm48_stop_cc_timer(trans);
864 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
865
866 memset(&alerting, 0, sizeof(struct gsm_mncc));
867 alerting.callref = trans->callref;
868 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
869 /* facility */
870 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
871 alerting.fields |= MNCC_F_FACILITY;
872 gsm48_decode_facility(&alerting.facility,
873 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
874 }
875
876 /* progress */
877 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
878 alerting.fields |= MNCC_F_PROGRESS;
879 gsm48_decode_progress(&alerting.progress,
880 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
881 }
882 /* ss-version */
883 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
884 alerting.fields |= MNCC_F_SSVERSION;
885 gsm48_decode_ssversion(&alerting.ssversion,
886 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
887 }
888
889 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
890
891 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
892 &alerting);
893}
894
895static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
896{
897 struct gsm_mncc *alerting = arg;
898 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
899 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
900
901 gh->msg_type = GSM48_MT_CC_ALERTING;
902
903 /* facility */
904 if (alerting->fields & MNCC_F_FACILITY)
905 gsm48_encode_facility(msg, 0, &alerting->facility);
906 /* progress */
907 if (alerting->fields & MNCC_F_PROGRESS)
908 gsm48_encode_progress(msg, 0, &alerting->progress);
909 /* user-user */
910 if (alerting->fields & MNCC_F_USERUSER)
911 gsm48_encode_useruser(msg, 0, &alerting->useruser);
912
913 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
914
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100915 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200916}
917
918static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
919{
920 struct gsm_mncc *progress = arg;
921 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
922 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
923
924 gh->msg_type = GSM48_MT_CC_PROGRESS;
925
926 /* progress */
927 gsm48_encode_progress(msg, 1, &progress->progress);
928 /* user-user */
929 if (progress->fields & MNCC_F_USERUSER)
930 gsm48_encode_useruser(msg, 0, &progress->useruser);
931
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100932 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200933}
934
935static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
936{
937 struct gsm_mncc *connect = arg;
938 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
939 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
940
941 gh->msg_type = GSM48_MT_CC_CONNECT;
942
943 gsm48_stop_cc_timer(trans);
944 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
945
946 /* facility */
947 if (connect->fields & MNCC_F_FACILITY)
948 gsm48_encode_facility(msg, 0, &connect->facility);
949 /* progress */
950 if (connect->fields & MNCC_F_PROGRESS)
951 gsm48_encode_progress(msg, 0, &connect->progress);
952 /* connected number */
953 if (connect->fields & MNCC_F_CONNECTED)
954 gsm48_encode_connected(msg, &connect->connected);
955 /* user-user */
956 if (connect->fields & MNCC_F_USERUSER)
957 gsm48_encode_useruser(msg, 0, &connect->useruser);
958
959 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
960
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100961 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200962}
963
964static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
965{
966 struct gsm48_hdr *gh = msgb_l3(msg);
967 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
968 struct tlv_parsed tp;
969 struct gsm_mncc connect;
970
971 gsm48_stop_cc_timer(trans);
972
973 memset(&connect, 0, sizeof(struct gsm_mncc));
974 connect.callref = trans->callref;
975 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
976 /* use subscriber as connected party number */
977 connect.fields |= MNCC_F_CONNECTED;
978 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
979 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
980
981 /* facility */
982 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
983 connect.fields |= MNCC_F_FACILITY;
984 gsm48_decode_facility(&connect.facility,
985 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
986 }
987 /* user-user */
988 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
989 connect.fields |= MNCC_F_USERUSER;
990 gsm48_decode_useruser(&connect.useruser,
991 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
992 }
993 /* ss-version */
994 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
995 connect.fields |= MNCC_F_SSVERSION;
996 gsm48_decode_ssversion(&connect.ssversion,
997 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
998 }
999
1000 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001001 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001002
1003 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1004}
1005
1006
1007static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1008{
1009 struct gsm_mncc connect_ack;
1010
1011 gsm48_stop_cc_timer(trans);
1012
1013 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001014 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 +02001015
1016 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1017 connect_ack.callref = trans->callref;
1018
1019 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1020 &connect_ack);
1021}
1022
1023static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1024{
1025 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1026 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1027
1028 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1029
1030 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1031
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001032 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001033}
1034
1035static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1036{
1037 struct gsm48_hdr *gh = msgb_l3(msg);
1038 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1039 struct tlv_parsed tp;
1040 struct gsm_mncc disc;
1041
1042 gsm48_stop_cc_timer(trans);
1043
1044 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1045
1046 memset(&disc, 0, sizeof(struct gsm_mncc));
1047 disc.callref = trans->callref;
1048 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1049 /* cause */
1050 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1051 disc.fields |= MNCC_F_CAUSE;
1052 gsm48_decode_cause(&disc.cause,
1053 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1054 }
1055 /* facility */
1056 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1057 disc.fields |= MNCC_F_FACILITY;
1058 gsm48_decode_facility(&disc.facility,
1059 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1060 }
1061 /* user-user */
1062 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1063 disc.fields |= MNCC_F_USERUSER;
1064 gsm48_decode_useruser(&disc.useruser,
1065 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1066 }
1067 /* ss-version */
1068 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1069 disc.fields |= MNCC_F_SSVERSION;
1070 gsm48_decode_ssversion(&disc.ssversion,
1071 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1072 }
1073
1074 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001075}
1076
1077static struct gsm_mncc_cause default_cause = {
1078 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1079 .coding = 0,
1080 .rec = 0,
1081 .rec_val = 0,
1082 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1083 .diag_len = 0,
1084 .diag = { 0 },
1085};
1086
1087static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1088{
1089 struct gsm_mncc *disc = arg;
1090 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1091 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1092
1093 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1094
1095 gsm48_stop_cc_timer(trans);
1096 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1097
1098 /* cause */
1099 if (disc->fields & MNCC_F_CAUSE)
1100 gsm48_encode_cause(msg, 1, &disc->cause);
1101 else
1102 gsm48_encode_cause(msg, 1, &default_cause);
1103
1104 /* facility */
1105 if (disc->fields & MNCC_F_FACILITY)
1106 gsm48_encode_facility(msg, 0, &disc->facility);
1107 /* progress */
1108 if (disc->fields & MNCC_F_PROGRESS)
1109 gsm48_encode_progress(msg, 0, &disc->progress);
1110 /* user-user */
1111 if (disc->fields & MNCC_F_USERUSER)
1112 gsm48_encode_useruser(msg, 0, &disc->useruser);
1113
1114 /* store disconnect cause for T306 expiry */
1115 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1116
1117 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1118
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001119 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001120}
1121
1122static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1123{
1124 struct gsm48_hdr *gh = msgb_l3(msg);
1125 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1126 struct tlv_parsed tp;
1127 struct gsm_mncc rel;
1128 int rc;
1129
1130 gsm48_stop_cc_timer(trans);
1131
1132 memset(&rel, 0, sizeof(struct gsm_mncc));
1133 rel.callref = trans->callref;
1134 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1135 /* cause */
1136 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1137 rel.fields |= MNCC_F_CAUSE;
1138 gsm48_decode_cause(&rel.cause,
1139 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1140 }
1141 /* facility */
1142 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1143 rel.fields |= MNCC_F_FACILITY;
1144 gsm48_decode_facility(&rel.facility,
1145 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1146 }
1147 /* user-user */
1148 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1149 rel.fields |= MNCC_F_USERUSER;
1150 gsm48_decode_useruser(&rel.useruser,
1151 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1152 }
1153 /* ss-version */
1154 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1155 rel.fields |= MNCC_F_SSVERSION;
1156 gsm48_decode_ssversion(&rel.ssversion,
1157 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1158 }
1159
1160 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1161 /* release collision 5.4.5 */
1162 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1163 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001164 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001165 GSM48_PDISC_CC | (trans->transaction_id << 4),
1166 GSM48_MT_CC_RELEASE_COMPL);
1167 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1168 }
1169
1170 new_cc_state(trans, GSM_CSTATE_NULL);
1171
1172 trans->callref = 0;
1173 trans_free(trans);
1174
1175 return rc;
1176}
1177
1178static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1179{
1180 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001181 struct msgb *msg;
1182 struct gsm48_hdr *gh;
1183
1184 if (!trans->msc_a) {
1185 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1186 return -EINVAL;
1187 }
1188
1189 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1190 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001191
1192 gh->msg_type = GSM48_MT_CC_RELEASE;
1193
1194 gsm48_stop_cc_timer(trans);
1195 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1196
1197 /* cause */
1198 if (rel->fields & MNCC_F_CAUSE)
1199 gsm48_encode_cause(msg, 0, &rel->cause);
1200 /* facility */
1201 if (rel->fields & MNCC_F_FACILITY)
1202 gsm48_encode_facility(msg, 0, &rel->facility);
1203 /* user-user */
1204 if (rel->fields & MNCC_F_USERUSER)
1205 gsm48_encode_useruser(msg, 0, &rel->useruser);
1206
1207 trans->cc.T308_second = 0;
1208 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1209
1210 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1211 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1212
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001213 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001214}
1215
1216static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1217{
1218 struct gsm48_hdr *gh = msgb_l3(msg);
1219 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1220 struct tlv_parsed tp;
1221 struct gsm_mncc rel;
1222 int rc = 0;
1223
1224 gsm48_stop_cc_timer(trans);
1225
1226 memset(&rel, 0, sizeof(struct gsm_mncc));
1227 rel.callref = trans->callref;
1228 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1229 /* cause */
1230 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1231 rel.fields |= MNCC_F_CAUSE;
1232 gsm48_decode_cause(&rel.cause,
1233 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1234 }
1235 /* facility */
1236 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1237 rel.fields |= MNCC_F_FACILITY;
1238 gsm48_decode_facility(&rel.facility,
1239 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1240 }
1241 /* user-user */
1242 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1243 rel.fields |= MNCC_F_USERUSER;
1244 gsm48_decode_useruser(&rel.useruser,
1245 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1246 }
1247 /* ss-version */
1248 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1249 rel.fields |= MNCC_F_SSVERSION;
1250 gsm48_decode_ssversion(&rel.ssversion,
1251 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1252 }
1253
1254 if (trans->callref) {
1255 switch (trans->cc.state) {
1256 case GSM_CSTATE_CALL_PRESENT:
1257 rc = mncc_recvmsg(trans->net, trans,
1258 MNCC_REJ_IND, &rel);
1259 break;
1260 case GSM_CSTATE_RELEASE_REQ:
1261 rc = mncc_recvmsg(trans->net, trans,
1262 MNCC_REL_CNF, &rel);
1263 break;
1264 default:
1265 rc = mncc_recvmsg(trans->net, trans,
1266 MNCC_REL_IND, &rel);
1267 }
1268 }
1269
1270 trans->callref = 0;
1271 trans_free(trans);
1272
1273 return rc;
1274}
1275
1276static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1277{
1278 struct gsm_mncc *rel = arg;
1279 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1280 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1281 int ret;
1282
1283 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1284
1285 trans->callref = 0;
1286
1287 gsm48_stop_cc_timer(trans);
1288
1289 /* cause */
1290 if (rel->fields & MNCC_F_CAUSE)
1291 gsm48_encode_cause(msg, 0, &rel->cause);
1292 /* facility */
1293 if (rel->fields & MNCC_F_FACILITY)
1294 gsm48_encode_facility(msg, 0, &rel->facility);
1295 /* user-user */
1296 if (rel->fields & MNCC_F_USERUSER)
1297 gsm48_encode_useruser(msg, 0, &rel->useruser);
1298
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001299 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001300
1301 trans_free(trans);
1302
1303 return ret;
1304}
1305
1306static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1307{
1308 struct gsm48_hdr *gh = msgb_l3(msg);
1309 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1310 struct tlv_parsed tp;
1311 struct gsm_mncc fac;
1312
1313 memset(&fac, 0, sizeof(struct gsm_mncc));
1314 fac.callref = trans->callref;
1315 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1316 /* facility */
1317 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1318 fac.fields |= MNCC_F_FACILITY;
1319 gsm48_decode_facility(&fac.facility,
1320 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1321 }
1322 /* ss-version */
1323 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1324 fac.fields |= MNCC_F_SSVERSION;
1325 gsm48_decode_ssversion(&fac.ssversion,
1326 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1327 }
1328
1329 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1330}
1331
1332static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1333{
1334 struct gsm_mncc *fac = arg;
1335 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1336 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1337
1338 gh->msg_type = GSM48_MT_CC_FACILITY;
1339
1340 /* facility */
1341 gsm48_encode_facility(msg, 1, &fac->facility);
1342
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001343 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001344}
1345
1346static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1347{
1348 struct gsm_mncc hold;
1349
1350 memset(&hold, 0, sizeof(struct gsm_mncc));
1351 hold.callref = trans->callref;
1352 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1353}
1354
1355static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1356{
1357 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1358 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1359
1360 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1361
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001362 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001363}
1364
1365static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1366{
1367 struct gsm_mncc *hold_rej = arg;
1368 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1369 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1370
1371 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1372
1373 /* cause */
1374 if (hold_rej->fields & MNCC_F_CAUSE)
1375 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1376 else
1377 gsm48_encode_cause(msg, 1, &default_cause);
1378
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001379 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001380}
1381
1382static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1383{
1384 struct gsm_mncc retrieve;
1385
1386 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1387 retrieve.callref = trans->callref;
1388 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1389 &retrieve);
1390}
1391
1392static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1393{
1394 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1395 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1396
1397 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1398
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001399 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001400}
1401
1402static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1403{
1404 struct gsm_mncc *retrieve_rej = arg;
1405 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1406 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1407
1408 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1409
1410 /* cause */
1411 if (retrieve_rej->fields & MNCC_F_CAUSE)
1412 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1413 else
1414 gsm48_encode_cause(msg, 1, &default_cause);
1415
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001416 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001417}
1418
1419static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1420{
1421 struct gsm48_hdr *gh = msgb_l3(msg);
1422 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1423 struct tlv_parsed tp;
1424 struct gsm_mncc dtmf;
1425
1426 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1427 dtmf.callref = trans->callref;
1428 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1429 /* keypad facility */
1430 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1431 dtmf.fields |= MNCC_F_KEYPAD;
1432 gsm48_decode_keypad(&dtmf.keypad,
1433 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1434 }
1435
1436 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1437}
1438
1439static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1440{
1441 struct gsm_mncc *dtmf = arg;
1442 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1443 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1444
1445 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1446
1447 /* keypad */
1448 if (dtmf->fields & MNCC_F_KEYPAD)
1449 gsm48_encode_keypad(msg, dtmf->keypad);
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_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1455{
1456 struct gsm_mncc *dtmf = arg;
1457 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1458 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1459
1460 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1461
1462 /* cause */
1463 if (dtmf->fields & MNCC_F_CAUSE)
1464 gsm48_encode_cause(msg, 1, &dtmf->cause);
1465 else
1466 gsm48_encode_cause(msg, 1, &default_cause);
1467
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001468 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001469}
1470
1471static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1472{
1473 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1474 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1475
1476 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1477
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001478 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001479}
1480
1481static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1482{
1483 struct gsm_mncc dtmf;
1484
1485 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1486 dtmf.callref = trans->callref;
1487
1488 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1489}
1490
1491static int gsm48_cc_rx_modify(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 modify;
1497
1498 memset(&modify, 0, sizeof(struct gsm_mncc));
1499 modify.callref = trans->callref;
1500 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1501 /* bearer capability */
1502 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1503 modify.fields |= MNCC_F_BEARER_CAP;
1504 gsm48_decode_bearer_cap(&modify.bearer_cap,
1505 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1506
1507 /* Create a copy of the bearer capability
1508 * in the transaction struct, so we can use
1509 * this information later */
1510 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1511 sizeof(trans->bearer_cap));
1512 }
1513
1514 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1515
1516 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1517}
1518
1519static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1520{
1521 struct gsm_mncc *modify = arg;
1522 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1524
1525 gh->msg_type = GSM48_MT_CC_MODIFY;
1526
1527 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1528
1529 /* bearer capability */
1530 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1531 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1532
1533 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1534
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001535 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001536}
1537
1538static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1539{
1540 struct gsm48_hdr *gh = msgb_l3(msg);
1541 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1542 struct tlv_parsed tp;
1543 struct gsm_mncc modify;
1544
1545 gsm48_stop_cc_timer(trans);
1546
1547 memset(&modify, 0, sizeof(struct gsm_mncc));
1548 modify.callref = trans->callref;
1549 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1550 /* bearer capability */
1551 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1552 modify.fields |= MNCC_F_BEARER_CAP;
1553 gsm48_decode_bearer_cap(&modify.bearer_cap,
1554 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1555
1556 /* Create a copy of the bearer capability
1557 * in the transaction struct, so we can use
1558 * this information later */
1559 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1560 sizeof(trans->bearer_cap));
1561 }
1562
1563 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1564
1565 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1566}
1567
1568static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1569{
1570 struct gsm_mncc *modify = arg;
1571 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1572 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1573
1574 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1575
1576 /* bearer capability */
1577 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1578 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1579
1580 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1581
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001582 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001583}
1584
1585static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1586{
1587 struct gsm48_hdr *gh = msgb_l3(msg);
1588 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1589 struct tlv_parsed tp;
1590 struct gsm_mncc modify;
1591
1592 gsm48_stop_cc_timer(trans);
1593
1594 memset(&modify, 0, sizeof(struct gsm_mncc));
1595 modify.callref = trans->callref;
1596 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1597 /* bearer capability */
1598 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1599 modify.fields |= GSM48_IE_BEARER_CAP;
1600 gsm48_decode_bearer_cap(&modify.bearer_cap,
1601 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1602
1603 /* Create a copy of the bearer capability
1604 * in the transaction struct, so we can use
1605 * this information later */
1606 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1607 sizeof(trans->bearer_cap));
1608 }
1609 /* cause */
1610 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1611 modify.fields |= MNCC_F_CAUSE;
1612 gsm48_decode_cause(&modify.cause,
1613 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1614 }
1615
1616 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1617
1618 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1619}
1620
1621static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1622{
1623 struct gsm_mncc *modify = arg;
1624 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1625 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1626
1627 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1628
1629 /* bearer capability */
1630 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1631 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1632 /* cause */
1633 gsm48_encode_cause(msg, 1, &modify->cause);
1634
1635 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1636
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001637 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001638}
1639
1640static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1641{
1642 struct gsm_mncc *notify = arg;
1643 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1644 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1645
1646 gh->msg_type = GSM48_MT_CC_NOTIFY;
1647
1648 /* notify */
1649 gsm48_encode_notify(msg, notify->notify);
1650
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001651 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001652}
1653
1654static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1655{
1656 struct gsm48_hdr *gh = msgb_l3(msg);
1657 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1658// struct tlv_parsed tp;
1659 struct gsm_mncc notify;
1660
1661 memset(&notify, 0, sizeof(struct gsm_mncc));
1662 notify.callref = trans->callref;
1663// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1664 if (payload_len >= 1)
1665 gsm48_decode_notify(&notify.notify, gh->data);
1666
1667 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1668}
1669
1670static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1671{
1672 struct gsm_mncc *user = arg;
1673 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1674 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1675
1676 gh->msg_type = GSM48_MT_CC_USER_INFO;
1677
1678 /* user-user */
1679 if (user->fields & MNCC_F_USERUSER)
1680 gsm48_encode_useruser(msg, 1, &user->useruser);
1681 /* more data */
1682 if (user->more)
1683 gsm48_encode_more(msg);
1684
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001685 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001686}
1687
1688static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1689{
1690 struct gsm48_hdr *gh = msgb_l3(msg);
1691 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1692 struct tlv_parsed tp;
1693 struct gsm_mncc user;
1694
1695 memset(&user, 0, sizeof(struct gsm_mncc));
1696 user.callref = trans->callref;
1697 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1698 /* user-user */
1699 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1700 user.fields |= MNCC_F_USERUSER;
1701 gsm48_decode_useruser(&user.useruser,
1702 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1703 }
1704 /* more data */
1705 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1706 user.more = 1;
1707
1708 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1709}
1710
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001711static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1712 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1713 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001714{
1715 uint8_t data[sizeof(struct gsm_mncc)];
1716 struct gsm_mncc_rtp *rtp;
1717
1718 memset(&data, 0, sizeof(data));
1719 rtp = (struct gsm_mncc_rtp *) &data[0];
1720
1721 rtp->callref = callref;
1722 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001723 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001724 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1725 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001726 }
Harald Welte27989d42018-06-21 20:39:20 +02001727 rtp->payload_type = payload_type;
1728 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001729 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001730}
1731
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001732static 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 +02001733{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001734 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001735}
1736
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001737static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001738{
1739 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001740
1741 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001742 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001743 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001744 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001745 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001746 return -EIO;
1747 }
1748 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001749 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001750 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001751 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001752 return 0;
1753 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001754 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001755
Harald Welte27989d42018-06-21 20:39:20 +02001756 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001757 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001758}
1759
1760/* Trigger TCH_RTP_CREATE acknowledgement */
1761int gsm48_tch_rtp_create(struct gsm_trans *trans)
1762{
1763 /* This function is called as soon as the port, on which the
1764 * mgcp-gw expects the incoming RTP stream from the remote
1765 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001766 struct msc_a *msc_a = trans->msc_a;
1767 struct gsm_network *net = msc_a_net(msc_a);
1768 struct call_leg *cl = msc_a->cc.call_leg;
1769 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001770 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1771 uint32_t payload_type;
1772 int payload_msg_type;
1773 const struct mgcp_conn_peer *mgcp_info;
Neels Hofmeyra001a702022-10-31 17:57:30 +01001774 const struct codec_mapping *m;
Harald Welte27989d42018-06-21 20:39:20 +02001775
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001776 if (!rtp_cn) {
1777 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1778 return -EINVAL;
1779 }
1780
1781 if (!rtp_cn->codec_known) {
1782 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1783 "Cannot RTP CREATE to MNCC, no codec set up for the RTP CN side\n");
1784 return -EINVAL;
1785 }
1786
1787 /* Codec */
Neels Hofmeyra001a702022-10-31 17:57:30 +01001788 m = codec_mapping_by_mgcp_codec(rtp_cn->codec);
1789 if (!m) {
1790 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
1791 "Cannot RTP CREATE to MNCC, cannot resolve codec '%s'\n",
1792 osmo_mgcpc_codec_name(rtp_cn->codec));
1793 return -EINVAL;
1794 }
1795 payload_msg_type = m->mncc_payload_msg_type;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001796
1797 /* Payload Type number */
1798 mgcp_info = osmo_mgcpc_ep_ci_get_rtp_info(rtp_cn->ci);
Neels Hofmeyr43e8d4d2019-08-30 01:05:58 +02001799 if (mgcp_info && mgcp_info->ptmap_len)
1800 payload_type = map_codec_to_pt(mgcp_info->ptmap, mgcp_info->ptmap_len, rtp_cn->codec);
1801 else
1802 payload_type = rtp_cn->codec;
Harald Welte27989d42018-06-21 20:39:20 +02001803
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001804 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1805 if (!rtp_cn_local) {
1806 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1807 return -EINVAL;
1808 }
1809
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001810 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, payload_type, payload_msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001811}
1812
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001813static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001814{
1815 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001816 struct call_leg *cl;
1817 struct rtp_stream *rtps;
1818 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001819
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001820 /* FIXME: in *rtp we should get the codec information of the remote
1821 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1822 * meaningful value based on this information but unfortunately we
1823 * can't do that yet because the mncc API can not signal dynamic
1824 * payload types yet. This must be fixed first. Also there may be
1825 * additional members necessary in trans->conn->rtp because we
1826 * somehow need to deal with dynamic payload types that do not
1827 * comply to 3gpp's assumptions of payload type numbers on the A
1828 * interface. See also related tickets: OS#3399 and OS1683 */
1829
Harald Welte27989d42018-06-21 20:39:20 +02001830 /* Find callref */
1831 trans = trans_find_by_callref(net, rtp->callref);
1832 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001833 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001834 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001835 return -EIO;
1836 }
1837 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001838 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001839 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001840 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001841 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001842 }
1843
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001844 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001845
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001846 cl = trans->msc_a->cc.call_leg;
1847 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1848
1849 if (!rtps) {
1850 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1851 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1852 return -EINVAL;
1853 }
1854
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001855 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1856 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1857 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1858 return -EINVAL;
1859 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001860 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1861 rtp_stream_commit(rtps);
1862 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001863}
1864
1865static struct downstate {
1866 uint32_t states;
1867 int type;
1868 int (*rout) (struct gsm_trans *trans, void *arg);
1869} downstatelist[] = {
1870 /* mobile originating call establishment */
1871 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1872 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1873 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1874 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1875 {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 */
1876 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1877 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1878 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1879 /* mobile terminating call establishment */
1880 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1881 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1882 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1883 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1884 /* signalling during call */
1885 {SBIT(GSM_CSTATE_ACTIVE),
1886 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1887 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1888 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1889 {ALL_STATES,
1890 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1891 {ALL_STATES,
1892 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1893 {ALL_STATES,
1894 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1895 {SBIT(GSM_CSTATE_ACTIVE),
1896 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1897 {SBIT(GSM_CSTATE_ACTIVE),
1898 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1899 {SBIT(GSM_CSTATE_ACTIVE),
1900 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1901 {SBIT(GSM_CSTATE_ACTIVE),
1902 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1903 {SBIT(GSM_CSTATE_ACTIVE),
1904 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1905 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1906 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1907 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1908 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1909 {SBIT(GSM_CSTATE_ACTIVE),
1910 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1911 /* clearing */
1912 {SBIT(GSM_CSTATE_INITIATED),
1913 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1914 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1915 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1916 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1917 MNCC_REL_REQ, gsm48_cc_tx_release},
1918};
1919
1920#define DOWNSLLEN \
1921 (sizeof(downstatelist) / sizeof(struct downstate))
1922
1923
Philipp Maiercd64af72019-08-01 09:46:40 +02001924static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001925{
1926 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001927 struct msc_a *msc_a = NULL;
1928 struct gsm_trans *trans = NULL;
1929 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001930
Harald Welte27989d42018-06-21 20:39:20 +02001931 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001932 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001933 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001934 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001935 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001936 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001937 return rc;
1938 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001939 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001940 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001941 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001942 case MNCC_RTP_FREE:
1943 /* unused right now */
1944 return -EIO;
1945
1946 case MNCC_FRAME_DROP:
1947 case MNCC_FRAME_RECV:
1948 case GSM_TCHF_FRAME:
1949 case GSM_TCHF_FRAME_EFR:
1950 case GSM_TCHH_FRAME:
1951 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001952 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001953 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001954 return -ENOTSUP;
1955 }
1956
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001957 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001958
1959 /* Find callref */
1960 trans = trans_find_by_callref(net, data->callref);
1961
1962 /* Callref unknown */
1963 if (!trans) {
1964 struct vlr_subscr *vsub;
1965
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001966 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001967 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001968 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001969 /* Invalid call reference */
1970 return mncc_release_ind(net, NULL, data->callref,
1971 GSM48_CAUSE_LOC_PRN_S_LU,
1972 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1973 }
1974 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001975 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001976 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001977 /* Invalid number */
1978 return mncc_release_ind(net, NULL, data->callref,
1979 GSM48_CAUSE_LOC_PRN_S_LU,
1980 GSM48_CC_CAUSE_INV_NR_FORMAT);
1981 }
1982 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001983 if (data->called.number[0]) {
1984 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1985 if (!vsub)
1986 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001987 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001988 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001989 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001990 if (!vsub)
1991 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001992 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001993 }
1994 if (!vsub)
1995 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02001996 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02001997 /* update the subscriber we deal with */
1998 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1999
Harald Welte27989d42018-06-21 20:39:20 +02002000 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002001 if (!vsub->lu_complete) {
2002 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002003 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002004 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002005 /* Temporarily out of order */
2006 return mncc_release_ind(net, NULL, data->callref,
2007 GSM48_CAUSE_LOC_PRN_S_LU,
2008 GSM48_CC_CAUSE_DEST_OOO);
2009 }
Keith Whyte991bb422019-08-08 15:43:40 +02002010
2011 /* Find valid conn */
2012 msc_a = msc_a_for_vsub(vsub, true);
2013
2014 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2015 if (!net->call_waiting && msc_a) {
2016 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2017 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2018 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2019 "rx '%s' for subscriber %s with trans state (%s)"
2020 " rejecting with USER_BUSY\n",
2021 get_mncc_name(msg->msg_type), data->called.number,
2022 gsm48_cc_state_name(existing_cc_trans->cc.state));
2023 return mncc_release_ind(net, NULL, data->callref,
2024 GSM48_CAUSE_LOC_PRN_S_LU,
2025 GSM48_CC_CAUSE_USER_BUSY);
2026 }
2027 }
2028
Harald Welte27989d42018-06-21 20:39:20 +02002029 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002030 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002031 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002032 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002033 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002034 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002035 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002036 mncc_release_ind(net, NULL, data->callref,
2037 GSM48_CAUSE_LOC_PRN_S_LU,
2038 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2039 return -ENOMEM;
2040 }
2041
Harald Welte27989d42018-06-21 20:39:20 +02002042 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002043 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002044 /* This condition will return before the common logging of the received MNCC message below, so
2045 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002046 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002047
Harald Welte27989d42018-06-21 20:39:20 +02002048 /* store setup information until paging succeeds */
2049 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2050
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002051 /* Request a channel. If Paging already started, paging_request_start() will append the new
2052 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002053 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2054 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002055 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002056 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002057 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002058 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002059 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002060 return 0;
2061 }
2062
2063 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002064 trans->msc_a = msc_a;
2065 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002066 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002067 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002068 } else {
2069 /* update the subscriber we deal with */
2070 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2071 }
2072
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002073 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002074
Philipp Maier9ca7b312018-10-10 17:00:49 +02002075 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002076 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002077
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002078 if (trans->msc_a)
2079 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002080
2081 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002082 if (!msc_a) {
2083 struct gsm_mncc rel = {
2084 .callref = data->callref,
2085 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002086 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002087 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2088 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002089 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002090 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2091 else
2092 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2093 trans->callref = 0;
2094 trans_free(trans);
2095 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002096 }
2097
2098 /* Find function for current state and message */
2099 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002100 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002101 && ((1 << trans->cc.state) & downstatelist[i].states))
2102 break;
2103 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002104 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002105 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002106 return 0;
2107 }
2108
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002109 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002110
2111 return rc;
2112}
2113
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002114struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2115{
2116 uint32_t callref;
2117
2118 switch (msg->msg_type) {
2119 case MNCC_BRIDGE:
2120 callref = msg->bridge.callref[0];
2121 break;
2122 case MNCC_RTP_CREATE:
2123 case MNCC_RTP_CONNECT:
2124 callref = msg->rtp.callref;
2125 break;
2126
2127 case MNCC_RTP_FREE:
2128 case MNCC_FRAME_DROP:
2129 case MNCC_FRAME_RECV:
2130 case GSM_TCHF_FRAME:
2131 case GSM_TCHF_FRAME_EFR:
2132 case GSM_TCHH_FRAME:
2133 case GSM_TCH_FRAME_AMR:
2134 return NULL;
2135
2136 default:
2137 callref = msg->signal.callref;
2138 break;
2139 }
2140
2141 return mncc_call_find_by_callref(callref);
2142}
2143
2144/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002145int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002146{
2147 const union mncc_msg *msg = arg;
2148 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002149
2150 if (msg->msg_type == MNCC_SETUP_REQ) {
2151 /* Incoming call to forward for inter-MSC Handover? */
2152 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2153 if (mncc_call)
2154 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2155 "Incoming call matches pending inter-MSC Handover Number\n");
2156 }
2157 if (!mncc_call) {
2158 /* Find already active MNCC FSM for this callref.
2159 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2160 * MNCC<->GSM-CC call handling. */
2161 mncc_call = mncc_find_by_callref_from_msg(msg);
2162 }
2163 if (mncc_call) {
2164 mncc_call_rx(mncc_call, msg);
2165 return 0;
2166 }
2167
2168 /* None of the above? Then it must be a normal GSM CC call related message. */
2169 return mncc_tx_to_gsm_cc(net, msg);
2170}
Harald Welte27989d42018-06-21 20:39:20 +02002171
2172static struct datastate {
2173 uint32_t states;
2174 int type;
2175 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2176} datastatelist[] = {
2177 /* mobile originating call establishment */
2178 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2179 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2180 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2181 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2182 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2183 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2184 /* mobile terminating call establishment */
2185 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2186 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2187 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2188 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2189 {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 */
2190 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2191 /* signalling during call */
2192 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2193 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2194 {SBIT(GSM_CSTATE_ACTIVE),
2195 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2196 {ALL_STATES,
2197 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2198 {ALL_STATES,
2199 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2200 {ALL_STATES,
2201 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2202 {SBIT(GSM_CSTATE_ACTIVE),
2203 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2204 {SBIT(GSM_CSTATE_ACTIVE),
2205 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2206 {SBIT(GSM_CSTATE_ACTIVE),
2207 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2208 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2209 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2210 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2211 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2212 {SBIT(GSM_CSTATE_ACTIVE),
2213 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2214 /* clearing */
2215 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2216 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2217 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2218 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2219 {ALL_STATES, /* 5.4.3.4 */
2220 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2221};
2222
2223#define DATASLLEN \
2224 (sizeof(datastatelist) / sizeof(struct datastate))
2225
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002226int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002227{
2228 struct gsm48_hdr *gh = msgb_l3(msg);
2229 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2230 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2231 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002232 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2233 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002234 int i, rc = 0;
2235
2236 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002237 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002238 return -EINVAL;
2239 }
2240
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002241 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002242 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002243 return -EINVAL;
2244 }
2245
2246 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002247 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002248
Harald Welte27989d42018-06-21 20:39:20 +02002249 /* Create transaction */
2250 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002251 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002252 trans = trans_alloc(net, vsub,
2253 TRANS_CC,
2254 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002255 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002256 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002257 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002258 GSM48_PDISC_CC | (transaction_id << 4),
2259 GSM48_MT_CC_RELEASE_COMPL);
2260 return -ENOMEM;
2261 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002262 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2263 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2264 trans_free(trans);
2265 return -EINVAL;
2266 }
2267
Harald Welte27989d42018-06-21 20:39:20 +02002268 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002269 msc_a_get(msc_a, MSC_A_USE_CC);
2270 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002271 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002272
2273 /* An earlier CM Service Request for this CC message now has concluded */
2274 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2275 LOG_MSC_A(msc_a, LOGL_ERROR,
2276 "Creating new CC transaction without prior CM Service Request\n");
2277 else
2278 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002279 }
2280
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002281 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2282 gsm48_cc_state_name(trans->cc.state));
2283
Harald Welte27989d42018-06-21 20:39:20 +02002284 /* find function for current state and message */
2285 for (i = 0; i < DATASLLEN; i++)
2286 if ((msg_type == datastatelist[i].type)
2287 && ((1 << trans->cc.state) & datastatelist[i].states))
2288 break;
2289 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002290 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002291
2292 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2293 * transaction right away. */
2294 if (trans->cc.state == GSM_CSTATE_NULL) {
2295 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2296 " -- disarding new CC transaction right away\n");
2297 trans_free(trans);
2298 }
Harald Welte27989d42018-06-21 20:39:20 +02002299 return 0;
2300 }
2301
2302 assert(trans->vsub);
2303
2304 rc = datastatelist[i].rout(trans, msg);
2305
Harald Welte27989d42018-06-21 20:39:20 +02002306 return rc;
2307}