blob: dd2c77654442ac4c12ccdcff1bf3d9427239da67 [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;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100574 struct sdp_msg *sdp;
575 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200576
Philipp Maier9ca7b312018-10-10 17:00:49 +0200577 gsm48_start_guard_timer(trans);
578
Harald Welte27989d42018-06-21 20:39:20 +0200579 memset(&setup, 0, sizeof(struct gsm_mncc));
580 setup.callref = trans->callref;
581
Keith Whytea1a70be2021-05-16 02:59:52 +0200582 /* New Global Call Reference */
583 if (!trans->cc.lcls)
584 trans->cc.lcls = trans_lcls_compose(trans, true);
585
586 /* Pass the LCLS GCR on to the MT call leg via MNCC */
Vadim Yanitskiyc6921e52021-10-27 17:05:55 +0300587 if (trans->cc.lcls) {
588 struct msgb *gcr_msg = msgb_alloc(sizeof(setup.gcr), "MNCC GCR");
589 const struct osmo_gcr_parsed *gcr = &trans->cc.lcls->gcr;
590 int rc;
591
592 if (gcr_msg != NULL && (rc = osmo_enc_gcr(gcr_msg, gcr)) > 0) {
593 memcpy(&setup.gcr[0], gcr_msg->data, rc);
594 setup.fields |= MNCC_F_GCR;
595 } else
596 LOG_TRANS(trans, LOGL_ERROR, "Failed to encode GCR\n");
597 msgb_free(gcr_msg);
598 }
Keith Whytea1a70be2021-05-16 02:59:52 +0200599
Harald Welte27989d42018-06-21 20:39:20 +0200600 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
601 /* emergency setup is identified by msg_type */
602 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
603 setup.fields |= MNCC_F_EMERGENCY;
604 setup.emergency = 1;
605 /* use destination number as configured by user (if any) */
606 if (trans->net->emergency.route_to_msisdn) {
607 setup.fields |= MNCC_F_CALLED;
608 setup.called.type = 0; /* unknown */
609 setup.called.plan = 0; /* unknown */
610 OSMO_STRLCPY_ARRAY(setup.called.number,
611 trans->net->emergency.route_to_msisdn);
612 }
613 }
614
615 /* use subscriber as calling party number */
616 setup.fields |= MNCC_F_CALLING;
617 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
618 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
619
620 /* bearer capability */
621 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
622 setup.fields |= MNCC_F_BEARER_CAP;
623 gsm48_decode_bearer_cap(&setup.bearer_cap,
624 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
625
626 /* Create a copy of the bearer capability
627 * in the transaction struct, so we can use
628 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100629 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200630 sizeof(trans->bearer_cap));
631 }
632 /* facility */
633 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
634 setup.fields |= MNCC_F_FACILITY;
635 gsm48_decode_facility(&setup.facility,
636 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
637 }
638 /* called party bcd number */
639 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
640 setup.fields |= MNCC_F_CALLED;
641 gsm48_decode_called(&setup.called,
642 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
643 }
644 /* user-user */
645 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
646 setup.fields |= MNCC_F_USERUSER;
647 gsm48_decode_useruser(&setup.useruser,
648 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
649 }
650 /* ss-version */
651 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
652 setup.fields |= MNCC_F_SSVERSION;
653 gsm48_decode_ssversion(&setup.ssversion,
654 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
655 }
656 /* CLIR suppression */
657 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
658 setup.clir.sup = 1;
659 /* CLIR invocation */
660 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
661 setup.clir.inv = 1;
662 /* cc cap */
663 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
664 setup.fields |= MNCC_F_CCCAP;
665 gsm48_decode_cccap(&setup.cccap,
666 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
667 }
668
669 new_cc_state(trans, GSM_CSTATE_INITIATED);
670
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100671 /* MO call leg starting, gather all codec information so far known: */
672 codec_filter_init(&trans->cc.codecs);
673 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
674 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
675 if (setup.fields & MNCC_F_BEARER_CAP)
676 codec_filter_set_ms_from_bc(&trans->cc.codecs, &trans->bearer_cap);
677 codec_filter_run(&trans->cc.codecs);
678
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100679 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
680 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Neels Hofmeyrf5559522022-01-13 21:39:11 +0100681 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
Harald Welte27989d42018-06-21 20:39:20 +0200682
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200683 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MO_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200684
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100685 /* FUTURE: so far this is composing SDP although the RTP address is not established yet (sending 0.0.0.0:0).
686 * Subsequent patch 'do CN CRCX first' (Ie433db1ba0c46d4b97538a969233c155cefac21c) changes the ordering so that
687 * the CN CRCX is completed before dispatching CC SETUP to remote, so that a valid RTP address is set. */
688
689 sdp = trans->cc.codecs.result.audio_codecs.count ? &trans->cc.codecs.result : NULL;
690 rc = sdp_msg_to_sdp_str_buf(setup.sdp, sizeof(setup.sdp), sdp);
691 if (rc >= sizeof(setup.sdp)) {
692 LOG_TRANS(trans, LOGL_ERROR, "MNCC_SETUP_IND: SDP too long (%d > %zu bytes)\n", rc, sizeof(setup.sdp));
693 trans_free(trans);
694 return -EINVAL;
695 }
696
Harald Welte27989d42018-06-21 20:39:20 +0200697 /* indicate setup to MNCC */
698 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
699
700 /* MNCC code will modify the channel asynchronously, we should
701 * ipaccess-bind only after the modification has been made to the
702 * lchan->tch_mode */
703 return 0;
704}
705
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100706static void rx_mncc_sdp(struct gsm_trans *trans, uint32_t mncc_msg_type, const char *sdp)
707{
708 int rc;
709 if (!sdp[0])
710 return;
711 rc = sdp_msg_from_sdp_str(&trans->cc.codecs.remote, sdp);
712 if (rc)
713 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "rx %s: Failed to parse SDP: %d\n",
714 get_mncc_name(mncc_msg_type), rc);
715}
716
Harald Welte27989d42018-06-21 20:39:20 +0200717static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
718{
Neels Hofmeyr3551d842022-01-13 19:35:12 +0100719 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC SETUP");
Harald Welte27989d42018-06-21 20:39:20 +0200720 struct gsm48_hdr *gh;
721 struct gsm_mncc *setup = arg;
722 int rc, trans_id;
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100723 struct gsm_mncc_bearer_cap bearer_cap;
Harald Welte27989d42018-06-21 20:39:20 +0200724
725 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
726
727 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700728 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100729 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200730 "This is not allowed!\n");
731 /* Temporarily out of order */
732 rc = mncc_release_ind(trans->net, trans, trans->callref,
733 GSM48_CAUSE_LOC_PRN_S_LU,
734 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
735 trans->callref = 0;
736 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200737 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200738 return rc;
739 }
740
741 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100742 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200743 if (trans_id < 0) {
744 /* no free transaction ID */
745 rc = mncc_release_ind(trans->net, trans, trans->callref,
746 GSM48_CAUSE_LOC_PRN_S_LU,
747 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
748 trans->callref = 0;
749 trans_free(trans);
Neels Hofmeyr61ae18c2019-08-28 03:41:05 +0200750 msgb_free(msg);
Harald Welte27989d42018-06-21 20:39:20 +0200751 return rc;
752 }
753 trans->transaction_id = trans_id;
754
755 gh->msg_type = GSM48_MT_CC_SETUP;
756
757 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
758
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100759 /* MT call leg is starting. Gather all codecs information so far known.
760 * (Usually) paging has succeeded, and now we're processing the MNCC Setup from the remote MO call leg.
761 * Initialize the codecs filter with this side's BSS' codec list, received at Complete Layer 3.
762 * We must not pass bearer_cap to codec_filter_init(), because we haven't received the MT MS's Bearer
763 * Capabilities yet; the Bearer Capabilities handled here are actually the remote call leg's Bearer
764 * Capabilities. */
765 codec_filter_init(&trans->cc.codecs);
766 codec_filter_set_ran(&trans->cc.codecs, trans->msc_a->c.ran->type);
767 codec_filter_set_bss(&trans->cc.codecs, &trans->msc_a->cc.compl_l3_codec_list_bss_supported);
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100768 /* sdp.remote: if SDP is included in the MNCC, take that as definitive list of remote audio codecs. */
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100769 rx_mncc_sdp(trans, setup->msg_type, setup->sdp);
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100770 /* sdp.remote: if there is no SDP information or we failed to parse it, try using the Bearer Capability from
771 * MNCC, if any. */
772 if (!trans->cc.codecs.remote.audio_codecs.count && (setup->fields & MNCC_F_BEARER_CAP)) {
773 trans->cc.codecs.remote = (struct sdp_msg){};
774 sdp_audio_codecs_from_bearer_cap(&trans->cc.codecs.remote.audio_codecs,
775 &setup->bearer_cap);
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100776 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s Bearer Cap: remote=%s\n",
777 get_mncc_name(setup->msg_type), sdp_msg_to_str(&trans->cc.codecs.remote));
Neels Hofmeyraf9d30e2022-01-13 21:40:58 +0100778 }
779 if (!trans->cc.codecs.remote.audio_codecs.count)
780 LOG_TRANS(trans, LOGL_INFO,
781 "Got no information of remote audio codecs: neither SDP nor Bearer Capability. Trying anyway.\n");
782
Neels Hofmeyr7ddc48c2022-01-13 21:40:58 +0100783 codec_filter_run(&trans->cc.codecs);
784 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
785
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100786 /* Compose Bearer Capability information that reflects only the codecs (Speech Versions) remaining after
787 * intersecting MS, BSS and remote call leg restrictions. To store in trans for later use, and to include in
788 * the outgoing CC Setup message. */
789 bearer_cap = (struct gsm_mncc_bearer_cap){
790 .speech_ver = { -1 },
791 };
792 sdp_audio_codecs_to_bearer_cap(&bearer_cap, &trans->cc.codecs.result.audio_codecs);
793 rc = bearer_cap_set_radio(&bearer_cap);
794 if (rc) {
795 LOG_TRANS(trans, LOGL_ERROR, "Error composing Bearer Capability for CC Setup\n");
796 trans_free(trans);
797 msgb_free(msg);
798 return rc;
Harald Welte27989d42018-06-21 20:39:20 +0200799 }
Neels Hofmeyr909ea522022-01-13 21:40:58 +0100800 /* Create a copy of the bearer capability in the transaction struct, so we can use this information later */
801 /* TODO: we should be able to drop trans->bearer_cap, replaced by the codecs filter. Verify this.
802 * So far let's just store it there like previous code did. */
803 trans->bearer_cap = bearer_cap;
804 /* If no resulting codecs remain, error out. We cannot find a codec that matches both call legs. If the MGW were
805 * able to transcode, we could use non-identical codecs on each conn of the MGW endpoint, but we are aiming for
806 * finding a matching codec. */
807 if (bearer_cap.speech_ver[0] == -1) {
808 LOG_TRANS(trans, LOGL_ERROR, "%s: no codec match possible: %s\n",
809 get_mncc_name(setup->msg_type), codec_filter_to_str(&trans->cc.codecs));
810
811 /* incompatible codecs */
812 rc = mncc_release_ind(trans->net, trans, trans->callref,
813 GSM48_CAUSE_LOC_PRN_S_LU,
814 GSM48_CC_CAUSE_INCOMPAT_DEST /* TODO: correct cause code? */);
815 trans->callref = 0;
816 trans_free(trans);
817 msgb_free(msg);
818 return rc;
819 }
820 gsm48_encode_bearer_cap(msg, 0, &bearer_cap);
821
Harald Welte27989d42018-06-21 20:39:20 +0200822 /* facility */
823 if (setup->fields & MNCC_F_FACILITY)
824 gsm48_encode_facility(msg, 0, &setup->facility);
825 /* progress */
826 if (setup->fields & MNCC_F_PROGRESS)
827 gsm48_encode_progress(msg, 0, &setup->progress);
828 /* calling party BCD number */
829 if (setup->fields & MNCC_F_CALLING)
830 gsm48_encode_calling(msg, &setup->calling);
831 /* called party BCD number */
832 if (setup->fields & MNCC_F_CALLED)
833 gsm48_encode_called(msg, &setup->called);
834 /* user-user */
835 if (setup->fields & MNCC_F_USERUSER)
836 gsm48_encode_useruser(msg, 0, &setup->useruser);
837 /* redirecting party BCD number */
838 if (setup->fields & MNCC_F_REDIRECTING)
839 gsm48_encode_redirecting(msg, &setup->redirecting);
840 /* signal */
841 if (setup->fields & MNCC_F_SIGNAL)
842 gsm48_encode_signal(msg, setup->signal);
843
844 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
845
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200846 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_SETUP));
Harald Welte27989d42018-06-21 20:39:20 +0200847
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100848 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200849}
850
851static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
852{
853 struct gsm48_hdr *gh = msgb_l3(msg);
854 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
855 struct tlv_parsed tp;
856 struct gsm_mncc call_conf;
857 int rc;
858
859 gsm48_stop_cc_timer(trans);
860 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
861
862 memset(&call_conf, 0, sizeof(struct gsm_mncc));
863 call_conf.callref = trans->callref;
864
865 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
866#if 0
867 /* repeat */
868 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
869 call_conf.repeat = 1;
870 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
871 call_conf.repeat = 2;
872#endif
873 /* bearer capability */
874 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
875 call_conf.fields |= MNCC_F_BEARER_CAP;
876 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
877 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
878
879 /* Create a copy of the bearer capability
880 * in the transaction struct, so we can use
881 * this information later */
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100882 memcpy(&trans->bearer_cap, &call_conf.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200883 sizeof(trans->bearer_cap));
Neels Hofmeyr10357f82022-01-13 19:59:02 +0100884
885 /* This is the MT call leg's Call Conf, containing the MS Bearer Capabilities of the MT MS.
886 * Store in codecs filter. */
887 codec_filter_set_ms_from_bc(&trans->cc.codecs, &call_conf.bearer_cap);
Harald Welte27989d42018-06-21 20:39:20 +0200888 }
Neels Hofmeyra9e383f2022-01-13 19:58:05 +0100889
Harald Welte27989d42018-06-21 20:39:20 +0200890 /* cause */
891 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
892 call_conf.fields |= MNCC_F_CAUSE;
893 gsm48_decode_cause(&call_conf.cause,
894 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
895 }
896 /* cc cap */
897 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
898 call_conf.fields |= MNCC_F_CCCAP;
899 gsm48_decode_cccap(&call_conf.cccap,
900 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
901 }
902
903 /* IMSI of called subscriber */
904 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
905
906 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
907
908 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100909 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200910
911 /* don't continue, if there were problems with
912 * the call assignment. */
913 if (rc)
914 return rc;
915
916 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
917 &call_conf);
918}
919
920static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
921{
922 struct gsm_mncc *proceeding = arg;
923 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
924 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
925 int rc;
926
927 gh->msg_type = GSM48_MT_CC_CALL_PROC;
928
929 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
930
931 /* bearer capability */
932 if (proceeding->fields & MNCC_F_BEARER_CAP) {
933 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
934 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
935 }
936 /* facility */
937 if (proceeding->fields & MNCC_F_FACILITY)
938 gsm48_encode_facility(msg, 0, &proceeding->facility);
939 /* progress */
940 if (proceeding->fields & MNCC_F_PROGRESS)
941 gsm48_encode_progress(msg, 0, &proceeding->progress);
942
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100943 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200944 if (rc)
945 return rc;
946
947 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100948 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200949}
950
951static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
952{
953 struct gsm48_hdr *gh = msgb_l3(msg);
954 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
955 struct tlv_parsed tp;
956 struct gsm_mncc alerting;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100957 int rc;
Harald Welte27989d42018-06-21 20:39:20 +0200958
959 gsm48_stop_cc_timer(trans);
960 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
961
962 memset(&alerting, 0, sizeof(struct gsm_mncc));
963 alerting.callref = trans->callref;
964 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
965 /* facility */
966 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
967 alerting.fields |= MNCC_F_FACILITY;
968 gsm48_decode_facility(&alerting.facility,
969 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
970 }
971
972 /* progress */
973 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
974 alerting.fields |= MNCC_F_PROGRESS;
975 gsm48_decode_progress(&alerting.progress,
976 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
977 }
978 /* ss-version */
979 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
980 alerting.fields |= MNCC_F_SSVERSION;
981 gsm48_decode_ssversion(&alerting.ssversion,
982 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
983 }
984
985 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
986
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100987 codec_filter_run(&trans->cc.codecs);
988 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
989 rc = sdp_msg_to_sdp_str_buf(alerting.sdp, sizeof(alerting.sdp), &trans->cc.codecs.result);
990 if (rc >= sizeof(alerting.sdp)) {
991 LOG_TRANS(trans, LOGL_ERROR, "MNCC_ALERT_IND: SDP too long (%d > %zu bytes)\n",
992 rc, sizeof(alerting.sdp));
993 trans_free(trans);
994 return -EINVAL;
995 }
996
Harald Welte27989d42018-06-21 20:39:20 +0200997 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
998 &alerting);
999}
1000
1001static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1002{
1003 struct gsm_mncc *alerting = arg;
1004 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
1005 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1006
1007 gh->msg_type = GSM48_MT_CC_ALERTING;
1008
1009 /* facility */
1010 if (alerting->fields & MNCC_F_FACILITY)
1011 gsm48_encode_facility(msg, 0, &alerting->facility);
1012 /* progress */
1013 if (alerting->fields & MNCC_F_PROGRESS)
1014 gsm48_encode_progress(msg, 0, &alerting->progress);
1015 /* user-user */
1016 if (alerting->fields & MNCC_F_USERUSER)
1017 gsm48_encode_useruser(msg, 0, &alerting->useruser);
1018
1019 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1020
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001021 if (alerting->sdp[0]) {
1022 struct call_leg *cl = trans->msc_a->cc.call_leg;
1023 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1024 codec_filter_set_remote(&trans->cc.codecs, alerting->sdp);
1025 codec_filter_run(&trans->cc.codecs);
1026 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1027 get_mncc_name(alerting->msg_type), codec_filter_to_str(&trans->cc.codecs));
1028 if (rtp_cn) {
1029 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1030 rtp_stream_commit(rtp_cn);
1031 }
1032 }
1033
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001034 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001035}
1036
1037static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1038{
1039 struct gsm_mncc *progress = arg;
1040 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
1041 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1042
1043 gh->msg_type = GSM48_MT_CC_PROGRESS;
1044
1045 /* progress */
1046 gsm48_encode_progress(msg, 1, &progress->progress);
1047 /* user-user */
1048 if (progress->fields & MNCC_F_USERUSER)
1049 gsm48_encode_useruser(msg, 0, &progress->useruser);
1050
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001051 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001052}
1053
1054static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1055{
1056 struct gsm_mncc *connect = arg;
1057 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
1058 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1059
1060 gh->msg_type = GSM48_MT_CC_CONNECT;
1061
1062 gsm48_stop_cc_timer(trans);
1063 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1064
1065 /* facility */
1066 if (connect->fields & MNCC_F_FACILITY)
1067 gsm48_encode_facility(msg, 0, &connect->facility);
1068 /* progress */
1069 if (connect->fields & MNCC_F_PROGRESS)
1070 gsm48_encode_progress(msg, 0, &connect->progress);
1071 /* connected number */
1072 if (connect->fields & MNCC_F_CONNECTED)
1073 gsm48_encode_connected(msg, &connect->connected);
1074 /* user-user */
1075 if (connect->fields & MNCC_F_USERUSER)
1076 gsm48_encode_useruser(msg, 0, &connect->useruser);
1077
1078 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1079
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001080 /* Received an MNCC_SETUP_RSP with the remote leg's SDP information. Apply codec choice. */
1081 if (connect->sdp[0]) {
1082 struct call_leg *cl = trans->msc_a->cc.call_leg;
1083 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
1084 rx_mncc_sdp(trans, connect->msg_type, connect->sdp);
1085 codec_filter_run(&trans->cc.codecs);
1086 LOG_TRANS(trans, LOGL_DEBUG, "%s codecs: %s\n",
1087 get_mncc_name(connect->msg_type),
1088 codec_filter_to_str(&trans->cc.codecs));
1089 if (rtp_cn) {
1090 rtp_stream_set_remote_addr_and_codecs(rtp_cn, &trans->cc.codecs.remote);
1091 rtp_stream_commit(rtp_cn);
1092 }
1093 }
1094
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001095 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001096}
1097
1098static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1099{
1100 struct gsm48_hdr *gh = msgb_l3(msg);
1101 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1102 struct tlv_parsed tp;
1103 struct gsm_mncc connect;
1104
1105 gsm48_stop_cc_timer(trans);
1106
1107 memset(&connect, 0, sizeof(struct gsm_mncc));
1108 connect.callref = trans->callref;
1109 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1110 /* use subscriber as connected party number */
1111 connect.fields |= MNCC_F_CONNECTED;
1112 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
1113 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
1114
1115 /* facility */
1116 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1117 connect.fields |= MNCC_F_FACILITY;
1118 gsm48_decode_facility(&connect.facility,
1119 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1120 }
1121 /* user-user */
1122 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1123 connect.fields |= MNCC_F_USERUSER;
1124 gsm48_decode_useruser(&connect.useruser,
1125 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1126 }
1127 /* ss-version */
1128 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1129 connect.fields |= MNCC_F_SSVERSION;
1130 gsm48_decode_ssversion(&connect.ssversion,
1131 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1132 }
1133
1134 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001135 rate_ctr_inc(rate_ctr_group_get_ctr(trans->net->msc_ctrs, MSC_CTR_CALL_MT_CONNECT));
Harald Welte27989d42018-06-21 20:39:20 +02001136
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001137 codec_filter_run(&trans->cc.codecs);
1138 sdp_msg_to_sdp_str_buf(connect.sdp, sizeof(connect.sdp), &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001139 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
1140}
1141
1142
1143static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1144{
1145 struct gsm_mncc connect_ack;
1146
1147 gsm48_stop_cc_timer(trans);
1148
1149 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +02001150 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 +02001151
1152 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1153 connect_ack.callref = trans->callref;
1154
1155 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
1156 &connect_ack);
1157}
1158
1159static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1160{
1161 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
1162 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1163
1164 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1165
1166 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1167
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001168 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001169}
1170
1171static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1172{
1173 struct gsm48_hdr *gh = msgb_l3(msg);
1174 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1175 struct tlv_parsed tp;
1176 struct gsm_mncc disc;
1177
1178 gsm48_stop_cc_timer(trans);
1179
1180 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1181
1182 memset(&disc, 0, sizeof(struct gsm_mncc));
1183 disc.callref = trans->callref;
1184 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
1185 /* cause */
1186 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1187 disc.fields |= MNCC_F_CAUSE;
1188 gsm48_decode_cause(&disc.cause,
1189 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1190 }
1191 /* facility */
1192 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1193 disc.fields |= MNCC_F_FACILITY;
1194 gsm48_decode_facility(&disc.facility,
1195 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1196 }
1197 /* user-user */
1198 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1199 disc.fields |= MNCC_F_USERUSER;
1200 gsm48_decode_useruser(&disc.useruser,
1201 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1202 }
1203 /* ss-version */
1204 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1205 disc.fields |= MNCC_F_SSVERSION;
1206 gsm48_decode_ssversion(&disc.ssversion,
1207 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1208 }
1209
1210 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +02001211}
1212
1213static struct gsm_mncc_cause default_cause = {
1214 .location = GSM48_CAUSE_LOC_PRN_S_LU,
1215 .coding = 0,
1216 .rec = 0,
1217 .rec_val = 0,
1218 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
1219 .diag_len = 0,
1220 .diag = { 0 },
1221};
1222
1223static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
1224{
1225 struct gsm_mncc *disc = arg;
1226 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
1227 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1228
1229 gh->msg_type = GSM48_MT_CC_DISCONNECT;
1230
1231 gsm48_stop_cc_timer(trans);
1232 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
1233
1234 /* cause */
1235 if (disc->fields & MNCC_F_CAUSE)
1236 gsm48_encode_cause(msg, 1, &disc->cause);
1237 else
1238 gsm48_encode_cause(msg, 1, &default_cause);
1239
1240 /* facility */
1241 if (disc->fields & MNCC_F_FACILITY)
1242 gsm48_encode_facility(msg, 0, &disc->facility);
1243 /* progress */
1244 if (disc->fields & MNCC_F_PROGRESS)
1245 gsm48_encode_progress(msg, 0, &disc->progress);
1246 /* user-user */
1247 if (disc->fields & MNCC_F_USERUSER)
1248 gsm48_encode_useruser(msg, 0, &disc->useruser);
1249
1250 /* store disconnect cause for T306 expiry */
1251 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1252
1253 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1254
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001255 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001256}
1257
1258static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1259{
1260 struct gsm48_hdr *gh = msgb_l3(msg);
1261 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1262 struct tlv_parsed tp;
1263 struct gsm_mncc rel;
1264 int rc;
1265
1266 gsm48_stop_cc_timer(trans);
1267
1268 memset(&rel, 0, sizeof(struct gsm_mncc));
1269 rel.callref = trans->callref;
1270 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1271 /* cause */
1272 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1273 rel.fields |= MNCC_F_CAUSE;
1274 gsm48_decode_cause(&rel.cause,
1275 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1276 }
1277 /* facility */
1278 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1279 rel.fields |= MNCC_F_FACILITY;
1280 gsm48_decode_facility(&rel.facility,
1281 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1282 }
1283 /* user-user */
1284 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1285 rel.fields |= MNCC_F_USERUSER;
1286 gsm48_decode_useruser(&rel.useruser,
1287 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1288 }
1289 /* ss-version */
1290 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1291 rel.fields |= MNCC_F_SSVERSION;
1292 gsm48_decode_ssversion(&rel.ssversion,
1293 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1294 }
1295
1296 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1297 /* release collision 5.4.5 */
1298 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1299 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001300 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001301 GSM48_PDISC_CC | (trans->transaction_id << 4),
1302 GSM48_MT_CC_RELEASE_COMPL);
1303 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1304 }
1305
1306 new_cc_state(trans, GSM_CSTATE_NULL);
1307
1308 trans->callref = 0;
1309 trans_free(trans);
1310
1311 return rc;
1312}
1313
1314static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1315{
1316 struct gsm_mncc *rel = arg;
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001317 struct msgb *msg;
1318 struct gsm48_hdr *gh;
1319
1320 if (!trans->msc_a) {
1321 LOG_TRANS(trans, LOGL_DEBUG, "Cannot send CC REL, there is no MSC-A connection\n");
1322 return -EINVAL;
1323 }
1324
1325 msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1326 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte27989d42018-06-21 20:39:20 +02001327
1328 gh->msg_type = GSM48_MT_CC_RELEASE;
1329
1330 gsm48_stop_cc_timer(trans);
1331 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1332
1333 /* cause */
1334 if (rel->fields & MNCC_F_CAUSE)
1335 gsm48_encode_cause(msg, 0, &rel->cause);
1336 /* facility */
1337 if (rel->fields & MNCC_F_FACILITY)
1338 gsm48_encode_facility(msg, 0, &rel->facility);
1339 /* user-user */
1340 if (rel->fields & MNCC_F_USERUSER)
1341 gsm48_encode_useruser(msg, 0, &rel->useruser);
1342
1343 trans->cc.T308_second = 0;
1344 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1345
1346 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1347 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1348
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001349 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001350}
1351
1352static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1353{
1354 struct gsm48_hdr *gh = msgb_l3(msg);
1355 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1356 struct tlv_parsed tp;
1357 struct gsm_mncc rel;
1358 int rc = 0;
1359
1360 gsm48_stop_cc_timer(trans);
1361
1362 memset(&rel, 0, sizeof(struct gsm_mncc));
1363 rel.callref = trans->callref;
1364 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1365 /* cause */
1366 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1367 rel.fields |= MNCC_F_CAUSE;
1368 gsm48_decode_cause(&rel.cause,
1369 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1370 }
1371 /* facility */
1372 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1373 rel.fields |= MNCC_F_FACILITY;
1374 gsm48_decode_facility(&rel.facility,
1375 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1376 }
1377 /* user-user */
1378 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1379 rel.fields |= MNCC_F_USERUSER;
1380 gsm48_decode_useruser(&rel.useruser,
1381 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1382 }
1383 /* ss-version */
1384 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1385 rel.fields |= MNCC_F_SSVERSION;
1386 gsm48_decode_ssversion(&rel.ssversion,
1387 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1388 }
1389
1390 if (trans->callref) {
1391 switch (trans->cc.state) {
1392 case GSM_CSTATE_CALL_PRESENT:
1393 rc = mncc_recvmsg(trans->net, trans,
1394 MNCC_REJ_IND, &rel);
1395 break;
1396 case GSM_CSTATE_RELEASE_REQ:
1397 rc = mncc_recvmsg(trans->net, trans,
1398 MNCC_REL_CNF, &rel);
1399 break;
1400 default:
1401 rc = mncc_recvmsg(trans->net, trans,
1402 MNCC_REL_IND, &rel);
1403 }
1404 }
1405
1406 trans->callref = 0;
1407 trans_free(trans);
1408
1409 return rc;
1410}
1411
1412static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1413{
1414 struct gsm_mncc *rel = arg;
1415 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1416 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1417 int ret;
1418
1419 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1420
1421 trans->callref = 0;
1422
1423 gsm48_stop_cc_timer(trans);
1424
1425 /* cause */
1426 if (rel->fields & MNCC_F_CAUSE)
1427 gsm48_encode_cause(msg, 0, &rel->cause);
1428 /* facility */
1429 if (rel->fields & MNCC_F_FACILITY)
1430 gsm48_encode_facility(msg, 0, &rel->facility);
1431 /* user-user */
1432 if (rel->fields & MNCC_F_USERUSER)
1433 gsm48_encode_useruser(msg, 0, &rel->useruser);
1434
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001435 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001436
1437 trans_free(trans);
1438
1439 return ret;
1440}
1441
1442static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1443{
1444 struct gsm48_hdr *gh = msgb_l3(msg);
1445 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1446 struct tlv_parsed tp;
1447 struct gsm_mncc fac;
1448
1449 memset(&fac, 0, sizeof(struct gsm_mncc));
1450 fac.callref = trans->callref;
1451 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1452 /* facility */
1453 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1454 fac.fields |= MNCC_F_FACILITY;
1455 gsm48_decode_facility(&fac.facility,
1456 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1457 }
1458 /* ss-version */
1459 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1460 fac.fields |= MNCC_F_SSVERSION;
1461 gsm48_decode_ssversion(&fac.ssversion,
1462 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1463 }
1464
1465 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1466}
1467
1468static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1469{
1470 struct gsm_mncc *fac = arg;
1471 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1472 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1473
1474 gh->msg_type = GSM48_MT_CC_FACILITY;
1475
1476 /* facility */
1477 gsm48_encode_facility(msg, 1, &fac->facility);
1478
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001479 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001480}
1481
1482static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1483{
1484 struct gsm_mncc hold;
1485
1486 memset(&hold, 0, sizeof(struct gsm_mncc));
1487 hold.callref = trans->callref;
1488 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1489}
1490
1491static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1492{
1493 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1494 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1495
1496 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1497
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001498 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001499}
1500
1501static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1502{
1503 struct gsm_mncc *hold_rej = arg;
1504 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1505 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1506
1507 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1508
1509 /* cause */
1510 if (hold_rej->fields & MNCC_F_CAUSE)
1511 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1512 else
1513 gsm48_encode_cause(msg, 1, &default_cause);
1514
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001515 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001516}
1517
1518static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1519{
1520 struct gsm_mncc retrieve;
1521
1522 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1523 retrieve.callref = trans->callref;
1524 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1525 &retrieve);
1526}
1527
1528static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1529{
1530 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1531 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1532
1533 gh->msg_type = GSM48_MT_CC_RETR_ACK;
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_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1539{
1540 struct gsm_mncc *retrieve_rej = arg;
1541 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1542 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1543
1544 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1545
1546 /* cause */
1547 if (retrieve_rej->fields & MNCC_F_CAUSE)
1548 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1549 else
1550 gsm48_encode_cause(msg, 1, &default_cause);
1551
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001552 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001553}
1554
1555static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1556{
1557 struct gsm48_hdr *gh = msgb_l3(msg);
1558 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1559 struct tlv_parsed tp;
1560 struct gsm_mncc dtmf;
1561
1562 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1563 dtmf.callref = trans->callref;
1564 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1565 /* keypad facility */
1566 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1567 dtmf.fields |= MNCC_F_KEYPAD;
1568 gsm48_decode_keypad(&dtmf.keypad,
1569 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1570 }
1571
1572 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1573}
1574
1575static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1576{
1577 struct gsm_mncc *dtmf = arg;
1578 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1579 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1580
1581 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1582
1583 /* keypad */
1584 if (dtmf->fields & MNCC_F_KEYPAD)
1585 gsm48_encode_keypad(msg, dtmf->keypad);
1586
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001587 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001588}
1589
1590static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1591{
1592 struct gsm_mncc *dtmf = arg;
1593 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1594 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1595
1596 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1597
1598 /* cause */
1599 if (dtmf->fields & MNCC_F_CAUSE)
1600 gsm48_encode_cause(msg, 1, &dtmf->cause);
1601 else
1602 gsm48_encode_cause(msg, 1, &default_cause);
1603
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001604 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001605}
1606
1607static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1608{
1609 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1610 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1611
1612 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1613
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001614 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001615}
1616
1617static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1618{
1619 struct gsm_mncc dtmf;
1620
1621 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1622 dtmf.callref = trans->callref;
1623
1624 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1625}
1626
1627static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1628{
1629 struct gsm48_hdr *gh = msgb_l3(msg);
1630 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1631 struct tlv_parsed tp;
1632 struct gsm_mncc modify;
1633
1634 memset(&modify, 0, sizeof(struct gsm_mncc));
1635 modify.callref = trans->callref;
1636 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1637 /* bearer capability */
1638 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1639 modify.fields |= MNCC_F_BEARER_CAP;
1640 gsm48_decode_bearer_cap(&modify.bearer_cap,
1641 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1642
1643 /* Create a copy of the bearer capability
1644 * in the transaction struct, so we can use
1645 * this information later */
1646 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1647 sizeof(trans->bearer_cap));
1648 }
1649
1650 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1651
1652 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1653}
1654
1655static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1656{
1657 struct gsm_mncc *modify = arg;
1658 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1659 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1660
1661 gh->msg_type = GSM48_MT_CC_MODIFY;
1662
1663 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1664
1665 /* bearer capability */
1666 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1667 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1668
1669 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1670
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001671 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001672}
1673
1674static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1675{
1676 struct gsm48_hdr *gh = msgb_l3(msg);
1677 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1678 struct tlv_parsed tp;
1679 struct gsm_mncc modify;
1680
1681 gsm48_stop_cc_timer(trans);
1682
1683 memset(&modify, 0, sizeof(struct gsm_mncc));
1684 modify.callref = trans->callref;
1685 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1686 /* bearer capability */
1687 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1688 modify.fields |= MNCC_F_BEARER_CAP;
1689 gsm48_decode_bearer_cap(&modify.bearer_cap,
1690 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1691
1692 /* Create a copy of the bearer capability
1693 * in the transaction struct, so we can use
1694 * this information later */
1695 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1696 sizeof(trans->bearer_cap));
1697 }
1698
1699 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1700
1701 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1702}
1703
1704static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1705{
1706 struct gsm_mncc *modify = arg;
1707 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1708 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1709
1710 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1711
1712 /* bearer capability */
1713 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1714 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1715
1716 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1717
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001718 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001719}
1720
1721static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1722{
1723 struct gsm48_hdr *gh = msgb_l3(msg);
1724 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1725 struct tlv_parsed tp;
1726 struct gsm_mncc modify;
1727
1728 gsm48_stop_cc_timer(trans);
1729
1730 memset(&modify, 0, sizeof(struct gsm_mncc));
1731 modify.callref = trans->callref;
1732 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1733 /* bearer capability */
1734 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1735 modify.fields |= GSM48_IE_BEARER_CAP;
1736 gsm48_decode_bearer_cap(&modify.bearer_cap,
1737 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1738
1739 /* Create a copy of the bearer capability
1740 * in the transaction struct, so we can use
1741 * this information later */
1742 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1743 sizeof(trans->bearer_cap));
1744 }
1745 /* cause */
1746 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1747 modify.fields |= MNCC_F_CAUSE;
1748 gsm48_decode_cause(&modify.cause,
1749 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1750 }
1751
1752 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1753
1754 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1755}
1756
1757static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1758{
1759 struct gsm_mncc *modify = arg;
1760 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1761 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1762
1763 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1764
1765 /* bearer capability */
1766 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1767 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1768 /* cause */
1769 gsm48_encode_cause(msg, 1, &modify->cause);
1770
1771 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1772
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001773 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001774}
1775
1776static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1777{
1778 struct gsm_mncc *notify = arg;
1779 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1780 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1781
1782 gh->msg_type = GSM48_MT_CC_NOTIFY;
1783
1784 /* notify */
1785 gsm48_encode_notify(msg, notify->notify);
1786
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001787 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001788}
1789
1790static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1791{
1792 struct gsm48_hdr *gh = msgb_l3(msg);
1793 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1794// struct tlv_parsed tp;
1795 struct gsm_mncc notify;
1796
1797 memset(&notify, 0, sizeof(struct gsm_mncc));
1798 notify.callref = trans->callref;
1799// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1800 if (payload_len >= 1)
1801 gsm48_decode_notify(&notify.notify, gh->data);
1802
1803 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1804}
1805
1806static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1807{
1808 struct gsm_mncc *user = arg;
1809 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1810 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1811
1812 gh->msg_type = GSM48_MT_CC_USER_INFO;
1813
1814 /* user-user */
1815 if (user->fields & MNCC_F_USERUSER)
1816 gsm48_encode_useruser(msg, 1, &user->useruser);
1817 /* more data */
1818 if (user->more)
1819 gsm48_encode_more(msg);
1820
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001821 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001822}
1823
1824static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1825{
1826 struct gsm48_hdr *gh = msgb_l3(msg);
1827 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1828 struct tlv_parsed tp;
1829 struct gsm_mncc user;
1830
1831 memset(&user, 0, sizeof(struct gsm_mncc));
1832 user.callref = trans->callref;
1833 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1834 /* user-user */
1835 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1836 user.fields |= MNCC_F_USERUSER;
1837 gsm48_decode_useruser(&user.useruser,
1838 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1839 }
1840 /* more data */
1841 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1842 user.more = 1;
1843
1844 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1845}
1846
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001847static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1848 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001849 uint32_t payload_msg_type, const struct sdp_msg *sdp)
Harald Welte27989d42018-06-21 20:39:20 +02001850{
1851 uint8_t data[sizeof(struct gsm_mncc)];
1852 struct gsm_mncc_rtp *rtp;
1853
1854 memset(&data, 0, sizeof(data));
1855 rtp = (struct gsm_mncc_rtp *) &data[0];
1856
1857 rtp->callref = callref;
1858 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001859 if (rtp_addr) {
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001860 if (osmo_sockaddr_str_to_sockaddr(rtp_addr, &rtp->addr) < 0)
1861 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001862 }
Harald Welte27989d42018-06-21 20:39:20 +02001863 rtp->payload_type = payload_type;
1864 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001865 if (sdp)
1866 sdp_msg_to_sdp_str_buf(rtp->sdp, sizeof(rtp->sdp), sdp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001867 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001868}
1869
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001870static 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 +02001871{
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001872 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0, NULL);
Harald Welte27989d42018-06-21 20:39:20 +02001873}
1874
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001875static int tch_rtp_create(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001876{
1877 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001878
1879 /* Find callref */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001880 trans = trans_find_by_callref(net, rtp->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001881 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001882 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001883 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001884 return -EIO;
1885 }
1886 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001887 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001888 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001889 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001890 return 0;
1891 }
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001892 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001893
Harald Welte27989d42018-06-21 20:39:20 +02001894 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001895 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001896}
1897
1898/* Trigger TCH_RTP_CREATE acknowledgement */
1899int gsm48_tch_rtp_create(struct gsm_trans *trans)
1900{
1901 /* This function is called as soon as the port, on which the
1902 * mgcp-gw expects the incoming RTP stream from the remote
1903 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001904 struct msc_a *msc_a = trans->msc_a;
1905 struct gsm_network *net = msc_a_net(msc_a);
1906 struct call_leg *cl = msc_a->cc.call_leg;
1907 struct osmo_sockaddr_str *rtp_cn_local;
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001908 struct rtp_stream *rtp_cn = cl ? cl->rtp[RTP_TO_CN] : NULL;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001909 int mncc_payload_msg_type;
1910 struct sdp_audio_codec *codec;
Neels Hofmeyra001a702022-10-31 17:57:30 +01001911 const struct codec_mapping *m;
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001912 struct sdp_audio_codecs *codecs;
Harald Welte27989d42018-06-21 20:39:20 +02001913
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001914 if (!rtp_cn) {
1915 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no RTP set up for the CN side\n");
1916 return -EINVAL;
1917 }
1918
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001919 codec_filter_run(&trans->cc.codecs);
1920 LOG_TRANS(trans, LOGL_DEBUG, "codecs: %s\n", codec_filter_to_str(&trans->cc.codecs));
1921 codecs = &trans->cc.codecs.result.audio_codecs;
1922 if (!codecs->count) {
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001923 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR,
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001924 "Cannot RTP CREATE to MNCC, there is no codec available\n");
Neels Hofmeyr5e19b9a2019-04-27 19:09:14 +02001925 return -EINVAL;
1926 }
1927
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001928 /* Populate the legacy MNCC codec elements: payload_type and payload_msg_type */
1929 codec = &codecs->codec[0];
1930 m = codec_mapping_by_subtype_name(codec->subtype_name);
1931 mncc_payload_msg_type = m ? m->mncc_payload_msg_type : 0;
Harald Welte27989d42018-06-21 20:39:20 +02001932
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001933 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1934 if (!rtp_cn_local) {
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001935 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port to CN set up\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001936 return -EINVAL;
1937 }
1938
Neels Hofmeyr006b0ee2022-11-07 16:59:09 +01001939 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local,
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001940 codec->payload_type, mncc_payload_msg_type, &trans->cc.codecs.result);
Harald Welte27989d42018-06-21 20:39:20 +02001941}
1942
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001943static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001944{
1945 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001946 struct call_leg *cl;
1947 struct rtp_stream *rtps;
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001948
Harald Welte27989d42018-06-21 20:39:20 +02001949 /* Find callref */
1950 trans = trans_find_by_callref(net, rtp->callref);
1951 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001952 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001953 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001954 return -EIO;
1955 }
1956 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001957 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001958 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001959 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001960 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001961 }
1962
Neels Hofmeyr58f40882023-03-08 04:04:27 +01001963 log_mncc_rx_tx(trans, "rx", (const union mncc_msg *)rtp);
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001964
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001965 cl = trans->msc_a->cc.call_leg;
1966 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1967
1968 if (!rtps) {
1969 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1970 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1971 return -EINVAL;
1972 }
1973
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001974 rx_mncc_sdp(trans, rtp->msg_type, rtp->sdp);
1975 rtp_stream_set_remote_addr_and_codecs(rtps, &trans->cc.codecs.remote);
1976
1977 if (!osmo_sockaddr_str_is_nonzero(&rtps->remote)) {
1978 /* Didn't get an IP address from SDP. Try legacy MNCC IP address */
1979 struct osmo_sockaddr_str rtp_addr;
1980 if (osmo_sockaddr_str_from_sockaddr(&rtp_addr, &rtp->addr) < 0) {
1981 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect with invalid IP addr\n");
1982 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1983 return -EINVAL;
1984 }
1985 rtp_stream_set_remote_addr(rtps, &rtp_addr);
Pau Espin Pedroleeda9e12020-09-03 22:11:03 +02001986 }
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01001987
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001988 rtp_stream_commit(rtps);
1989 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001990}
1991
1992static struct downstate {
1993 uint32_t states;
1994 int type;
1995 int (*rout) (struct gsm_trans *trans, void *arg);
1996} downstatelist[] = {
1997 /* mobile originating call establishment */
1998 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1999 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
2000 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2001 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2002 {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 */
2003 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2004 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2005 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2006 /* mobile terminating call establishment */
2007 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2008 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2009 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2010 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2011 /* signalling during call */
2012 {SBIT(GSM_CSTATE_ACTIVE),
2013 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2014 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2015 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2016 {ALL_STATES,
2017 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2018 {ALL_STATES,
2019 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2020 {ALL_STATES,
2021 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2022 {SBIT(GSM_CSTATE_ACTIVE),
2023 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2024 {SBIT(GSM_CSTATE_ACTIVE),
2025 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2026 {SBIT(GSM_CSTATE_ACTIVE),
2027 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2028 {SBIT(GSM_CSTATE_ACTIVE),
2029 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2030 {SBIT(GSM_CSTATE_ACTIVE),
2031 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2032 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2033 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2034 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2035 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2036 {SBIT(GSM_CSTATE_ACTIVE),
2037 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2038 /* clearing */
2039 {SBIT(GSM_CSTATE_INITIATED),
2040 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2041 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2042 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2043 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2044 MNCC_REL_REQ, gsm48_cc_tx_release},
2045};
2046
2047#define DOWNSLLEN \
2048 (sizeof(downstatelist) / sizeof(struct downstate))
2049
2050
Philipp Maiercd64af72019-08-01 09:46:40 +02002051static int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002052{
2053 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002054 struct msc_a *msc_a = NULL;
2055 struct gsm_trans *trans = NULL;
2056 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02002057
Harald Welte27989d42018-06-21 20:39:20 +02002058 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002059 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02002060 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002061 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02002062 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002063 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02002064 return rc;
2065 case MNCC_RTP_CREATE:
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002066 return tch_rtp_create(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002067 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002068 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02002069 case MNCC_RTP_FREE:
2070 /* unused right now */
2071 return -EIO;
2072
2073 case MNCC_FRAME_DROP:
2074 case MNCC_FRAME_RECV:
2075 case GSM_TCHF_FRAME:
2076 case GSM_TCHF_FRAME_EFR:
2077 case GSM_TCHH_FRAME:
2078 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002079 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002080 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002081 return -ENOTSUP;
2082 }
2083
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002084 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02002085
2086 /* Find callref */
2087 trans = trans_find_by_callref(net, data->callref);
2088
2089 /* Callref unknown */
2090 if (!trans) {
2091 struct vlr_subscr *vsub;
2092
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002093 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002094 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002095 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002096 /* Invalid call reference */
2097 return mncc_release_ind(net, NULL, data->callref,
2098 GSM48_CAUSE_LOC_PRN_S_LU,
2099 GSM48_CC_CAUSE_INVAL_TRANS_ID);
2100 }
2101 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002102 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002103 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02002104 /* Invalid number */
2105 return mncc_release_ind(net, NULL, data->callref,
2106 GSM48_CAUSE_LOC_PRN_S_LU,
2107 GSM48_CC_CAUSE_INV_NR_FORMAT);
2108 }
2109 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002110 if (data->called.number[0]) {
2111 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
2112 if (!vsub)
2113 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002114 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002115 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002116 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002117 if (!vsub)
2118 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002119 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002120 }
2121 if (!vsub)
2122 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
Neels Hofmeyr43a349f2019-08-22 22:30:20 +02002123 GSM48_CC_CAUSE_USER_NOTRESPOND);
Harald Welte27989d42018-06-21 20:39:20 +02002124 /* update the subscriber we deal with */
2125 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
2126
Harald Welte27989d42018-06-21 20:39:20 +02002127 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002128 if (!vsub->lu_complete) {
2129 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002130 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002131 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002132 /* Temporarily out of order */
2133 return mncc_release_ind(net, NULL, data->callref,
2134 GSM48_CAUSE_LOC_PRN_S_LU,
2135 GSM48_CC_CAUSE_DEST_OOO);
2136 }
Keith Whyte991bb422019-08-08 15:43:40 +02002137
2138 /* Find valid conn */
2139 msc_a = msc_a_for_vsub(vsub, true);
2140
2141 /* If subscriber is BUSY and we do not DO call in call aka "call-waiting" */
2142 if (!net->call_waiting && msc_a) {
2143 struct gsm_trans *existing_cc_trans = trans_find_by_type(msc_a, TRANS_CC);
2144 if (existing_cc_trans && existing_cc_trans->cc.state != GSM_CSTATE_NULL) {
2145 LOG_TRANS_CAT(existing_cc_trans, DCC, LOGL_NOTICE,
2146 "rx '%s' for subscriber %s with trans state (%s)"
2147 " rejecting with USER_BUSY\n",
2148 get_mncc_name(msg->msg_type), data->called.number,
2149 gsm48_cc_state_name(existing_cc_trans->cc.state));
2150 return mncc_release_ind(net, NULL, data->callref,
2151 GSM48_CAUSE_LOC_PRN_S_LU,
2152 GSM48_CC_CAUSE_USER_BUSY);
2153 }
2154 }
2155
Harald Welte27989d42018-06-21 20:39:20 +02002156 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002157 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07002158 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02002159 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002160 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002161 vlr_subscr_put(vsub, __func__);
Martin Hauke3f07dac2019-11-14 17:49:08 +01002162 /* Resource unavailable */
Harald Welte27989d42018-06-21 20:39:20 +02002163 mncc_release_ind(net, NULL, data->callref,
2164 GSM48_CAUSE_LOC_PRN_S_LU,
2165 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
2166 return -ENOMEM;
2167 }
2168
Neels Hofmeyr8dd16462022-01-13 20:06:53 +01002169 /* Remember remote SDP, if any */
2170 rx_mncc_sdp(trans, data->msg_type, data->sdp);
2171
Harald Welte27989d42018-06-21 20:39:20 +02002172 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002173 if (!msc_a) {
Neels Hofmeyrc67b4832019-10-21 02:34:54 +02002174 /* This condition will return before the common logging of the received MNCC message below, so
2175 * log it now. */
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002176 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002177
Harald Welte27989d42018-06-21 20:39:20 +02002178 /* store setup information until paging succeeds */
2179 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
2180
Neels Hofmeyrbde605d2019-10-21 03:07:25 +02002181 /* Request a channel. If Paging already started, paging_request_start() will append the new
2182 * trans to the already ongoing Paging. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002183 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
2184 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02002185 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002186 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02002187 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02002188 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002189 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002190 return 0;
2191 }
2192
2193 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002194 trans->msc_a = msc_a;
2195 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002196 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01002197 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02002198 } else {
2199 /* update the subscriber we deal with */
2200 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2201 }
2202
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002203 log_mncc_rx_tx(trans, "rx", msg);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002204
Philipp Maier9ca7b312018-10-10 17:00:49 +02002205 gsm48_start_guard_timer(trans);
Neels Hofmeyrcf90bdb2019-10-01 19:47:26 +02002206 trans->cc.mncc_initiated = true;
Philipp Maier9ca7b312018-10-10 17:00:49 +02002207
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002208 if (trans->msc_a)
2209 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002210
2211 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002212 if (!msc_a) {
2213 struct gsm_mncc rel = {
2214 .callref = data->callref,
2215 };
Neels Hofmeyr58f40882023-03-08 04:04:27 +01002216 LOG_TRANS(trans, LOGL_DEBUG, "still paging\n");
Harald Welte27989d42018-06-21 20:39:20 +02002217 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2218 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002219 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02002220 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2221 else
2222 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2223 trans->callref = 0;
2224 trans_free(trans);
2225 return rc;
Harald Welte27989d42018-06-21 20:39:20 +02002226 }
2227
2228 /* Find function for current state and message */
2229 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002230 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02002231 && ((1 << trans->cc.state) & downstatelist[i].states))
2232 break;
2233 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002234 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002235 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02002236 return 0;
2237 }
2238
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002239 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02002240
2241 return rc;
2242}
2243
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002244struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
2245{
2246 uint32_t callref;
2247
2248 switch (msg->msg_type) {
2249 case MNCC_BRIDGE:
2250 callref = msg->bridge.callref[0];
2251 break;
2252 case MNCC_RTP_CREATE:
2253 case MNCC_RTP_CONNECT:
2254 callref = msg->rtp.callref;
2255 break;
2256
2257 case MNCC_RTP_FREE:
2258 case MNCC_FRAME_DROP:
2259 case MNCC_FRAME_RECV:
2260 case GSM_TCHF_FRAME:
2261 case GSM_TCHF_FRAME_EFR:
2262 case GSM_TCHH_FRAME:
2263 case GSM_TCH_FRAME_AMR:
2264 return NULL;
2265
2266 default:
2267 callref = msg->signal.callref;
2268 break;
2269 }
2270
2271 return mncc_call_find_by_callref(callref);
2272}
2273
2274/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002275int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002276{
2277 const union mncc_msg *msg = arg;
2278 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002279
2280 if (msg->msg_type == MNCC_SETUP_REQ) {
2281 /* Incoming call to forward for inter-MSC Handover? */
2282 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2283 if (mncc_call)
2284 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2285 "Incoming call matches pending inter-MSC Handover Number\n");
2286 }
2287 if (!mncc_call) {
2288 /* Find already active MNCC FSM for this callref.
2289 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2290 * MNCC<->GSM-CC call handling. */
2291 mncc_call = mncc_find_by_callref_from_msg(msg);
2292 }
2293 if (mncc_call) {
2294 mncc_call_rx(mncc_call, msg);
2295 return 0;
2296 }
2297
2298 /* None of the above? Then it must be a normal GSM CC call related message. */
2299 return mncc_tx_to_gsm_cc(net, msg);
2300}
Harald Welte27989d42018-06-21 20:39:20 +02002301
2302static struct datastate {
2303 uint32_t states;
2304 int type;
2305 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2306} datastatelist[] = {
2307 /* mobile originating call establishment */
2308 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2309 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2310 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2311 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2312 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2313 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2314 /* mobile terminating call establishment */
2315 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2316 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2317 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2318 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2319 {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 */
2320 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2321 /* signalling during call */
2322 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2323 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2324 {SBIT(GSM_CSTATE_ACTIVE),
2325 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2326 {ALL_STATES,
2327 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2328 {ALL_STATES,
2329 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2330 {ALL_STATES,
2331 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2332 {SBIT(GSM_CSTATE_ACTIVE),
2333 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2334 {SBIT(GSM_CSTATE_ACTIVE),
2335 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2336 {SBIT(GSM_CSTATE_ACTIVE),
2337 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2338 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2339 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2340 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2341 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2342 {SBIT(GSM_CSTATE_ACTIVE),
2343 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2344 /* clearing */
2345 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2346 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2347 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2348 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2349 {ALL_STATES, /* 5.4.3.4 */
2350 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2351};
2352
2353#define DATASLLEN \
2354 (sizeof(datastatelist) / sizeof(struct datastate))
2355
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002356int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002357{
2358 struct gsm48_hdr *gh = msgb_l3(msg);
2359 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2360 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2361 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002362 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2363 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002364 int i, rc = 0;
2365
2366 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002367 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002368 return -EINVAL;
2369 }
2370
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002371 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002372 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002373 return -EINVAL;
2374 }
2375
2376 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002377 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002378
Harald Welte27989d42018-06-21 20:39:20 +02002379 /* Create transaction */
2380 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002381 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002382 trans = trans_alloc(net, vsub,
2383 TRANS_CC,
2384 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002385 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002386 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002387 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002388 GSM48_PDISC_CC | (transaction_id << 4),
2389 GSM48_MT_CC_RELEASE_COMPL);
2390 return -ENOMEM;
2391 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002392 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2393 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2394 trans_free(trans);
2395 return -EINVAL;
2396 }
2397
Harald Welte27989d42018-06-21 20:39:20 +02002398 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002399 msc_a_get(msc_a, MSC_A_USE_CC);
2400 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002401 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002402
2403 /* An earlier CM Service Request for this CC message now has concluded */
2404 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2405 LOG_MSC_A(msc_a, LOGL_ERROR,
2406 "Creating new CC transaction without prior CM Service Request\n");
2407 else
2408 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002409 }
2410
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002411 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2412 gsm48_cc_state_name(trans->cc.state));
2413
Harald Welte27989d42018-06-21 20:39:20 +02002414 /* find function for current state and message */
2415 for (i = 0; i < DATASLLEN; i++)
2416 if ((msg_type == datastatelist[i].type)
2417 && ((1 << trans->cc.state) & datastatelist[i].states))
2418 break;
2419 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002420 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002421
2422 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2423 * transaction right away. */
2424 if (trans->cc.state == GSM_CSTATE_NULL) {
2425 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2426 " -- disarding new CC transaction right away\n");
2427 trans_free(trans);
2428 }
Harald Welte27989d42018-06-21 20:39:20 +02002429 return 0;
2430 }
2431
2432 assert(trans->vsub);
2433
2434 rc = datastatelist[i].rout(trans, msg);
2435
Harald Welte27989d42018-06-21 20:39:20 +02002436 return rc;
2437}