blob: 2869bba1224348dcbd64c828b6019d7c44080cb2 [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
Harald Welte27989d42018-06-21 20:39:20 +020033#include <osmocom/msc/db.h>
34#include <osmocom/msc/debug.h>
35#include <osmocom/msc/gsm_data.h>
36#include <osmocom/msc/gsm_subscriber.h>
37#include <osmocom/msc/gsm_04_11.h>
38#include <osmocom/msc/gsm_04_08.h>
39#include <osmocom/msc/gsm_04_80.h>
40#include <osmocom/msc/gsm_04_14.h>
41#include <osmocom/msc/gsm_09_11.h>
42#include <osmocom/msc/signal.h>
43#include <osmocom/msc/transaction.h>
44#include <osmocom/msc/silent_call.h>
Harald Welte27989d42018-06-21 20:39:20 +020045#include <osmocom/msc/mncc_int.h>
46#include <osmocom/abis/e1_input.h>
47#include <osmocom/core/bitvec.h>
48#include <osmocom/msc/vlr.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010049#include <osmocom/msc/msub.h>
50#include <osmocom/msc/msc_a.h>
51#include <osmocom/msc/paging.h>
52#include <osmocom/msc/call_leg.h>
53#include <osmocom/msc/rtp_stream.h>
54#include <osmocom/msc/mncc_call.h>
55#include <osmocom/msc/msc_t.h>
Harald Welte27989d42018-06-21 20:39:20 +020056
57#include <osmocom/gsm/gsm48.h>
58#include <osmocom/gsm/gsm0480.h>
59#include <osmocom/gsm/gsm_utils.h>
60#include <osmocom/gsm/protocol/gsm_04_08.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/core/utils.h>
64#include <osmocom/core/byteswap.h>
65#include <osmocom/gsm/tlv.h>
66#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020067
68#include <assert.h>
69
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010070static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
71static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
72static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
73
74static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
75{
76 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
77 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
78 OMSC_LINKID_CB(msg) = trans->dlci;
79
80 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
81}
82
83uint32_t msc_cc_next_outgoing_callref() {
84 static uint32_t last_callref = 0x80000000;
85 last_callref++;
86 if (last_callref < 0x80000001)
87 last_callref = 0x80000001;
88 return last_callref;
89}
Harald Welte27989d42018-06-21 20:39:20 +020090
Philipp Maier9ca7b312018-10-10 17:00:49 +020091static void gsm48_cc_guard_timeout(void *arg)
92{
93 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010094 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020095 trans_free(trans);
96 return;
97}
98
99static void gsm48_stop_guard_timer(struct gsm_trans *trans)
100{
101 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100102 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200103 osmo_timer_del(&trans->cc.timer_guard);
104 }
105}
106
107static void gsm48_start_guard_timer(struct gsm_trans *trans)
108{
109 /* NOTE: The purpose of this timer is to prevent the cc state machine
110 * from hanging in cases where mncc, gsm48 or both become unresponsive
111 * for some reason. The timer is started initially with the setup from
112 * the gsm48 side and then re-started with every incoming mncc message.
113 * Once the mncc state reaches its active state the timer is stopped.
114 * So if the cc state machine does not show any activity for an
115 * extended amount of time during call setup or teardown the guard
116 * timer will time out and hard-clear the connection. */
117 if (osmo_timer_pending(&trans->cc.timer_guard))
118 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100119 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200120 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
121 osmo_timer_schedule(&trans->cc.timer_guard,
122 trans->net->mncc_guard_timeout, 0);
123}
Harald Welte27989d42018-06-21 20:39:20 +0200124
125/* Call Control */
126
127void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
128{
129 net->mncc_recv(net, msg);
130}
131
132int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
133{
134 struct gsm48_hdr *gh;
135 struct msgb *ss_notify;
136
137 ss_notify = gsm0480_create_notifySS(message);
138 if (!ss_notify)
139 return -1;
140
141 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
142 uint8_t *data = msgb_push(ss_notify, 1);
143 data[0] = ss_notify->len - 1;
144 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
145 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100146 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200147}
148
149/* FIXME: this count_statistics is a state machine behaviour. we should convert
150 * the complete call control into a state machine. Afterwards we can move this
151 * code into state transitions.
152 */
153static void count_statistics(struct gsm_trans *trans, int new_state)
154{
155 int old_state = trans->cc.state;
156 struct rate_ctr_group *msc = trans->net->msc_ctrs;
157
158 if (old_state == new_state)
159 return;
160
161 /* state incoming */
162 switch (new_state) {
163 case GSM_CSTATE_ACTIVE:
164 osmo_counter_inc(trans->net->active_calls);
165 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
166 break;
167 }
168
169 /* state outgoing */
170 switch (old_state) {
171 case GSM_CSTATE_ACTIVE:
172 osmo_counter_dec(trans->net->active_calls);
173 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
174 new_state == GSM_CSTATE_DISCONNECT_IND)
175 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
176 else
177 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
178 break;
179 }
180}
181
Harald Welte27989d42018-06-21 20:39:20 +0200182static void new_cc_state(struct gsm_trans *trans, int state)
183{
184 if (state > 31 || state < 0)
185 return;
186
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100187 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
188 gsm48_cc_state_name(trans->cc.state),
189 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200190
191 count_statistics(trans, state);
192 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200193
194 /* Stop the guard timer when a call reaches the active state */
195 if (state == GSM_CSTATE_ACTIVE)
196 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200197}
198
199static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
200{
201 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
202 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
203 uint8_t *cause, *call_state;
204
205 gh->msg_type = GSM48_MT_CC_STATUS;
206
207 cause = msgb_put(msg, 3);
208 cause[0] = 2;
209 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
210 cause[2] = 0x80 | 30; /* response to status inquiry */
211
212 call_state = msgb_put(msg, 1);
213 call_state[0] = 0xc0 | 0x00;
214
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100215 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200216}
217
218static void gsm48_stop_cc_timer(struct gsm_trans *trans)
219{
220 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100221 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200222 osmo_timer_del(&trans->cc.timer);
223 trans->cc.Tcurrent = 0;
224 }
225}
226
227static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
228 int msg_type, struct gsm_mncc *mncc)
229{
230 struct msgb *msg;
231 unsigned char *data;
232
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100233 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200234
235 mncc->msg_type = msg_type;
236
237 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
238 if (!msg)
239 return -ENOMEM;
240
241 data = msgb_put(msg, sizeof(struct gsm_mncc));
242 memcpy(data, mncc, sizeof(struct gsm_mncc));
243
244 cc_tx_to_mncc(net, msg);
245
246 return 0;
247}
248
249int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
250 uint32_t callref, int location, int value)
251{
252 struct gsm_mncc rel;
253
254 memset(&rel, 0, sizeof(rel));
255 rel.callref = callref;
256 mncc_set_cause(&rel, location, value);
257 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
258 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
259 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
260}
261
262/* Call Control Specific transaction release.
263 * gets called by trans_free, DO NOT CALL YOURSELF! */
264void _gsm48_cc_trans_free(struct gsm_trans *trans)
265{
266 gsm48_stop_cc_timer(trans);
267
Harald Welte27989d42018-06-21 20:39:20 +0200268 /* send release to L4, if callref still exists */
269 if (trans->callref) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100270 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
271 * CC Release to the MS if it gets freed here. Hack it to do so. */
272 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
273 struct gsm_mncc rel = {};
274 rel.callref = trans->callref;
275 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
276 gsm48_cc_tx_release(trans, &rel);
277 }
Harald Welte27989d42018-06-21 20:39:20 +0200278 /* Ressource unavailable */
279 mncc_release_ind(trans->net, trans, trans->callref,
280 GSM48_CAUSE_LOC_PRN_S_LU,
281 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
282 /* This is a final freeing of the transaction. The MNCC release may have triggered the
283 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
284 gsm48_stop_cc_timer(trans);
285 }
286 if (trans->cc.state != GSM_CSTATE_NULL)
287 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200288
289 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100290
291 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
292 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200293}
294
Harald Welte27989d42018-06-21 20:39:20 +0200295/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100296static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200297{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100298 if (trans->msc_a) {
299 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
300 "Handle paging error: transaction already associated with subscriber,"
301 " apparently it was already handled. Skip.\n");
302 return;
Harald Welte27989d42018-06-21 20:39:20 +0200303 }
304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100305 if (msc_a) {
306 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
307 /* Assign conn */
308 msc_a_get(msc_a, MSC_A_USE_CC);
309 trans->msc_a = msc_a;
310 trans->paging_request = NULL;
311 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
312 /* send SETUP request to called party */
313 gsm48_cc_tx_setup(trans, &trans->cc.msg);
314 } else {
315 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
316 /* Temporarily out of order */
317 mncc_release_ind(trans->net, trans,
318 trans->callref,
319 GSM48_CAUSE_LOC_PRN_S_LU,
320 GSM48_CC_CAUSE_DEST_OOO);
321 trans->callref = 0;
322 trans->paging_request = NULL;
323 trans_free(trans);
324 }
Harald Welte27989d42018-06-21 20:39:20 +0200325}
326
327/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100328static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200329{
330 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
331 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100332 struct call_leg *cl1;
333 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200334
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100335 if (!trans1 || !trans2) {
336 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200337 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100338 }
Harald Welte27989d42018-06-21 20:39:20 +0200339
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100340 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100341 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
342 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 +0200343 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100344 }
345
346 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
347 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200348
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100349 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
350 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
351 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
352 * If we can't, then we must give up. */
353 cl1 = trans1->msc_a->cc.call_leg;
354 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200355
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100356 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200357}
358
359static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
360{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100361 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200362 return gsm48_cc_tx_status(trans, msg);
363}
364
Harald Welte27989d42018-06-21 20:39:20 +0200365static void gsm48_cc_timeout(void *arg)
366{
367 struct gsm_trans *trans = arg;
368 int disconnect = 0, release = 0;
369 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
370 int mo_location = GSM48_CAUSE_LOC_USER;
371 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
372 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
373 struct gsm_mncc mo_rel, l4_rel;
374
375 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
376 mo_rel.callref = trans->callref;
377 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
378 l4_rel.callref = trans->callref;
379
380 switch(trans->cc.Tcurrent) {
381 case 0x303:
382 release = 1;
383 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
384 break;
385 case 0x310:
386 disconnect = 1;
387 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
388 break;
389 case 0x313:
390 disconnect = 1;
391 /* unknown, did not find it in the specs */
392 break;
393 case 0x301:
394 disconnect = 1;
395 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
396 break;
397 case 0x308:
398 if (!trans->cc.T308_second) {
399 /* restart T308 a second time */
400 gsm48_cc_tx_release(trans, &trans->cc.msg);
401 trans->cc.T308_second = 1;
402 break; /* stay in release state */
403 }
404 trans_free(trans);
405 return;
406 case 0x306:
407 release = 1;
408 mo_cause = trans->cc.msg.cause.value;
409 mo_location = trans->cc.msg.cause.location;
410 break;
411 case 0x323:
412 disconnect = 1;
413 break;
414 default:
415 release = 1;
416 }
417
418 if (release && trans->callref) {
419 /* process release towards layer 4 */
420 mncc_release_ind(trans->net, trans, trans->callref,
421 l4_location, l4_cause);
422 trans->callref = 0;
423 }
424
425 if (disconnect && trans->callref) {
426 /* process disconnect towards layer 4 */
427 mncc_set_cause(&l4_rel, l4_location, l4_cause);
428 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
429 }
430
431 /* process disconnect towards mobile station */
432 if (disconnect || release) {
433 mncc_set_cause(&mo_rel, mo_location, mo_cause);
434 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
435 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
436 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
437 mo_rel.cause.diag_len = 3;
438
439 if (disconnect)
440 gsm48_cc_tx_disconnect(trans, &mo_rel);
441 if (release)
442 gsm48_cc_tx_release(trans, &mo_rel);
443 }
444
445}
446
447/* disconnect both calls from the bridge */
448static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100449 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200450{
451 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
452 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
453 struct gsm_mncc mx_rel;
454 if (!trans0 || !trans1)
455 return;
456
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100457 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
458 trans0->callref, trans1->callref, strerror(err));
459 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200460 trans0->callref, trans1->callref, strerror(err));
461
462 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
463 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
464 GSM48_CC_CAUSE_CHAN_UNACCEPT);
465
466 mx_rel.callref = trans0->callref;
467 gsm48_cc_tx_disconnect(trans0, &mx_rel);
468
469 mx_rel.callref = trans1->callref;
470 gsm48_cc_tx_disconnect(trans1, &mx_rel);
471}
472
473static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
474 int sec, int micro)
475{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100476 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200477 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
478 osmo_timer_schedule(&trans->cc.timer, sec, micro);
479 trans->cc.Tcurrent = current;
480}
481
482static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
483{
484 struct gsm48_hdr *gh = msgb_l3(msg);
485 uint8_t msg_type = gsm48_hdr_msg_type(gh);
486 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
487 struct tlv_parsed tp;
488 struct gsm_mncc setup;
489
Philipp Maier9ca7b312018-10-10 17:00:49 +0200490 gsm48_start_guard_timer(trans);
491
Harald Welte27989d42018-06-21 20:39:20 +0200492 memset(&setup, 0, sizeof(struct gsm_mncc));
493 setup.callref = trans->callref;
494
495 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
496 /* emergency setup is identified by msg_type */
497 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
498 setup.fields |= MNCC_F_EMERGENCY;
499 setup.emergency = 1;
500 /* use destination number as configured by user (if any) */
501 if (trans->net->emergency.route_to_msisdn) {
502 setup.fields |= MNCC_F_CALLED;
503 setup.called.type = 0; /* unknown */
504 setup.called.plan = 0; /* unknown */
505 OSMO_STRLCPY_ARRAY(setup.called.number,
506 trans->net->emergency.route_to_msisdn);
507 }
508 }
509
510 /* use subscriber as calling party number */
511 setup.fields |= MNCC_F_CALLING;
512 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
513 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
514
515 /* bearer capability */
516 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
517 setup.fields |= MNCC_F_BEARER_CAP;
518 gsm48_decode_bearer_cap(&setup.bearer_cap,
519 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
520
521 /* Create a copy of the bearer capability
522 * in the transaction struct, so we can use
523 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100524 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200525 sizeof(trans->bearer_cap));
526 }
527 /* facility */
528 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
529 setup.fields |= MNCC_F_FACILITY;
530 gsm48_decode_facility(&setup.facility,
531 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
532 }
533 /* called party bcd number */
534 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
535 setup.fields |= MNCC_F_CALLED;
536 gsm48_decode_called(&setup.called,
537 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
538 }
539 /* user-user */
540 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
541 setup.fields |= MNCC_F_USERUSER;
542 gsm48_decode_useruser(&setup.useruser,
543 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
544 }
545 /* ss-version */
546 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
547 setup.fields |= MNCC_F_SSVERSION;
548 gsm48_decode_ssversion(&setup.ssversion,
549 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
550 }
551 /* CLIR suppression */
552 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
553 setup.clir.sup = 1;
554 /* CLIR invocation */
555 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
556 setup.clir.inv = 1;
557 /* cc cap */
558 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
559 setup.fields |= MNCC_F_CCCAP;
560 gsm48_decode_cccap(&setup.cccap,
561 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
562 }
563
564 new_cc_state(trans, GSM_CSTATE_INITIATED);
565
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100566 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
567 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200568
569 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
570
571 /* indicate setup to MNCC */
572 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
573
574 /* MNCC code will modify the channel asynchronously, we should
575 * ipaccess-bind only after the modification has been made to the
576 * lchan->tch_mode */
577 return 0;
578}
579
580static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
581{
582 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
583 struct gsm48_hdr *gh;
584 struct gsm_mncc *setup = arg;
585 int rc, trans_id;
586
587 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
588
589 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700590 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100591 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200592 "This is not allowed!\n");
593 /* Temporarily out of order */
594 rc = mncc_release_ind(trans->net, trans, trans->callref,
595 GSM48_CAUSE_LOC_PRN_S_LU,
596 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
597 trans->callref = 0;
598 trans_free(trans);
599 return rc;
600 }
601
602 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100603 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200604 if (trans_id < 0) {
605 /* no free transaction ID */
606 rc = mncc_release_ind(trans->net, trans, trans->callref,
607 GSM48_CAUSE_LOC_PRN_S_LU,
608 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
609 trans->callref = 0;
610 trans_free(trans);
611 return rc;
612 }
613 trans->transaction_id = trans_id;
614
615 gh->msg_type = GSM48_MT_CC_SETUP;
616
617 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
618
619 /* bearer capability */
620 if (setup->fields & MNCC_F_BEARER_CAP) {
621 /* Create a copy of the bearer capability in the transaction struct, so we
622 * can use this information later */
623 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
624 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
625 }
626 /* facility */
627 if (setup->fields & MNCC_F_FACILITY)
628 gsm48_encode_facility(msg, 0, &setup->facility);
629 /* progress */
630 if (setup->fields & MNCC_F_PROGRESS)
631 gsm48_encode_progress(msg, 0, &setup->progress);
632 /* calling party BCD number */
633 if (setup->fields & MNCC_F_CALLING)
634 gsm48_encode_calling(msg, &setup->calling);
635 /* called party BCD number */
636 if (setup->fields & MNCC_F_CALLED)
637 gsm48_encode_called(msg, &setup->called);
638 /* user-user */
639 if (setup->fields & MNCC_F_USERUSER)
640 gsm48_encode_useruser(msg, 0, &setup->useruser);
641 /* redirecting party BCD number */
642 if (setup->fields & MNCC_F_REDIRECTING)
643 gsm48_encode_redirecting(msg, &setup->redirecting);
644 /* signal */
645 if (setup->fields & MNCC_F_SIGNAL)
646 gsm48_encode_signal(msg, setup->signal);
647
648 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
649
650 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
651
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100652 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200653}
654
655static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
656{
657 struct gsm48_hdr *gh = msgb_l3(msg);
658 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
659 struct tlv_parsed tp;
660 struct gsm_mncc call_conf;
661 int rc;
662
663 gsm48_stop_cc_timer(trans);
664 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
665
666 memset(&call_conf, 0, sizeof(struct gsm_mncc));
667 call_conf.callref = trans->callref;
668
669 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
670#if 0
671 /* repeat */
672 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
673 call_conf.repeat = 1;
674 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
675 call_conf.repeat = 2;
676#endif
677 /* bearer capability */
678 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
679 call_conf.fields |= MNCC_F_BEARER_CAP;
680 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
681 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
682
683 /* Create a copy of the bearer capability
684 * in the transaction struct, so we can use
685 * this information later */
686 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
687 sizeof(trans->bearer_cap));
688 }
689 /* cause */
690 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
691 call_conf.fields |= MNCC_F_CAUSE;
692 gsm48_decode_cause(&call_conf.cause,
693 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
694 }
695 /* cc cap */
696 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
697 call_conf.fields |= MNCC_F_CCCAP;
698 gsm48_decode_cccap(&call_conf.cccap,
699 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
700 }
701
702 /* IMSI of called subscriber */
703 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
704
705 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
706
707 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100708 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200709
710 /* don't continue, if there were problems with
711 * the call assignment. */
712 if (rc)
713 return rc;
714
715 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
716 &call_conf);
717}
718
719static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
720{
721 struct gsm_mncc *proceeding = arg;
722 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
723 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
724 int rc;
725
726 gh->msg_type = GSM48_MT_CC_CALL_PROC;
727
728 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
729
730 /* bearer capability */
731 if (proceeding->fields & MNCC_F_BEARER_CAP) {
732 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
733 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
734 }
735 /* facility */
736 if (proceeding->fields & MNCC_F_FACILITY)
737 gsm48_encode_facility(msg, 0, &proceeding->facility);
738 /* progress */
739 if (proceeding->fields & MNCC_F_PROGRESS)
740 gsm48_encode_progress(msg, 0, &proceeding->progress);
741
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100742 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200743 if (rc)
744 return rc;
745
746 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100747 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200748}
749
750static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
751{
752 struct gsm48_hdr *gh = msgb_l3(msg);
753 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
754 struct tlv_parsed tp;
755 struct gsm_mncc alerting;
756
757 gsm48_stop_cc_timer(trans);
758 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
759
760 memset(&alerting, 0, sizeof(struct gsm_mncc));
761 alerting.callref = trans->callref;
762 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
763 /* facility */
764 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
765 alerting.fields |= MNCC_F_FACILITY;
766 gsm48_decode_facility(&alerting.facility,
767 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
768 }
769
770 /* progress */
771 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
772 alerting.fields |= MNCC_F_PROGRESS;
773 gsm48_decode_progress(&alerting.progress,
774 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
775 }
776 /* ss-version */
777 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
778 alerting.fields |= MNCC_F_SSVERSION;
779 gsm48_decode_ssversion(&alerting.ssversion,
780 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
781 }
782
783 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
784
785 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
786 &alerting);
787}
788
789static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
790{
791 struct gsm_mncc *alerting = arg;
792 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
793 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
794
795 gh->msg_type = GSM48_MT_CC_ALERTING;
796
797 /* facility */
798 if (alerting->fields & MNCC_F_FACILITY)
799 gsm48_encode_facility(msg, 0, &alerting->facility);
800 /* progress */
801 if (alerting->fields & MNCC_F_PROGRESS)
802 gsm48_encode_progress(msg, 0, &alerting->progress);
803 /* user-user */
804 if (alerting->fields & MNCC_F_USERUSER)
805 gsm48_encode_useruser(msg, 0, &alerting->useruser);
806
807 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
808
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100809 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200810}
811
812static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
813{
814 struct gsm_mncc *progress = arg;
815 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
816 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
817
818 gh->msg_type = GSM48_MT_CC_PROGRESS;
819
820 /* progress */
821 gsm48_encode_progress(msg, 1, &progress->progress);
822 /* user-user */
823 if (progress->fields & MNCC_F_USERUSER)
824 gsm48_encode_useruser(msg, 0, &progress->useruser);
825
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100826 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200827}
828
829static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
830{
831 struct gsm_mncc *connect = arg;
832 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
833 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
834
835 gh->msg_type = GSM48_MT_CC_CONNECT;
836
837 gsm48_stop_cc_timer(trans);
838 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
839
840 /* facility */
841 if (connect->fields & MNCC_F_FACILITY)
842 gsm48_encode_facility(msg, 0, &connect->facility);
843 /* progress */
844 if (connect->fields & MNCC_F_PROGRESS)
845 gsm48_encode_progress(msg, 0, &connect->progress);
846 /* connected number */
847 if (connect->fields & MNCC_F_CONNECTED)
848 gsm48_encode_connected(msg, &connect->connected);
849 /* user-user */
850 if (connect->fields & MNCC_F_USERUSER)
851 gsm48_encode_useruser(msg, 0, &connect->useruser);
852
853 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
854
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100855 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200856}
857
858static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
859{
860 struct gsm48_hdr *gh = msgb_l3(msg);
861 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
862 struct tlv_parsed tp;
863 struct gsm_mncc connect;
864
865 gsm48_stop_cc_timer(trans);
866
867 memset(&connect, 0, sizeof(struct gsm_mncc));
868 connect.callref = trans->callref;
869 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
870 /* use subscriber as connected party number */
871 connect.fields |= MNCC_F_CONNECTED;
872 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
873 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
874
875 /* facility */
876 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
877 connect.fields |= MNCC_F_FACILITY;
878 gsm48_decode_facility(&connect.facility,
879 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
880 }
881 /* user-user */
882 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
883 connect.fields |= MNCC_F_USERUSER;
884 gsm48_decode_useruser(&connect.useruser,
885 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
886 }
887 /* ss-version */
888 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
889 connect.fields |= MNCC_F_SSVERSION;
890 gsm48_decode_ssversion(&connect.ssversion,
891 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
892 }
893
894 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
895 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
896
897 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
898}
899
900
901static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
902{
903 struct gsm_mncc connect_ack;
904
905 gsm48_stop_cc_timer(trans);
906
907 new_cc_state(trans, GSM_CSTATE_ACTIVE);
908 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
909
910 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
911 connect_ack.callref = trans->callref;
912
913 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
914 &connect_ack);
915}
916
917static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
918{
919 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
920 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
921
922 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
923
924 new_cc_state(trans, GSM_CSTATE_ACTIVE);
925
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100926 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200927}
928
929static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
930{
931 struct gsm48_hdr *gh = msgb_l3(msg);
932 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
933 struct tlv_parsed tp;
934 struct gsm_mncc disc;
935
936 gsm48_stop_cc_timer(trans);
937
938 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
939
940 memset(&disc, 0, sizeof(struct gsm_mncc));
941 disc.callref = trans->callref;
942 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
943 /* cause */
944 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
945 disc.fields |= MNCC_F_CAUSE;
946 gsm48_decode_cause(&disc.cause,
947 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
948 }
949 /* facility */
950 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
951 disc.fields |= MNCC_F_FACILITY;
952 gsm48_decode_facility(&disc.facility,
953 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
954 }
955 /* user-user */
956 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
957 disc.fields |= MNCC_F_USERUSER;
958 gsm48_decode_useruser(&disc.useruser,
959 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
960 }
961 /* ss-version */
962 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
963 disc.fields |= MNCC_F_SSVERSION;
964 gsm48_decode_ssversion(&disc.ssversion,
965 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
966 }
967
968 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +0200969}
970
971static struct gsm_mncc_cause default_cause = {
972 .location = GSM48_CAUSE_LOC_PRN_S_LU,
973 .coding = 0,
974 .rec = 0,
975 .rec_val = 0,
976 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
977 .diag_len = 0,
978 .diag = { 0 },
979};
980
981static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
982{
983 struct gsm_mncc *disc = arg;
984 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
985 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
986
987 gh->msg_type = GSM48_MT_CC_DISCONNECT;
988
989 gsm48_stop_cc_timer(trans);
990 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
991
992 /* cause */
993 if (disc->fields & MNCC_F_CAUSE)
994 gsm48_encode_cause(msg, 1, &disc->cause);
995 else
996 gsm48_encode_cause(msg, 1, &default_cause);
997
998 /* facility */
999 if (disc->fields & MNCC_F_FACILITY)
1000 gsm48_encode_facility(msg, 0, &disc->facility);
1001 /* progress */
1002 if (disc->fields & MNCC_F_PROGRESS)
1003 gsm48_encode_progress(msg, 0, &disc->progress);
1004 /* user-user */
1005 if (disc->fields & MNCC_F_USERUSER)
1006 gsm48_encode_useruser(msg, 0, &disc->useruser);
1007
1008 /* store disconnect cause for T306 expiry */
1009 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1010
1011 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1012
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001013 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001014}
1015
1016static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1017{
1018 struct gsm48_hdr *gh = msgb_l3(msg);
1019 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1020 struct tlv_parsed tp;
1021 struct gsm_mncc rel;
1022 int rc;
1023
1024 gsm48_stop_cc_timer(trans);
1025
1026 memset(&rel, 0, sizeof(struct gsm_mncc));
1027 rel.callref = trans->callref;
1028 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1029 /* cause */
1030 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1031 rel.fields |= MNCC_F_CAUSE;
1032 gsm48_decode_cause(&rel.cause,
1033 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1034 }
1035 /* facility */
1036 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1037 rel.fields |= MNCC_F_FACILITY;
1038 gsm48_decode_facility(&rel.facility,
1039 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1040 }
1041 /* user-user */
1042 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1043 rel.fields |= MNCC_F_USERUSER;
1044 gsm48_decode_useruser(&rel.useruser,
1045 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1046 }
1047 /* ss-version */
1048 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1049 rel.fields |= MNCC_F_SSVERSION;
1050 gsm48_decode_ssversion(&rel.ssversion,
1051 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1052 }
1053
1054 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1055 /* release collision 5.4.5 */
1056 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1057 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001058 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001059 GSM48_PDISC_CC | (trans->transaction_id << 4),
1060 GSM48_MT_CC_RELEASE_COMPL);
1061 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1062 }
1063
1064 new_cc_state(trans, GSM_CSTATE_NULL);
1065
1066 trans->callref = 0;
1067 trans_free(trans);
1068
1069 return rc;
1070}
1071
1072static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1073{
1074 struct gsm_mncc *rel = arg;
1075 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1076 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1077
1078 gh->msg_type = GSM48_MT_CC_RELEASE;
1079
1080 gsm48_stop_cc_timer(trans);
1081 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1082
1083 /* cause */
1084 if (rel->fields & MNCC_F_CAUSE)
1085 gsm48_encode_cause(msg, 0, &rel->cause);
1086 /* facility */
1087 if (rel->fields & MNCC_F_FACILITY)
1088 gsm48_encode_facility(msg, 0, &rel->facility);
1089 /* user-user */
1090 if (rel->fields & MNCC_F_USERUSER)
1091 gsm48_encode_useruser(msg, 0, &rel->useruser);
1092
1093 trans->cc.T308_second = 0;
1094 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1095
1096 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1097 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1098
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001099 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001100}
1101
1102static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1103{
1104 struct gsm48_hdr *gh = msgb_l3(msg);
1105 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1106 struct tlv_parsed tp;
1107 struct gsm_mncc rel;
1108 int rc = 0;
1109
1110 gsm48_stop_cc_timer(trans);
1111
1112 memset(&rel, 0, sizeof(struct gsm_mncc));
1113 rel.callref = trans->callref;
1114 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1115 /* cause */
1116 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1117 rel.fields |= MNCC_F_CAUSE;
1118 gsm48_decode_cause(&rel.cause,
1119 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1120 }
1121 /* facility */
1122 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1123 rel.fields |= MNCC_F_FACILITY;
1124 gsm48_decode_facility(&rel.facility,
1125 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1126 }
1127 /* user-user */
1128 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1129 rel.fields |= MNCC_F_USERUSER;
1130 gsm48_decode_useruser(&rel.useruser,
1131 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1132 }
1133 /* ss-version */
1134 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1135 rel.fields |= MNCC_F_SSVERSION;
1136 gsm48_decode_ssversion(&rel.ssversion,
1137 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1138 }
1139
1140 if (trans->callref) {
1141 switch (trans->cc.state) {
1142 case GSM_CSTATE_CALL_PRESENT:
1143 rc = mncc_recvmsg(trans->net, trans,
1144 MNCC_REJ_IND, &rel);
1145 break;
1146 case GSM_CSTATE_RELEASE_REQ:
1147 rc = mncc_recvmsg(trans->net, trans,
1148 MNCC_REL_CNF, &rel);
1149 break;
1150 default:
1151 rc = mncc_recvmsg(trans->net, trans,
1152 MNCC_REL_IND, &rel);
1153 }
1154 }
1155
1156 trans->callref = 0;
1157 trans_free(trans);
1158
1159 return rc;
1160}
1161
1162static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1163{
1164 struct gsm_mncc *rel = arg;
1165 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1166 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1167 int ret;
1168
1169 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1170
1171 trans->callref = 0;
1172
1173 gsm48_stop_cc_timer(trans);
1174
1175 /* cause */
1176 if (rel->fields & MNCC_F_CAUSE)
1177 gsm48_encode_cause(msg, 0, &rel->cause);
1178 /* facility */
1179 if (rel->fields & MNCC_F_FACILITY)
1180 gsm48_encode_facility(msg, 0, &rel->facility);
1181 /* user-user */
1182 if (rel->fields & MNCC_F_USERUSER)
1183 gsm48_encode_useruser(msg, 0, &rel->useruser);
1184
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001185 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001186
1187 trans_free(trans);
1188
1189 return ret;
1190}
1191
1192static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1193{
1194 struct gsm48_hdr *gh = msgb_l3(msg);
1195 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1196 struct tlv_parsed tp;
1197 struct gsm_mncc fac;
1198
1199 memset(&fac, 0, sizeof(struct gsm_mncc));
1200 fac.callref = trans->callref;
1201 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1202 /* facility */
1203 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1204 fac.fields |= MNCC_F_FACILITY;
1205 gsm48_decode_facility(&fac.facility,
1206 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1207 }
1208 /* ss-version */
1209 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1210 fac.fields |= MNCC_F_SSVERSION;
1211 gsm48_decode_ssversion(&fac.ssversion,
1212 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1213 }
1214
1215 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1216}
1217
1218static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1219{
1220 struct gsm_mncc *fac = arg;
1221 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1222 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1223
1224 gh->msg_type = GSM48_MT_CC_FACILITY;
1225
1226 /* facility */
1227 gsm48_encode_facility(msg, 1, &fac->facility);
1228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001229 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001230}
1231
1232static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1233{
1234 struct gsm_mncc hold;
1235
1236 memset(&hold, 0, sizeof(struct gsm_mncc));
1237 hold.callref = trans->callref;
1238 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1239}
1240
1241static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1242{
1243 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1244 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1245
1246 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1247
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001248 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001249}
1250
1251static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1252{
1253 struct gsm_mncc *hold_rej = arg;
1254 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1255 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1256
1257 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1258
1259 /* cause */
1260 if (hold_rej->fields & MNCC_F_CAUSE)
1261 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1262 else
1263 gsm48_encode_cause(msg, 1, &default_cause);
1264
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001265 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001266}
1267
1268static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1269{
1270 struct gsm_mncc retrieve;
1271
1272 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1273 retrieve.callref = trans->callref;
1274 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1275 &retrieve);
1276}
1277
1278static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1279{
1280 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1281 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1282
1283 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1284
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001285 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001286}
1287
1288static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1289{
1290 struct gsm_mncc *retrieve_rej = arg;
1291 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1292 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1293
1294 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1295
1296 /* cause */
1297 if (retrieve_rej->fields & MNCC_F_CAUSE)
1298 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1299 else
1300 gsm48_encode_cause(msg, 1, &default_cause);
1301
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001302 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001303}
1304
1305static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1306{
1307 struct gsm48_hdr *gh = msgb_l3(msg);
1308 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1309 struct tlv_parsed tp;
1310 struct gsm_mncc dtmf;
1311
1312 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1313 dtmf.callref = trans->callref;
1314 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1315 /* keypad facility */
1316 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1317 dtmf.fields |= MNCC_F_KEYPAD;
1318 gsm48_decode_keypad(&dtmf.keypad,
1319 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1320 }
1321
1322 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1323}
1324
1325static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1326{
1327 struct gsm_mncc *dtmf = arg;
1328 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1329 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1330
1331 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1332
1333 /* keypad */
1334 if (dtmf->fields & MNCC_F_KEYPAD)
1335 gsm48_encode_keypad(msg, dtmf->keypad);
1336
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001337 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001338}
1339
1340static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1341{
1342 struct gsm_mncc *dtmf = arg;
1343 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1344 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1345
1346 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1347
1348 /* cause */
1349 if (dtmf->fields & MNCC_F_CAUSE)
1350 gsm48_encode_cause(msg, 1, &dtmf->cause);
1351 else
1352 gsm48_encode_cause(msg, 1, &default_cause);
1353
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001354 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001355}
1356
1357static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1358{
1359 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1360 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1361
1362 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1363
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001364 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001365}
1366
1367static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1368{
1369 struct gsm_mncc dtmf;
1370
1371 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1372 dtmf.callref = trans->callref;
1373
1374 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1375}
1376
1377static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1378{
1379 struct gsm48_hdr *gh = msgb_l3(msg);
1380 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1381 struct tlv_parsed tp;
1382 struct gsm_mncc modify;
1383
1384 memset(&modify, 0, sizeof(struct gsm_mncc));
1385 modify.callref = trans->callref;
1386 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1387 /* bearer capability */
1388 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1389 modify.fields |= MNCC_F_BEARER_CAP;
1390 gsm48_decode_bearer_cap(&modify.bearer_cap,
1391 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1392
1393 /* Create a copy of the bearer capability
1394 * in the transaction struct, so we can use
1395 * this information later */
1396 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1397 sizeof(trans->bearer_cap));
1398 }
1399
1400 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1401
1402 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1403}
1404
1405static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1406{
1407 struct gsm_mncc *modify = arg;
1408 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1409 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1410
1411 gh->msg_type = GSM48_MT_CC_MODIFY;
1412
1413 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1414
1415 /* bearer capability */
1416 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1417 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1418
1419 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1420
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001421 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001422}
1423
1424static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1425{
1426 struct gsm48_hdr *gh = msgb_l3(msg);
1427 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1428 struct tlv_parsed tp;
1429 struct gsm_mncc modify;
1430
1431 gsm48_stop_cc_timer(trans);
1432
1433 memset(&modify, 0, sizeof(struct gsm_mncc));
1434 modify.callref = trans->callref;
1435 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1436 /* bearer capability */
1437 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1438 modify.fields |= MNCC_F_BEARER_CAP;
1439 gsm48_decode_bearer_cap(&modify.bearer_cap,
1440 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1441
1442 /* Create a copy of the bearer capability
1443 * in the transaction struct, so we can use
1444 * this information later */
1445 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1446 sizeof(trans->bearer_cap));
1447 }
1448
1449 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1450
1451 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1452}
1453
1454static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1455{
1456 struct gsm_mncc *modify = arg;
1457 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1458 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1459
1460 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1461
1462 /* bearer capability */
1463 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1464 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1465
1466 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1467
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001468 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001469}
1470
1471static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1472{
1473 struct gsm48_hdr *gh = msgb_l3(msg);
1474 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1475 struct tlv_parsed tp;
1476 struct gsm_mncc modify;
1477
1478 gsm48_stop_cc_timer(trans);
1479
1480 memset(&modify, 0, sizeof(struct gsm_mncc));
1481 modify.callref = trans->callref;
1482 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1483 /* bearer capability */
1484 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1485 modify.fields |= GSM48_IE_BEARER_CAP;
1486 gsm48_decode_bearer_cap(&modify.bearer_cap,
1487 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1488
1489 /* Create a copy of the bearer capability
1490 * in the transaction struct, so we can use
1491 * this information later */
1492 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1493 sizeof(trans->bearer_cap));
1494 }
1495 /* cause */
1496 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1497 modify.fields |= MNCC_F_CAUSE;
1498 gsm48_decode_cause(&modify.cause,
1499 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1500 }
1501
1502 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1503
1504 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1505}
1506
1507static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1508{
1509 struct gsm_mncc *modify = arg;
1510 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1511 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1512
1513 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1514
1515 /* bearer capability */
1516 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1517 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1518 /* cause */
1519 gsm48_encode_cause(msg, 1, &modify->cause);
1520
1521 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1522
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001523 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001524}
1525
1526static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1527{
1528 struct gsm_mncc *notify = arg;
1529 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1530 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1531
1532 gh->msg_type = GSM48_MT_CC_NOTIFY;
1533
1534 /* notify */
1535 gsm48_encode_notify(msg, notify->notify);
1536
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001537 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001538}
1539
1540static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1541{
1542 struct gsm48_hdr *gh = msgb_l3(msg);
1543 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1544// struct tlv_parsed tp;
1545 struct gsm_mncc notify;
1546
1547 memset(&notify, 0, sizeof(struct gsm_mncc));
1548 notify.callref = trans->callref;
1549// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1550 if (payload_len >= 1)
1551 gsm48_decode_notify(&notify.notify, gh->data);
1552
1553 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1554}
1555
1556static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1557{
1558 struct gsm_mncc *user = arg;
1559 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1560 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1561
1562 gh->msg_type = GSM48_MT_CC_USER_INFO;
1563
1564 /* user-user */
1565 if (user->fields & MNCC_F_USERUSER)
1566 gsm48_encode_useruser(msg, 1, &user->useruser);
1567 /* more data */
1568 if (user->more)
1569 gsm48_encode_more(msg);
1570
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001571 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001572}
1573
1574static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1575{
1576 struct gsm48_hdr *gh = msgb_l3(msg);
1577 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1578 struct tlv_parsed tp;
1579 struct gsm_mncc user;
1580
1581 memset(&user, 0, sizeof(struct gsm_mncc));
1582 user.callref = trans->callref;
1583 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1584 /* user-user */
1585 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1586 user.fields |= MNCC_F_USERUSER;
1587 gsm48_decode_useruser(&user.useruser,
1588 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1589 }
1590 /* more data */
1591 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1592 user.more = 1;
1593
1594 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1595}
1596
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001597static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1598 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1599 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001600{
1601 uint8_t data[sizeof(struct gsm_mncc)];
1602 struct gsm_mncc_rtp *rtp;
1603
1604 memset(&data, 0, sizeof(data));
1605 rtp = (struct gsm_mncc_rtp *) &data[0];
1606
1607 rtp->callref = callref;
1608 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001609 if (rtp_addr) {
1610 rtp->ip = osmo_htonl(inet_addr(rtp_addr->ip));
1611 rtp->port = rtp_addr->port;
1612 }
Harald Welte27989d42018-06-21 20:39:20 +02001613 rtp->payload_type = payload_type;
1614 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001615 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001616}
1617
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001618static 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 +02001619{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001620 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001621}
1622
1623static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1624{
1625 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001626
1627 /* Find callref */
1628 trans = trans_find_by_callref(net, callref);
1629 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001630 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001631 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001632 return -EIO;
1633 }
1634 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001635 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001636 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001637 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001638 return 0;
1639 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001640 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001641
Harald Welte27989d42018-06-21 20:39:20 +02001642 /* When we call msc_mgcp_call_assignment() we will trigger, depending
1643 * on the RAN type the call assignment on the A or Iu interface.
1644 * msc_mgcp_call_assignment() also takes care about sending the CRCX
1645 * command to the MGCP-GW. The CRCX will return the port number,
1646 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
1647 * have to return this port number back to the MNCC by sending
1648 * it back with the TCH_RTP_CREATE message. To make sure that
1649 * this message is sent AFTER the response to CRCX from the
1650 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
1651 * to take care of this by setting trans->tch_rtp_create to true.
1652 * This will make sure that gsm48_tch_rtp_create() (below) is
1653 * called as soon as the local port number has become known. */
1654 trans->tch_rtp_create = true;
1655
1656 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001657 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001658}
1659
1660/* Trigger TCH_RTP_CREATE acknowledgement */
1661int gsm48_tch_rtp_create(struct gsm_trans *trans)
1662{
1663 /* This function is called as soon as the port, on which the
1664 * mgcp-gw expects the incoming RTP stream from the remote
1665 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001666 struct msc_a *msc_a = trans->msc_a;
1667 struct gsm_network *net = msc_a_net(msc_a);
1668 struct call_leg *cl = msc_a->cc.call_leg;
1669 struct osmo_sockaddr_str *rtp_cn_local;
1670 /* FIXME: This has to be set to some meaningful value,
1671 * before the MSC-Split, this value was pulled from
1672 * lchan->abis_ip.rtp_payload */
1673 uint32_t payload_type = 0;
1674 int msg_type;
Harald Welte27989d42018-06-21 20:39:20 +02001675
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001676 /* FIXME This has to be set to some meaningful value.
1677 * Possible options are:
1678 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
1679 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
1680 * (0 if unknown) */
1681 msg_type = GSM_TCHF_FRAME;
Harald Welte27989d42018-06-21 20:39:20 +02001682
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001683 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1684 if (!rtp_cn_local) {
1685 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1686 return -EINVAL;
1687 }
1688
1689 return mncc_recv_rtp(net, trans, trans->callref, MNCC_RTP_CREATE, rtp_cn_local, payload_type, msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02001690}
1691
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001692static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001693{
1694 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001695 struct call_leg *cl;
1696 struct rtp_stream *rtps;
1697 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001698
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001699 /* FIXME: in *rtp we should get the codec information of the remote
1700 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1701 * meaningful value based on this information but unfortunately we
1702 * can't do that yet because the mncc API can not signal dynamic
1703 * payload types yet. This must be fixed first. Also there may be
1704 * additional members necessary in trans->conn->rtp because we
1705 * somehow need to deal with dynamic payload types that do not
1706 * comply to 3gpp's assumptions of payload type numbers on the A
1707 * interface. See also related tickets: OS#3399 and OS1683 */
1708
Harald Welte27989d42018-06-21 20:39:20 +02001709 /* Find callref */
1710 trans = trans_find_by_callref(net, rtp->callref);
1711 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001712 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001713 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001714 return -EIO;
1715 }
1716 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001717 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001718 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001719 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001720 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001721 }
1722
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001723 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CONNECT));
1724
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001725 cl = trans->msc_a->cc.call_leg;
1726 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1727
1728 if (!rtps) {
1729 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1730 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1731 return -EINVAL;
1732 }
1733
1734 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CONNECT));
1735
1736 osmo_sockaddr_str_from_32n(&rtp_addr, rtp->ip, rtp->port);
1737 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1738 rtp_stream_commit(rtps);
1739 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001740}
1741
1742static struct downstate {
1743 uint32_t states;
1744 int type;
1745 int (*rout) (struct gsm_trans *trans, void *arg);
1746} downstatelist[] = {
1747 /* mobile originating call establishment */
1748 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1749 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1750 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1751 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1752 {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 */
1753 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1754 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1755 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1756 /* mobile terminating call establishment */
1757 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1758 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1759 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1760 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1761 /* signalling during call */
1762 {SBIT(GSM_CSTATE_ACTIVE),
1763 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1764 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1765 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1766 {ALL_STATES,
1767 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1768 {ALL_STATES,
1769 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1770 {ALL_STATES,
1771 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1772 {SBIT(GSM_CSTATE_ACTIVE),
1773 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1774 {SBIT(GSM_CSTATE_ACTIVE),
1775 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1776 {SBIT(GSM_CSTATE_ACTIVE),
1777 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1778 {SBIT(GSM_CSTATE_ACTIVE),
1779 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1780 {SBIT(GSM_CSTATE_ACTIVE),
1781 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1782 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1783 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1784 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1785 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1786 {SBIT(GSM_CSTATE_ACTIVE),
1787 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1788 /* clearing */
1789 {SBIT(GSM_CSTATE_INITIATED),
1790 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1791 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1792 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1793 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1794 MNCC_REL_REQ, gsm48_cc_tx_release},
1795};
1796
1797#define DOWNSLLEN \
1798 (sizeof(downstatelist) / sizeof(struct downstate))
1799
1800
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001801int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001802{
1803 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001804 struct msc_a *msc_a = NULL;
1805 struct gsm_trans *trans = NULL;
1806 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001807
Harald Welte27989d42018-06-21 20:39:20 +02001808 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001809 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001810 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001811 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001812 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001813 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001814 return rc;
1815 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001816 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001817 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001818 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001819 case MNCC_RTP_FREE:
1820 /* unused right now */
1821 return -EIO;
1822
1823 case MNCC_FRAME_DROP:
1824 case MNCC_FRAME_RECV:
1825 case GSM_TCHF_FRAME:
1826 case GSM_TCHF_FRAME_EFR:
1827 case GSM_TCHH_FRAME:
1828 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001829 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001830 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001831 return -ENOTSUP;
1832 }
1833
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001834 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001835
1836 /* Find callref */
1837 trans = trans_find_by_callref(net, data->callref);
1838
1839 /* Callref unknown */
1840 if (!trans) {
1841 struct vlr_subscr *vsub;
1842
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001843 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001844 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001845 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001846 /* Invalid call reference */
1847 return mncc_release_ind(net, NULL, data->callref,
1848 GSM48_CAUSE_LOC_PRN_S_LU,
1849 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1850 }
1851 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001852 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001853 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001854 /* Invalid number */
1855 return mncc_release_ind(net, NULL, data->callref,
1856 GSM48_CAUSE_LOC_PRN_S_LU,
1857 GSM48_CC_CAUSE_INV_NR_FORMAT);
1858 }
1859 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001860 if (data->called.number[0]) {
1861 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1862 if (!vsub)
1863 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001864 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001865 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001866 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001867 if (!vsub)
1868 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001869 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001870 }
1871 if (!vsub)
1872 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
1873 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte27989d42018-06-21 20:39:20 +02001874 /* update the subscriber we deal with */
1875 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1876
Harald Welte27989d42018-06-21 20:39:20 +02001877 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001878 if (!vsub->lu_complete) {
1879 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001880 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001881 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001882 /* Temporarily out of order */
1883 return mncc_release_ind(net, NULL, data->callref,
1884 GSM48_CAUSE_LOC_PRN_S_LU,
1885 GSM48_CC_CAUSE_DEST_OOO);
1886 }
1887 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001888 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001889 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001890 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001891 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001892 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001893 /* Ressource unavailable */
1894 mncc_release_ind(net, NULL, data->callref,
1895 GSM48_CAUSE_LOC_PRN_S_LU,
1896 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1897 return -ENOMEM;
1898 }
1899
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001900 /* Find valid conn */
1901 msc_a = msc_a_for_vsub(vsub, true);
Harald Welte27989d42018-06-21 20:39:20 +02001902
1903 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001904 if (!msc_a) {
1905
1906 if (vsub->cs.is_paging) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001907 LOG_TRANS(trans, LOGL_DEBUG,
1908 "rx %s, subscriber not yet connected, paging already started\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001909 get_mncc_name(msg->msg_type));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001910 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001911 trans_free(trans);
1912 return 0;
1913 }
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001914
Harald Welte27989d42018-06-21 20:39:20 +02001915 /* store setup information until paging succeeds */
1916 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1917
1918 /* Request a channel */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001919 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1920 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001921 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001922 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001923 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001924 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001925 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001926 return 0;
1927 }
1928
1929 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001930 trans->msc_a = msc_a;
1931 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001932 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001933 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001934 } else {
1935 /* update the subscriber we deal with */
1936 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1937 }
1938
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001939 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001940
Philipp Maier9ca7b312018-10-10 17:00:49 +02001941 gsm48_start_guard_timer(trans);
1942
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001943 if (trans->msc_a)
1944 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02001945
1946 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001947 if (!msc_a) {
1948 struct gsm_mncc rel = {
1949 .callref = data->callref,
1950 };
1951 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001952 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
1953 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001954 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02001955 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
1956 else
1957 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1958 trans->callref = 0;
1959 trans_free(trans);
1960 return rc;
1961 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001962 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001963 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02001964 }
1965
1966 /* Find function for current state and message */
1967 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001968 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02001969 && ((1 << trans->cc.state) & downstatelist[i].states))
1970 break;
1971 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001972 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001973 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02001974 return 0;
1975 }
1976
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001977 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02001978
1979 return rc;
1980}
1981
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001982struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
1983{
1984 uint32_t callref;
1985
1986 switch (msg->msg_type) {
1987 case MNCC_BRIDGE:
1988 callref = msg->bridge.callref[0];
1989 break;
1990 case MNCC_RTP_CREATE:
1991 case MNCC_RTP_CONNECT:
1992 callref = msg->rtp.callref;
1993 break;
1994
1995 case MNCC_RTP_FREE:
1996 case MNCC_FRAME_DROP:
1997 case MNCC_FRAME_RECV:
1998 case GSM_TCHF_FRAME:
1999 case GSM_TCHF_FRAME_EFR:
2000 case GSM_TCHH_FRAME:
2001 case GSM_TCH_FRAME_AMR:
2002 return NULL;
2003
2004 default:
2005 callref = msg->signal.callref;
2006 break;
2007 }
2008
2009 return mncc_call_find_by_callref(callref);
2010}
2011
2012/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
Neels Hofmeyr52558742019-05-09 01:23:09 +02002013int mncc_tx_to_cc(struct gsm_network *net, void *arg)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002014{
2015 const union mncc_msg *msg = arg;
2016 struct mncc_call *mncc_call = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002017
2018 if (msg->msg_type == MNCC_SETUP_REQ) {
2019 /* Incoming call to forward for inter-MSC Handover? */
2020 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2021 if (mncc_call)
2022 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2023 "Incoming call matches pending inter-MSC Handover Number\n");
2024 }
2025 if (!mncc_call) {
2026 /* Find already active MNCC FSM for this callref.
2027 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2028 * MNCC<->GSM-CC call handling. */
2029 mncc_call = mncc_find_by_callref_from_msg(msg);
2030 }
2031 if (mncc_call) {
2032 mncc_call_rx(mncc_call, msg);
2033 return 0;
2034 }
2035
2036 /* None of the above? Then it must be a normal GSM CC call related message. */
2037 return mncc_tx_to_gsm_cc(net, msg);
2038}
Harald Welte27989d42018-06-21 20:39:20 +02002039
2040static struct datastate {
2041 uint32_t states;
2042 int type;
2043 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2044} datastatelist[] = {
2045 /* mobile originating call establishment */
2046 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2047 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2048 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2049 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2050 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2051 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2052 /* mobile terminating call establishment */
2053 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2054 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2055 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2056 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2057 {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 */
2058 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2059 /* signalling during call */
2060 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2061 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2062 {SBIT(GSM_CSTATE_ACTIVE),
2063 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2064 {ALL_STATES,
2065 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2066 {ALL_STATES,
2067 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2068 {ALL_STATES,
2069 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2070 {SBIT(GSM_CSTATE_ACTIVE),
2071 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2072 {SBIT(GSM_CSTATE_ACTIVE),
2073 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2074 {SBIT(GSM_CSTATE_ACTIVE),
2075 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2076 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2077 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2078 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2079 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2080 {SBIT(GSM_CSTATE_ACTIVE),
2081 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2082 /* clearing */
2083 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2084 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2085 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2086 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2087 {ALL_STATES, /* 5.4.3.4 */
2088 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2089};
2090
2091#define DATASLLEN \
2092 (sizeof(datastatelist) / sizeof(struct datastate))
2093
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002094int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002095{
2096 struct gsm48_hdr *gh = msgb_l3(msg);
2097 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2098 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2099 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002100 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2101 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002102 int i, rc = 0;
2103
2104 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002105 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002106 return -EINVAL;
2107 }
2108
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002109 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002110 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002111 return -EINVAL;
2112 }
2113
2114 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002115 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002116
Harald Welte27989d42018-06-21 20:39:20 +02002117 /* Create transaction */
2118 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002119 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002120 trans = trans_alloc(net, vsub,
2121 TRANS_CC,
2122 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002123 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002124 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002125 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002126 GSM48_PDISC_CC | (transaction_id << 4),
2127 GSM48_MT_CC_RELEASE_COMPL);
2128 return -ENOMEM;
2129 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002130 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2131 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2132 trans_free(trans);
2133 return -EINVAL;
2134 }
2135
Harald Welte27989d42018-06-21 20:39:20 +02002136 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002137 msc_a_get(msc_a, MSC_A_USE_CC);
2138 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002139 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002140
2141 /* An earlier CM Service Request for this CC message now has concluded */
2142 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2143 LOG_MSC_A(msc_a, LOGL_ERROR,
2144 "Creating new CC transaction without prior CM Service Request\n");
2145 else
2146 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002147 }
2148
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002149 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2150 gsm48_cc_state_name(trans->cc.state));
2151
Harald Welte27989d42018-06-21 20:39:20 +02002152 /* find function for current state and message */
2153 for (i = 0; i < DATASLLEN; i++)
2154 if ((msg_type == datastatelist[i].type)
2155 && ((1 << trans->cc.state) & datastatelist[i].states))
2156 break;
2157 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002158 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002159
2160 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2161 * transaction right away. */
2162 if (trans->cc.state == GSM_CSTATE_NULL) {
2163 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2164 " -- disarding new CC transaction right away\n");
2165 trans_free(trans);
2166 }
Harald Welte27989d42018-06-21 20:39:20 +02002167 return 0;
2168 }
2169
2170 assert(trans->vsub);
2171
2172 rc = datastatelist[i].rout(trans, msg);
2173
Harald Welte27989d42018-06-21 20:39:20 +02002174 return rc;
2175}