blob: aa9764968b2b511dc31243518705cdee16b01754 [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
33#include "bscconfig.h"
34
35#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>
Harald Welte27989d42018-06-21 20:39:20 +020058
59#include <osmocom/gsm/gsm48.h>
60#include <osmocom/gsm/gsm0480.h>
61#include <osmocom/gsm/gsm_utils.h>
62#include <osmocom/gsm/protocol/gsm_04_08.h>
63#include <osmocom/core/msgb.h>
64#include <osmocom/core/talloc.h>
65#include <osmocom/core/utils.h>
66#include <osmocom/core/byteswap.h>
67#include <osmocom/gsm/tlv.h>
68#include <osmocom/crypt/auth.h>
Harald Welte27989d42018-06-21 20:39:20 +020069
70#include <assert.h>
71
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010072static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
73static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
74static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
75
76static int trans_tx_gsm48(struct gsm_trans *trans, struct msgb *msg)
77{
78 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
79 gh->proto_discr = GSM48_PDISC_CC | (trans->transaction_id << 4);
80 OMSC_LINKID_CB(msg) = trans->dlci;
81
82 return msc_a_tx_dtap_to_i(trans->msc_a, msg);
83}
84
85uint32_t msc_cc_next_outgoing_callref() {
86 static uint32_t last_callref = 0x80000000;
87 last_callref++;
88 if (last_callref < 0x80000001)
89 last_callref = 0x80000001;
90 return last_callref;
91}
Harald Welte27989d42018-06-21 20:39:20 +020092
Philipp Maier9ca7b312018-10-10 17:00:49 +020093static void gsm48_cc_guard_timeout(void *arg)
94{
95 struct gsm_trans *trans = arg;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +010096 LOG_TRANS(trans, LOGL_DEBUG, "guard timeout expired\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +020097 trans_free(trans);
98 return;
99}
100
101static void gsm48_stop_guard_timer(struct gsm_trans *trans)
102{
103 if (osmo_timer_pending(&trans->cc.timer_guard)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100104 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending guard timer\n");
Philipp Maier9ca7b312018-10-10 17:00:49 +0200105 osmo_timer_del(&trans->cc.timer_guard);
106 }
107}
108
109static void gsm48_start_guard_timer(struct gsm_trans *trans)
110{
111 /* NOTE: The purpose of this timer is to prevent the cc state machine
112 * from hanging in cases where mncc, gsm48 or both become unresponsive
113 * for some reason. The timer is started initially with the setup from
114 * the gsm48 side and then re-started with every incoming mncc message.
115 * Once the mncc state reaches its active state the timer is stopped.
116 * So if the cc state machine does not show any activity for an
117 * extended amount of time during call setup or teardown the guard
118 * timer will time out and hard-clear the connection. */
119 if (osmo_timer_pending(&trans->cc.timer_guard))
120 gsm48_stop_guard_timer(trans);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100121 LOG_TRANS(trans, LOGL_DEBUG, "starting guard timer with %d seconds\n", trans->net->mncc_guard_timeout);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200122 osmo_timer_setup(&trans->cc.timer_guard, gsm48_cc_guard_timeout, trans);
123 osmo_timer_schedule(&trans->cc.timer_guard,
124 trans->net->mncc_guard_timeout, 0);
125}
Harald Welte27989d42018-06-21 20:39:20 +0200126
127/* Call Control */
128
129void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
130{
131 net->mncc_recv(net, msg);
132}
133
134int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
135{
136 struct gsm48_hdr *gh;
137 struct msgb *ss_notify;
138
139 ss_notify = gsm0480_create_notifySS(message);
140 if (!ss_notify)
141 return -1;
142
143 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
144 uint8_t *data = msgb_push(ss_notify, 1);
145 data[0] = ss_notify->len - 1;
146 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
147 gh->msg_type = GSM48_MT_CC_FACILITY;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100148 return trans_tx_gsm48(trans, ss_notify);
Harald Welte27989d42018-06-21 20:39:20 +0200149}
150
151/* FIXME: this count_statistics is a state machine behaviour. we should convert
152 * the complete call control into a state machine. Afterwards we can move this
153 * code into state transitions.
154 */
155static void count_statistics(struct gsm_trans *trans, int new_state)
156{
157 int old_state = trans->cc.state;
158 struct rate_ctr_group *msc = trans->net->msc_ctrs;
159
160 if (old_state == new_state)
161 return;
162
163 /* state incoming */
164 switch (new_state) {
165 case GSM_CSTATE_ACTIVE:
166 osmo_counter_inc(trans->net->active_calls);
167 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
168 break;
169 }
170
171 /* state outgoing */
172 switch (old_state) {
173 case GSM_CSTATE_ACTIVE:
174 osmo_counter_dec(trans->net->active_calls);
175 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
176 new_state == GSM_CSTATE_DISCONNECT_IND)
177 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
178 else
179 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
180 break;
181 }
182}
183
Harald Welte27989d42018-06-21 20:39:20 +0200184static void new_cc_state(struct gsm_trans *trans, int state)
185{
186 if (state > 31 || state < 0)
187 return;
188
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100189 LOG_TRANS(trans, LOGL_DEBUG, "new state %s -> %s\n",
190 gsm48_cc_state_name(trans->cc.state),
191 gsm48_cc_state_name(state));
Harald Welte27989d42018-06-21 20:39:20 +0200192
193 count_statistics(trans, state);
194 trans->cc.state = state;
Philipp Maier9ca7b312018-10-10 17:00:49 +0200195
196 /* Stop the guard timer when a call reaches the active state */
197 if (state == GSM_CSTATE_ACTIVE)
198 gsm48_stop_guard_timer(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200199}
200
201static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
202{
203 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
204 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
205 uint8_t *cause, *call_state;
206
207 gh->msg_type = GSM48_MT_CC_STATUS;
208
209 cause = msgb_put(msg, 3);
210 cause[0] = 2;
211 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
212 cause[2] = 0x80 | 30; /* response to status inquiry */
213
214 call_state = msgb_put(msg, 1);
215 call_state[0] = 0xc0 | 0x00;
216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100217 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200218}
219
220static void gsm48_stop_cc_timer(struct gsm_trans *trans)
221{
222 if (osmo_timer_pending(&trans->cc.timer)) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100223 LOG_TRANS(trans, LOGL_DEBUG, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Harald Welte27989d42018-06-21 20:39:20 +0200224 osmo_timer_del(&trans->cc.timer);
225 trans->cc.Tcurrent = 0;
226 }
227}
228
229static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
230 int msg_type, struct gsm_mncc *mncc)
231{
232 struct msgb *msg;
233 unsigned char *data;
234
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100235 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "tx %s\n", get_mncc_name(msg_type));
Harald Welte27989d42018-06-21 20:39:20 +0200236
237 mncc->msg_type = msg_type;
238
239 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
240 if (!msg)
241 return -ENOMEM;
242
243 data = msgb_put(msg, sizeof(struct gsm_mncc));
244 memcpy(data, mncc, sizeof(struct gsm_mncc));
245
246 cc_tx_to_mncc(net, msg);
247
248 return 0;
249}
250
251int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
252 uint32_t callref, int location, int value)
253{
254 struct gsm_mncc rel;
255
256 memset(&rel, 0, sizeof(rel));
257 rel.callref = callref;
258 mncc_set_cause(&rel, location, value);
259 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
260 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
261 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
262}
263
264/* Call Control Specific transaction release.
265 * gets called by trans_free, DO NOT CALL YOURSELF! */
266void _gsm48_cc_trans_free(struct gsm_trans *trans)
267{
268 gsm48_stop_cc_timer(trans);
269
Harald Welte27989d42018-06-21 20:39:20 +0200270 /* send release to L4, if callref still exists */
271 if (trans->callref) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100272 /* FIXME: currently, a CC trans that would not yet be in state GSM_CSTATE_RELEASE_REQ fails to send a
273 * CC Release to the MS if it gets freed here. Hack it to do so. */
274 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ) {
275 struct gsm_mncc rel = {};
276 rel.callref = trans->callref;
277 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU, GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
278 gsm48_cc_tx_release(trans, &rel);
279 }
Harald Welte27989d42018-06-21 20:39:20 +0200280 /* Ressource unavailable */
281 mncc_release_ind(trans->net, trans, trans->callref,
282 GSM48_CAUSE_LOC_PRN_S_LU,
283 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
284 /* This is a final freeing of the transaction. The MNCC release may have triggered the
285 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
286 gsm48_stop_cc_timer(trans);
287 }
288 if (trans->cc.state != GSM_CSTATE_NULL)
289 new_cc_state(trans, GSM_CSTATE_NULL);
Philipp Maier9ca7b312018-10-10 17:00:49 +0200290
291 gsm48_stop_guard_timer(trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100292
293 if (trans->msc_a && trans->msc_a->cc.active_trans == trans)
294 trans->msc_a->cc.active_trans = NULL;
Harald Welte27989d42018-06-21 20:39:20 +0200295}
296
Harald Welte27989d42018-06-21 20:39:20 +0200297/* call-back from paging the B-end of the connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100298static void cc_paging_cb(struct msc_a *msc_a, struct gsm_trans *trans)
Harald Welte27989d42018-06-21 20:39:20 +0200299{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100300 if (trans->msc_a) {
301 LOG_MSC_A_CAT(msc_a, DPAG, LOGL_ERROR,
302 "Handle paging error: transaction already associated with subscriber,"
303 " apparently it was already handled. Skip.\n");
304 return;
Harald Welte27989d42018-06-21 20:39:20 +0200305 }
306
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100307 if (msc_a) {
308 LOG_TRANS(trans, LOGL_DEBUG, "Paging succeeded\n");
309 /* Assign conn */
310 msc_a_get(msc_a, MSC_A_USE_CC);
311 trans->msc_a = msc_a;
312 trans->paging_request = NULL;
313 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans);
314 /* send SETUP request to called party */
315 gsm48_cc_tx_setup(trans, &trans->cc.msg);
316 } else {
317 LOG_TRANS(trans, LOGL_DEBUG, "Paging expired\n");
318 /* Temporarily out of order */
319 mncc_release_ind(trans->net, trans,
320 trans->callref,
321 GSM48_CAUSE_LOC_PRN_S_LU,
322 GSM48_CC_CAUSE_DEST_OOO);
323 trans->callref = 0;
324 trans->paging_request = NULL;
325 trans_free(trans);
326 }
Harald Welte27989d42018-06-21 20:39:20 +0200327}
328
329/* bridge channels of two transactions */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100330static int tch_bridge(struct gsm_network *net, const struct gsm_mncc_bridge *bridge)
Harald Welte27989d42018-06-21 20:39:20 +0200331{
332 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
333 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100334 struct call_leg *cl1;
335 struct call_leg *cl2;
Harald Welte27989d42018-06-21 20:39:20 +0200336
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100337 if (!trans1 || !trans2) {
338 LOG_TRANS(trans1 ? : trans2, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs are unset\n");
Harald Welte27989d42018-06-21 20:39:20 +0200339 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100340 }
Harald Welte27989d42018-06-21 20:39:20 +0200341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100342 if (!trans1->msc_a || !trans2->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100343 LOG_TRANS(trans1, LOGL_ERROR, "Cannot MNCC_BRIDGE, one or both call legs lack an active connection\n");
344 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 +0200345 return -EIO;
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100346 }
347
348 LOG_TRANS(trans1, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans2->callref);
349 LOG_TRANS(trans2, LOGL_DEBUG, "MNCC_BRIDGE: Local bridge to callref 0x%x\n", trans1->callref);
Harald Welte27989d42018-06-21 20:39:20 +0200350
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100351 /* This call bridging mechanism is only used with the internal MNCC (with external MNCC briding would be done by
352 * the PBX). For inter-MSC Handover scenarios, an external MNCC is mandatory. The conclusion is that in this
353 * code path, there is only one MSC, and the MSC-I role is local, and hence we can directly access the ran_conn.
354 * If we can't, then we must give up. */
355 cl1 = trans1->msc_a->cc.call_leg;
356 cl2 = trans2->msc_a->cc.call_leg;
Harald Welte27989d42018-06-21 20:39:20 +0200357
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100358 return call_leg_local_bridge(cl1, trans1->callref, trans1, cl2, trans2->callref, trans2);
Harald Welte27989d42018-06-21 20:39:20 +0200359}
360
361static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
362{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100363 LOG_TRANS(trans, LOGL_DEBUG, "-> STATUS ENQ\n");
Harald Welte27989d42018-06-21 20:39:20 +0200364 return gsm48_cc_tx_status(trans, msg);
365}
366
Harald Welte27989d42018-06-21 20:39:20 +0200367static void gsm48_cc_timeout(void *arg)
368{
369 struct gsm_trans *trans = arg;
370 int disconnect = 0, release = 0;
371 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
372 int mo_location = GSM48_CAUSE_LOC_USER;
373 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
374 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
375 struct gsm_mncc mo_rel, l4_rel;
376
377 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
378 mo_rel.callref = trans->callref;
379 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
380 l4_rel.callref = trans->callref;
381
382 switch(trans->cc.Tcurrent) {
383 case 0x303:
384 release = 1;
385 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
386 break;
387 case 0x310:
388 disconnect = 1;
389 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
390 break;
391 case 0x313:
392 disconnect = 1;
393 /* unknown, did not find it in the specs */
394 break;
395 case 0x301:
396 disconnect = 1;
397 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
398 break;
399 case 0x308:
400 if (!trans->cc.T308_second) {
401 /* restart T308 a second time */
402 gsm48_cc_tx_release(trans, &trans->cc.msg);
403 trans->cc.T308_second = 1;
404 break; /* stay in release state */
405 }
406 trans_free(trans);
407 return;
408 case 0x306:
409 release = 1;
410 mo_cause = trans->cc.msg.cause.value;
411 mo_location = trans->cc.msg.cause.location;
412 break;
413 case 0x323:
414 disconnect = 1;
415 break;
416 default:
417 release = 1;
418 }
419
420 if (release && trans->callref) {
421 /* process release towards layer 4 */
422 mncc_release_ind(trans->net, trans, trans->callref,
423 l4_location, l4_cause);
424 trans->callref = 0;
425 }
426
427 if (disconnect && trans->callref) {
428 /* process disconnect towards layer 4 */
429 mncc_set_cause(&l4_rel, l4_location, l4_cause);
430 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
431 }
432
433 /* process disconnect towards mobile station */
434 if (disconnect || release) {
435 mncc_set_cause(&mo_rel, mo_location, mo_cause);
436 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
437 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
438 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
439 mo_rel.cause.diag_len = 3;
440
441 if (disconnect)
442 gsm48_cc_tx_disconnect(trans, &mo_rel);
443 if (release)
444 gsm48_cc_tx_release(trans, &mo_rel);
445 }
446
447}
448
449/* disconnect both calls from the bridge */
450static inline void disconnect_bridge(struct gsm_network *net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100451 const struct gsm_mncc_bridge *bridge, int err)
Harald Welte27989d42018-06-21 20:39:20 +0200452{
453 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
454 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
455 struct gsm_mncc mx_rel;
456 if (!trans0 || !trans1)
457 return;
458
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100459 LOG_TRANS(trans0, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
460 trans0->callref, trans1->callref, strerror(err));
461 LOG_TRANS(trans1, LOGL_ERROR, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
Harald Welte27989d42018-06-21 20:39:20 +0200462 trans0->callref, trans1->callref, strerror(err));
463
464 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
465 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
466 GSM48_CC_CAUSE_CHAN_UNACCEPT);
467
468 mx_rel.callref = trans0->callref;
469 gsm48_cc_tx_disconnect(trans0, &mx_rel);
470
471 mx_rel.callref = trans1->callref;
472 gsm48_cc_tx_disconnect(trans1, &mx_rel);
473}
474
475static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
476 int sec, int micro)
477{
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100478 LOG_TRANS(trans, LOGL_DEBUG, "starting timer T%x with %d seconds\n", current, sec);
Harald Welte27989d42018-06-21 20:39:20 +0200479 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
480 osmo_timer_schedule(&trans->cc.timer, sec, micro);
481 trans->cc.Tcurrent = current;
482}
483
484static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
485{
486 struct gsm48_hdr *gh = msgb_l3(msg);
487 uint8_t msg_type = gsm48_hdr_msg_type(gh);
488 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
489 struct tlv_parsed tp;
490 struct gsm_mncc setup;
491
Philipp Maier9ca7b312018-10-10 17:00:49 +0200492 gsm48_start_guard_timer(trans);
493
Harald Welte27989d42018-06-21 20:39:20 +0200494 memset(&setup, 0, sizeof(struct gsm_mncc));
495 setup.callref = trans->callref;
496
497 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
498 /* emergency setup is identified by msg_type */
499 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
500 setup.fields |= MNCC_F_EMERGENCY;
501 setup.emergency = 1;
502 /* use destination number as configured by user (if any) */
503 if (trans->net->emergency.route_to_msisdn) {
504 setup.fields |= MNCC_F_CALLED;
505 setup.called.type = 0; /* unknown */
506 setup.called.plan = 0; /* unknown */
507 OSMO_STRLCPY_ARRAY(setup.called.number,
508 trans->net->emergency.route_to_msisdn);
509 }
510 }
511
512 /* use subscriber as calling party number */
513 setup.fields |= MNCC_F_CALLING;
514 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
515 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
516
517 /* bearer capability */
518 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
519 setup.fields |= MNCC_F_BEARER_CAP;
520 gsm48_decode_bearer_cap(&setup.bearer_cap,
521 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
522
523 /* Create a copy of the bearer capability
524 * in the transaction struct, so we can use
525 * this information later */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100526 memcpy(&trans->bearer_cap, &setup.bearer_cap,
Harald Welte27989d42018-06-21 20:39:20 +0200527 sizeof(trans->bearer_cap));
528 }
529 /* facility */
530 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
531 setup.fields |= MNCC_F_FACILITY;
532 gsm48_decode_facility(&setup.facility,
533 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
534 }
535 /* called party bcd number */
536 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
537 setup.fields |= MNCC_F_CALLED;
538 gsm48_decode_called(&setup.called,
539 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
540 }
541 /* user-user */
542 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
543 setup.fields |= MNCC_F_USERUSER;
544 gsm48_decode_useruser(&setup.useruser,
545 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
546 }
547 /* ss-version */
548 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
549 setup.fields |= MNCC_F_SSVERSION;
550 gsm48_decode_ssversion(&setup.ssversion,
551 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
552 }
553 /* CLIR suppression */
554 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
555 setup.clir.sup = 1;
556 /* CLIR invocation */
557 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
558 setup.clir.inv = 1;
559 /* cc cap */
560 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
561 setup.fields |= MNCC_F_CCCAP;
562 gsm48_decode_cccap(&setup.cccap,
563 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
564 }
565
566 new_cc_state(trans, GSM_CSTATE_INITIATED);
567
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100568 LOG_TRANS(trans, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "%sSETUP to %s\n",
569 setup.emergency ? "EMERGENCY_" : "", setup.called.number);
Harald Welte27989d42018-06-21 20:39:20 +0200570
571 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
572
573 /* indicate setup to MNCC */
574 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
575
576 /* MNCC code will modify the channel asynchronously, we should
577 * ipaccess-bind only after the modification has been made to the
578 * lchan->tch_mode */
579 return 0;
580}
581
582static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
583{
584 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
585 struct gsm48_hdr *gh;
586 struct gsm_mncc *setup = arg;
587 int rc, trans_id;
588
589 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
590
591 /* transaction id must not be assigned */
Maxd8daaae2019-02-14 16:54:10 +0700592 if (trans->transaction_id != TRANS_ID_UNASSIGNED) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +0100593 LOG_TRANS(trans, LOGL_DEBUG, "TX Setup with assigned transaction. "
Harald Welte27989d42018-06-21 20:39:20 +0200594 "This is not allowed!\n");
595 /* Temporarily out of order */
596 rc = mncc_release_ind(trans->net, trans, trans->callref,
597 GSM48_CAUSE_LOC_PRN_S_LU,
598 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
599 trans->callref = 0;
600 trans_free(trans);
601 return rc;
602 }
603
604 /* Get free transaction_id */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100605 trans_id = trans_assign_trans_id(trans->net, trans->vsub, TRANS_CC);
Harald Welte27989d42018-06-21 20:39:20 +0200606 if (trans_id < 0) {
607 /* no free transaction ID */
608 rc = mncc_release_ind(trans->net, trans, trans->callref,
609 GSM48_CAUSE_LOC_PRN_S_LU,
610 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
611 trans->callref = 0;
612 trans_free(trans);
613 return rc;
614 }
615 trans->transaction_id = trans_id;
616
617 gh->msg_type = GSM48_MT_CC_SETUP;
618
619 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
620
621 /* bearer capability */
622 if (setup->fields & MNCC_F_BEARER_CAP) {
623 /* Create a copy of the bearer capability in the transaction struct, so we
624 * can use this information later */
625 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
626 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
627 }
628 /* facility */
629 if (setup->fields & MNCC_F_FACILITY)
630 gsm48_encode_facility(msg, 0, &setup->facility);
631 /* progress */
632 if (setup->fields & MNCC_F_PROGRESS)
633 gsm48_encode_progress(msg, 0, &setup->progress);
634 /* calling party BCD number */
635 if (setup->fields & MNCC_F_CALLING)
636 gsm48_encode_calling(msg, &setup->calling);
637 /* called party BCD number */
638 if (setup->fields & MNCC_F_CALLED)
639 gsm48_encode_called(msg, &setup->called);
640 /* user-user */
641 if (setup->fields & MNCC_F_USERUSER)
642 gsm48_encode_useruser(msg, 0, &setup->useruser);
643 /* redirecting party BCD number */
644 if (setup->fields & MNCC_F_REDIRECTING)
645 gsm48_encode_redirecting(msg, &setup->redirecting);
646 /* signal */
647 if (setup->fields & MNCC_F_SIGNAL)
648 gsm48_encode_signal(msg, setup->signal);
649
650 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
651
652 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
653
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100654 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200655}
656
657static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
658{
659 struct gsm48_hdr *gh = msgb_l3(msg);
660 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
661 struct tlv_parsed tp;
662 struct gsm_mncc call_conf;
663 int rc;
664
665 gsm48_stop_cc_timer(trans);
666 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
667
668 memset(&call_conf, 0, sizeof(struct gsm_mncc));
669 call_conf.callref = trans->callref;
670
671 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
672#if 0
673 /* repeat */
674 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
675 call_conf.repeat = 1;
676 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
677 call_conf.repeat = 2;
678#endif
679 /* bearer capability */
680 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
681 call_conf.fields |= MNCC_F_BEARER_CAP;
682 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
683 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
684
685 /* Create a copy of the bearer capability
686 * in the transaction struct, so we can use
687 * this information later */
688 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
689 sizeof(trans->bearer_cap));
690 }
691 /* cause */
692 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
693 call_conf.fields |= MNCC_F_CAUSE;
694 gsm48_decode_cause(&call_conf.cause,
695 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
696 }
697 /* cc cap */
698 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
699 call_conf.fields |= MNCC_F_CCCAP;
700 gsm48_decode_cccap(&call_conf.cccap,
701 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
702 }
703
704 /* IMSI of called subscriber */
705 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
706
707 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
708
709 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100710 rc = msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200711
712 /* don't continue, if there were problems with
713 * the call assignment. */
714 if (rc)
715 return rc;
716
717 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
718 &call_conf);
719}
720
721static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
722{
723 struct gsm_mncc *proceeding = arg;
724 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
725 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
726 int rc;
727
728 gh->msg_type = GSM48_MT_CC_CALL_PROC;
729
730 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
731
732 /* bearer capability */
733 if (proceeding->fields & MNCC_F_BEARER_CAP) {
734 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
735 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
736 }
737 /* facility */
738 if (proceeding->fields & MNCC_F_FACILITY)
739 gsm48_encode_facility(msg, 0, &proceeding->facility);
740 /* progress */
741 if (proceeding->fields & MNCC_F_PROGRESS)
742 gsm48_encode_progress(msg, 0, &proceeding->progress);
743
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100744 rc = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200745 if (rc)
746 return rc;
747
748 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100749 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +0200750}
751
752static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
753{
754 struct gsm48_hdr *gh = msgb_l3(msg);
755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
756 struct tlv_parsed tp;
757 struct gsm_mncc alerting;
758
759 gsm48_stop_cc_timer(trans);
760 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
761
762 memset(&alerting, 0, sizeof(struct gsm_mncc));
763 alerting.callref = trans->callref;
764 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
765 /* facility */
766 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
767 alerting.fields |= MNCC_F_FACILITY;
768 gsm48_decode_facility(&alerting.facility,
769 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
770 }
771
772 /* progress */
773 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
774 alerting.fields |= MNCC_F_PROGRESS;
775 gsm48_decode_progress(&alerting.progress,
776 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
777 }
778 /* ss-version */
779 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
780 alerting.fields |= MNCC_F_SSVERSION;
781 gsm48_decode_ssversion(&alerting.ssversion,
782 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
783 }
784
785 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
786
787 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
788 &alerting);
789}
790
791static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
792{
793 struct gsm_mncc *alerting = arg;
794 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
795 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
796
797 gh->msg_type = GSM48_MT_CC_ALERTING;
798
799 /* facility */
800 if (alerting->fields & MNCC_F_FACILITY)
801 gsm48_encode_facility(msg, 0, &alerting->facility);
802 /* progress */
803 if (alerting->fields & MNCC_F_PROGRESS)
804 gsm48_encode_progress(msg, 0, &alerting->progress);
805 /* user-user */
806 if (alerting->fields & MNCC_F_USERUSER)
807 gsm48_encode_useruser(msg, 0, &alerting->useruser);
808
809 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
810
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100811 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200812}
813
814static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
815{
816 struct gsm_mncc *progress = arg;
817 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
818 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
819
820 gh->msg_type = GSM48_MT_CC_PROGRESS;
821
822 /* progress */
823 gsm48_encode_progress(msg, 1, &progress->progress);
824 /* user-user */
825 if (progress->fields & MNCC_F_USERUSER)
826 gsm48_encode_useruser(msg, 0, &progress->useruser);
827
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100828 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200829}
830
831static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
832{
833 struct gsm_mncc *connect = arg;
834 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
835 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
836
837 gh->msg_type = GSM48_MT_CC_CONNECT;
838
839 gsm48_stop_cc_timer(trans);
840 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
841
842 /* facility */
843 if (connect->fields & MNCC_F_FACILITY)
844 gsm48_encode_facility(msg, 0, &connect->facility);
845 /* progress */
846 if (connect->fields & MNCC_F_PROGRESS)
847 gsm48_encode_progress(msg, 0, &connect->progress);
848 /* connected number */
849 if (connect->fields & MNCC_F_CONNECTED)
850 gsm48_encode_connected(msg, &connect->connected);
851 /* user-user */
852 if (connect->fields & MNCC_F_USERUSER)
853 gsm48_encode_useruser(msg, 0, &connect->useruser);
854
855 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
856
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100857 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200858}
859
860static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
861{
862 struct gsm48_hdr *gh = msgb_l3(msg);
863 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
864 struct tlv_parsed tp;
865 struct gsm_mncc connect;
866
867 gsm48_stop_cc_timer(trans);
868
869 memset(&connect, 0, sizeof(struct gsm_mncc));
870 connect.callref = trans->callref;
871 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
872 /* use subscriber as connected party number */
873 connect.fields |= MNCC_F_CONNECTED;
874 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
875 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
876
877 /* facility */
878 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
879 connect.fields |= MNCC_F_FACILITY;
880 gsm48_decode_facility(&connect.facility,
881 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
882 }
883 /* user-user */
884 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
885 connect.fields |= MNCC_F_USERUSER;
886 gsm48_decode_useruser(&connect.useruser,
887 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
888 }
889 /* ss-version */
890 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
891 connect.fields |= MNCC_F_SSVERSION;
892 gsm48_decode_ssversion(&connect.ssversion,
893 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
894 }
895
896 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
897 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
898
899 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
900}
901
902
903static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
904{
905 struct gsm_mncc connect_ack;
906
907 gsm48_stop_cc_timer(trans);
908
909 new_cc_state(trans, GSM_CSTATE_ACTIVE);
910 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
911
912 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
913 connect_ack.callref = trans->callref;
914
915 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
916 &connect_ack);
917}
918
919static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
920{
921 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
922 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
923
924 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
925
926 new_cc_state(trans, GSM_CSTATE_ACTIVE);
927
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100928 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +0200929}
930
931static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
932{
933 struct gsm48_hdr *gh = msgb_l3(msg);
934 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
935 struct tlv_parsed tp;
936 struct gsm_mncc disc;
937
938 gsm48_stop_cc_timer(trans);
939
940 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
941
942 memset(&disc, 0, sizeof(struct gsm_mncc));
943 disc.callref = trans->callref;
944 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
945 /* cause */
946 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
947 disc.fields |= MNCC_F_CAUSE;
948 gsm48_decode_cause(&disc.cause,
949 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
950 }
951 /* facility */
952 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
953 disc.fields |= MNCC_F_FACILITY;
954 gsm48_decode_facility(&disc.facility,
955 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
956 }
957 /* user-user */
958 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
959 disc.fields |= MNCC_F_USERUSER;
960 gsm48_decode_useruser(&disc.useruser,
961 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
962 }
963 /* ss-version */
964 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
965 disc.fields |= MNCC_F_SSVERSION;
966 gsm48_decode_ssversion(&disc.ssversion,
967 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
968 }
969
970 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte27989d42018-06-21 20:39:20 +0200971}
972
973static struct gsm_mncc_cause default_cause = {
974 .location = GSM48_CAUSE_LOC_PRN_S_LU,
975 .coding = 0,
976 .rec = 0,
977 .rec_val = 0,
978 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
979 .diag_len = 0,
980 .diag = { 0 },
981};
982
983static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
984{
985 struct gsm_mncc *disc = arg;
986 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
987 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
988
989 gh->msg_type = GSM48_MT_CC_DISCONNECT;
990
991 gsm48_stop_cc_timer(trans);
992 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
993
994 /* cause */
995 if (disc->fields & MNCC_F_CAUSE)
996 gsm48_encode_cause(msg, 1, &disc->cause);
997 else
998 gsm48_encode_cause(msg, 1, &default_cause);
999
1000 /* facility */
1001 if (disc->fields & MNCC_F_FACILITY)
1002 gsm48_encode_facility(msg, 0, &disc->facility);
1003 /* progress */
1004 if (disc->fields & MNCC_F_PROGRESS)
1005 gsm48_encode_progress(msg, 0, &disc->progress);
1006 /* user-user */
1007 if (disc->fields & MNCC_F_USERUSER)
1008 gsm48_encode_useruser(msg, 0, &disc->useruser);
1009
1010 /* store disconnect cause for T306 expiry */
1011 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
1012
1013 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
1014
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001015 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001016}
1017
1018static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
1019{
1020 struct gsm48_hdr *gh = msgb_l3(msg);
1021 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1022 struct tlv_parsed tp;
1023 struct gsm_mncc rel;
1024 int rc;
1025
1026 gsm48_stop_cc_timer(trans);
1027
1028 memset(&rel, 0, sizeof(struct gsm_mncc));
1029 rel.callref = trans->callref;
1030 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1031 /* cause */
1032 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1033 rel.fields |= MNCC_F_CAUSE;
1034 gsm48_decode_cause(&rel.cause,
1035 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1036 }
1037 /* facility */
1038 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1039 rel.fields |= MNCC_F_FACILITY;
1040 gsm48_decode_facility(&rel.facility,
1041 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1042 }
1043 /* user-user */
1044 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1045 rel.fields |= MNCC_F_USERUSER;
1046 gsm48_decode_useruser(&rel.useruser,
1047 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1048 }
1049 /* ss-version */
1050 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1051 rel.fields |= MNCC_F_SSVERSION;
1052 gsm48_decode_ssversion(&rel.ssversion,
1053 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1054 }
1055
1056 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
1057 /* release collision 5.4.5 */
1058 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
1059 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001060 rc = gsm48_tx_simple(trans->msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02001061 GSM48_PDISC_CC | (trans->transaction_id << 4),
1062 GSM48_MT_CC_RELEASE_COMPL);
1063 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
1064 }
1065
1066 new_cc_state(trans, GSM_CSTATE_NULL);
1067
1068 trans->callref = 0;
1069 trans_free(trans);
1070
1071 return rc;
1072}
1073
1074static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
1075{
1076 struct gsm_mncc *rel = arg;
1077 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
1078 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1079
1080 gh->msg_type = GSM48_MT_CC_RELEASE;
1081
1082 gsm48_stop_cc_timer(trans);
1083 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
1084
1085 /* cause */
1086 if (rel->fields & MNCC_F_CAUSE)
1087 gsm48_encode_cause(msg, 0, &rel->cause);
1088 /* facility */
1089 if (rel->fields & MNCC_F_FACILITY)
1090 gsm48_encode_facility(msg, 0, &rel->facility);
1091 /* user-user */
1092 if (rel->fields & MNCC_F_USERUSER)
1093 gsm48_encode_useruser(msg, 0, &rel->useruser);
1094
1095 trans->cc.T308_second = 0;
1096 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
1097
1098 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
1099 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
1100
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001101 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001102}
1103
1104static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
1105{
1106 struct gsm48_hdr *gh = msgb_l3(msg);
1107 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1108 struct tlv_parsed tp;
1109 struct gsm_mncc rel;
1110 int rc = 0;
1111
1112 gsm48_stop_cc_timer(trans);
1113
1114 memset(&rel, 0, sizeof(struct gsm_mncc));
1115 rel.callref = trans->callref;
1116 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1117 /* cause */
1118 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1119 rel.fields |= MNCC_F_CAUSE;
1120 gsm48_decode_cause(&rel.cause,
1121 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1122 }
1123 /* facility */
1124 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1125 rel.fields |= MNCC_F_FACILITY;
1126 gsm48_decode_facility(&rel.facility,
1127 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1128 }
1129 /* user-user */
1130 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1131 rel.fields |= MNCC_F_USERUSER;
1132 gsm48_decode_useruser(&rel.useruser,
1133 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1134 }
1135 /* ss-version */
1136 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1137 rel.fields |= MNCC_F_SSVERSION;
1138 gsm48_decode_ssversion(&rel.ssversion,
1139 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1140 }
1141
1142 if (trans->callref) {
1143 switch (trans->cc.state) {
1144 case GSM_CSTATE_CALL_PRESENT:
1145 rc = mncc_recvmsg(trans->net, trans,
1146 MNCC_REJ_IND, &rel);
1147 break;
1148 case GSM_CSTATE_RELEASE_REQ:
1149 rc = mncc_recvmsg(trans->net, trans,
1150 MNCC_REL_CNF, &rel);
1151 break;
1152 default:
1153 rc = mncc_recvmsg(trans->net, trans,
1154 MNCC_REL_IND, &rel);
1155 }
1156 }
1157
1158 trans->callref = 0;
1159 trans_free(trans);
1160
1161 return rc;
1162}
1163
1164static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
1165{
1166 struct gsm_mncc *rel = arg;
1167 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
1168 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1169 int ret;
1170
1171 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
1172
1173 trans->callref = 0;
1174
1175 gsm48_stop_cc_timer(trans);
1176
1177 /* cause */
1178 if (rel->fields & MNCC_F_CAUSE)
1179 gsm48_encode_cause(msg, 0, &rel->cause);
1180 /* facility */
1181 if (rel->fields & MNCC_F_FACILITY)
1182 gsm48_encode_facility(msg, 0, &rel->facility);
1183 /* user-user */
1184 if (rel->fields & MNCC_F_USERUSER)
1185 gsm48_encode_useruser(msg, 0, &rel->useruser);
1186
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001187 ret = trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001188
1189 trans_free(trans);
1190
1191 return ret;
1192}
1193
1194static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
1195{
1196 struct gsm48_hdr *gh = msgb_l3(msg);
1197 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1198 struct tlv_parsed tp;
1199 struct gsm_mncc fac;
1200
1201 memset(&fac, 0, sizeof(struct gsm_mncc));
1202 fac.callref = trans->callref;
1203 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
1204 /* facility */
1205 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1206 fac.fields |= MNCC_F_FACILITY;
1207 gsm48_decode_facility(&fac.facility,
1208 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1209 }
1210 /* ss-version */
1211 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1212 fac.fields |= MNCC_F_SSVERSION;
1213 gsm48_decode_ssversion(&fac.ssversion,
1214 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1215 }
1216
1217 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
1218}
1219
1220static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
1221{
1222 struct gsm_mncc *fac = arg;
1223 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
1224 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1225
1226 gh->msg_type = GSM48_MT_CC_FACILITY;
1227
1228 /* facility */
1229 gsm48_encode_facility(msg, 1, &fac->facility);
1230
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001231 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001232}
1233
1234static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
1235{
1236 struct gsm_mncc hold;
1237
1238 memset(&hold, 0, sizeof(struct gsm_mncc));
1239 hold.callref = trans->callref;
1240 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
1241}
1242
1243static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
1244{
1245 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
1246 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1247
1248 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
1249
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001250 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001251}
1252
1253static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
1254{
1255 struct gsm_mncc *hold_rej = arg;
1256 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
1257 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1258
1259 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
1260
1261 /* cause */
1262 if (hold_rej->fields & MNCC_F_CAUSE)
1263 gsm48_encode_cause(msg, 1, &hold_rej->cause);
1264 else
1265 gsm48_encode_cause(msg, 1, &default_cause);
1266
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001267 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001268}
1269
1270static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
1271{
1272 struct gsm_mncc retrieve;
1273
1274 memset(&retrieve, 0, sizeof(struct gsm_mncc));
1275 retrieve.callref = trans->callref;
1276 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
1277 &retrieve);
1278}
1279
1280static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
1281{
1282 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
1283 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1284
1285 gh->msg_type = GSM48_MT_CC_RETR_ACK;
1286
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001287 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001288}
1289
1290static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
1291{
1292 struct gsm_mncc *retrieve_rej = arg;
1293 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
1294 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1295
1296 gh->msg_type = GSM48_MT_CC_RETR_REJ;
1297
1298 /* cause */
1299 if (retrieve_rej->fields & MNCC_F_CAUSE)
1300 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
1301 else
1302 gsm48_encode_cause(msg, 1, &default_cause);
1303
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001304 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001305}
1306
1307static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
1308{
1309 struct gsm48_hdr *gh = msgb_l3(msg);
1310 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1311 struct tlv_parsed tp;
1312 struct gsm_mncc dtmf;
1313
1314 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1315 dtmf.callref = trans->callref;
1316 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
1317 /* keypad facility */
1318 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
1319 dtmf.fields |= MNCC_F_KEYPAD;
1320 gsm48_decode_keypad(&dtmf.keypad,
1321 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
1322 }
1323
1324 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
1325}
1326
1327static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
1328{
1329 struct gsm_mncc *dtmf = arg;
1330 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
1331 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1332
1333 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
1334
1335 /* keypad */
1336 if (dtmf->fields & MNCC_F_KEYPAD)
1337 gsm48_encode_keypad(msg, dtmf->keypad);
1338
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001339 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001340}
1341
1342static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
1343{
1344 struct gsm_mncc *dtmf = arg;
1345 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
1346 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1347
1348 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
1349
1350 /* cause */
1351 if (dtmf->fields & MNCC_F_CAUSE)
1352 gsm48_encode_cause(msg, 1, &dtmf->cause);
1353 else
1354 gsm48_encode_cause(msg, 1, &default_cause);
1355
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001356 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001357}
1358
1359static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
1360{
1361 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
1362 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1363
1364 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
1365
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001366 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001367}
1368
1369static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
1370{
1371 struct gsm_mncc dtmf;
1372
1373 memset(&dtmf, 0, sizeof(struct gsm_mncc));
1374 dtmf.callref = trans->callref;
1375
1376 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
1377}
1378
1379static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
1380{
1381 struct gsm48_hdr *gh = msgb_l3(msg);
1382 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1383 struct tlv_parsed tp;
1384 struct gsm_mncc modify;
1385
1386 memset(&modify, 0, sizeof(struct gsm_mncc));
1387 modify.callref = trans->callref;
1388 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1389 /* bearer capability */
1390 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1391 modify.fields |= MNCC_F_BEARER_CAP;
1392 gsm48_decode_bearer_cap(&modify.bearer_cap,
1393 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1394
1395 /* Create a copy of the bearer capability
1396 * in the transaction struct, so we can use
1397 * this information later */
1398 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1399 sizeof(trans->bearer_cap));
1400 }
1401
1402 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
1403
1404 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
1405}
1406
1407static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
1408{
1409 struct gsm_mncc *modify = arg;
1410 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
1411 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1412
1413 gh->msg_type = GSM48_MT_CC_MODIFY;
1414
1415 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
1416
1417 /* bearer capability */
1418 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1419 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1420
1421 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
1422
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001423 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001424}
1425
1426static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
1427{
1428 struct gsm48_hdr *gh = msgb_l3(msg);
1429 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1430 struct tlv_parsed tp;
1431 struct gsm_mncc modify;
1432
1433 gsm48_stop_cc_timer(trans);
1434
1435 memset(&modify, 0, sizeof(struct gsm_mncc));
1436 modify.callref = trans->callref;
1437 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
1438 /* bearer capability */
1439 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1440 modify.fields |= MNCC_F_BEARER_CAP;
1441 gsm48_decode_bearer_cap(&modify.bearer_cap,
1442 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1443
1444 /* Create a copy of the bearer capability
1445 * in the transaction struct, so we can use
1446 * this information later */
1447 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1448 sizeof(trans->bearer_cap));
1449 }
1450
1451 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1452
1453 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
1454}
1455
1456static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
1457{
1458 struct gsm_mncc *modify = arg;
1459 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
1460 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1461
1462 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
1463
1464 /* bearer capability */
1465 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1466 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1467
1468 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1469
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001470 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001471}
1472
1473static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
1474{
1475 struct gsm48_hdr *gh = msgb_l3(msg);
1476 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1477 struct tlv_parsed tp;
1478 struct gsm_mncc modify;
1479
1480 gsm48_stop_cc_timer(trans);
1481
1482 memset(&modify, 0, sizeof(struct gsm_mncc));
1483 modify.callref = trans->callref;
1484 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
1485 /* bearer capability */
1486 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1487 modify.fields |= GSM48_IE_BEARER_CAP;
1488 gsm48_decode_bearer_cap(&modify.bearer_cap,
1489 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1490
1491 /* Create a copy of the bearer capability
1492 * in the transaction struct, so we can use
1493 * this information later */
1494 memcpy(&trans->bearer_cap,&modify.bearer_cap,
1495 sizeof(trans->bearer_cap));
1496 }
1497 /* cause */
1498 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1499 modify.fields |= MNCC_F_CAUSE;
1500 gsm48_decode_cause(&modify.cause,
1501 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1502 }
1503
1504 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1505
1506 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
1507}
1508
1509static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
1510{
1511 struct gsm_mncc *modify = arg;
1512 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
1513 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1514
1515 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
1516
1517 /* bearer capability */
1518 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
1519 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
1520 /* cause */
1521 gsm48_encode_cause(msg, 1, &modify->cause);
1522
1523 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1524
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001525 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001526}
1527
1528static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
1529{
1530 struct gsm_mncc *notify = arg;
1531 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
1532 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1533
1534 gh->msg_type = GSM48_MT_CC_NOTIFY;
1535
1536 /* notify */
1537 gsm48_encode_notify(msg, notify->notify);
1538
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001539 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001540}
1541
1542static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
1543{
1544 struct gsm48_hdr *gh = msgb_l3(msg);
1545 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1546// struct tlv_parsed tp;
1547 struct gsm_mncc notify;
1548
1549 memset(&notify, 0, sizeof(struct gsm_mncc));
1550 notify.callref = trans->callref;
1551// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
1552 if (payload_len >= 1)
1553 gsm48_decode_notify(&notify.notify, gh->data);
1554
1555 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
1556}
1557
1558static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
1559{
1560 struct gsm_mncc *user = arg;
1561 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
1562 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1563
1564 gh->msg_type = GSM48_MT_CC_USER_INFO;
1565
1566 /* user-user */
1567 if (user->fields & MNCC_F_USERUSER)
1568 gsm48_encode_useruser(msg, 1, &user->useruser);
1569 /* more data */
1570 if (user->more)
1571 gsm48_encode_more(msg);
1572
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001573 return trans_tx_gsm48(trans, msg);
Harald Welte27989d42018-06-21 20:39:20 +02001574}
1575
1576static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
1577{
1578 struct gsm48_hdr *gh = msgb_l3(msg);
1579 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1580 struct tlv_parsed tp;
1581 struct gsm_mncc user;
1582
1583 memset(&user, 0, sizeof(struct gsm_mncc));
1584 user.callref = trans->callref;
1585 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
1586 /* user-user */
1587 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1588 user.fields |= MNCC_F_USERUSER;
1589 gsm48_decode_useruser(&user.useruser,
1590 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1591 }
1592 /* more data */
1593 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
1594 user.more = 1;
1595
1596 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
1597}
1598
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001599static int mncc_recv_rtp(struct gsm_network *net, struct gsm_trans *trans, uint32_t callref,
1600 int cmd, struct osmo_sockaddr_str *rtp_addr, uint32_t payload_type,
1601 uint32_t payload_msg_type)
Harald Welte27989d42018-06-21 20:39:20 +02001602{
1603 uint8_t data[sizeof(struct gsm_mncc)];
1604 struct gsm_mncc_rtp *rtp;
1605
1606 memset(&data, 0, sizeof(data));
1607 rtp = (struct gsm_mncc_rtp *) &data[0];
1608
1609 rtp->callref = callref;
1610 rtp->msg_type = cmd;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001611 if (rtp_addr) {
1612 rtp->ip = osmo_htonl(inet_addr(rtp_addr->ip));
1613 rtp->port = rtp_addr->port;
1614 }
Harald Welte27989d42018-06-21 20:39:20 +02001615 rtp->payload_type = payload_type;
1616 rtp->payload_msg_type = payload_msg_type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001617 return mncc_recvmsg(net, trans, cmd, (struct gsm_mncc *)data);
Harald Welte27989d42018-06-21 20:39:20 +02001618}
1619
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001620static 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 +02001621{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001622 mncc_recv_rtp(net, trans, callref, cmd, NULL, 0, 0);
Harald Welte27989d42018-06-21 20:39:20 +02001623}
1624
1625static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
1626{
1627 struct gsm_trans *trans;
Harald Welte27989d42018-06-21 20:39:20 +02001628
1629 /* Find callref */
1630 trans = trans_find_by_callref(net, callref);
1631 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001632 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001633 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001634 return -EIO;
1635 }
1636 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001637 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001638 LOG_TRANS_CAT(trans, DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001639 mncc_recv_rtp_err(net, trans, callref, MNCC_RTP_CREATE);
Harald Welte27989d42018-06-21 20:39:20 +02001640 return 0;
1641 }
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001642 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CREATE));
Harald Welte27989d42018-06-21 20:39:20 +02001643
Harald Welte27989d42018-06-21 20:39:20 +02001644 /* When we call msc_mgcp_call_assignment() we will trigger, depending
1645 * on the RAN type the call assignment on the A or Iu interface.
1646 * msc_mgcp_call_assignment() also takes care about sending the CRCX
1647 * command to the MGCP-GW. The CRCX will return the port number,
1648 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
1649 * have to return this port number back to the MNCC by sending
1650 * it back with the TCH_RTP_CREATE message. To make sure that
1651 * this message is sent AFTER the response to CRCX from the
1652 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
1653 * to take care of this by setting trans->tch_rtp_create to true.
1654 * This will make sure that gsm48_tch_rtp_create() (below) is
1655 * called as soon as the local port number has become known. */
1656 trans->tch_rtp_create = true;
1657
1658 /* Assign call (if not done yet) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001659 return msc_a_try_call_assignment(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001660}
1661
1662/* Trigger TCH_RTP_CREATE acknowledgement */
1663int gsm48_tch_rtp_create(struct gsm_trans *trans)
1664{
1665 /* This function is called as soon as the port, on which the
1666 * mgcp-gw expects the incoming RTP stream from the remote
1667 * end (e.g. Asterisk) is known. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001668 struct msc_a *msc_a = trans->msc_a;
1669 struct gsm_network *net = msc_a_net(msc_a);
1670 struct call_leg *cl = msc_a->cc.call_leg;
1671 struct osmo_sockaddr_str *rtp_cn_local;
1672 /* FIXME: This has to be set to some meaningful value,
1673 * before the MSC-Split, this value was pulled from
1674 * lchan->abis_ip.rtp_payload */
1675 uint32_t payload_type = 0;
1676 int msg_type;
Harald Welte27989d42018-06-21 20:39:20 +02001677
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001678 /* FIXME This has to be set to some meaningful value.
1679 * Possible options are:
1680 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
1681 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
1682 * (0 if unknown) */
1683 msg_type = GSM_TCHF_FRAME;
Harald Welte27989d42018-06-21 20:39:20 +02001684
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001685 rtp_cn_local = call_leg_local_ip(cl, RTP_TO_CN);
1686 if (!rtp_cn_local) {
1687 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "Cannot RTP CREATE to MNCC, no local RTP IP:port set up\n");
1688 return -EINVAL;
1689 }
1690
1691 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 +02001692}
1693
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001694static int tch_rtp_connect(struct gsm_network *net, const struct gsm_mncc_rtp *rtp)
Harald Welte27989d42018-06-21 20:39:20 +02001695{
1696 struct gsm_trans *trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001697 struct call_leg *cl;
1698 struct rtp_stream *rtps;
1699 struct osmo_sockaddr_str rtp_addr;
Harald Welte27989d42018-06-21 20:39:20 +02001700
Philipp Maier8ad3dac2018-08-07 13:00:14 +02001701 /* FIXME: in *rtp we should get the codec information of the remote
1702 * leg. We will have to populate trans->conn->rtp.codec_cn with a
1703 * meaningful value based on this information but unfortunately we
1704 * can't do that yet because the mncc API can not signal dynamic
1705 * payload types yet. This must be fixed first. Also there may be
1706 * additional members necessary in trans->conn->rtp because we
1707 * somehow need to deal with dynamic payload types that do not
1708 * comply to 3gpp's assumptions of payload type numbers on the A
1709 * interface. See also related tickets: OS#3399 and OS1683 */
1710
Harald Welte27989d42018-06-21 20:39:20 +02001711 /* Find callref */
1712 trans = trans_find_by_callref(net, rtp->callref);
1713 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001714 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001715 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Harald Welte27989d42018-06-21 20:39:20 +02001716 return -EIO;
1717 }
1718 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001719 if (!trans->msc_a) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001720 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001721 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001722 return -EIO;
Harald Welte27989d42018-06-21 20:39:20 +02001723 }
1724
Neels Hofmeyrc65cfe82019-04-08 03:48:56 +02001725 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CONNECT));
1726
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001727 cl = trans->msc_a->cc.call_leg;
1728 rtps = cl ? cl->rtp[RTP_TO_CN] : NULL;
1729
1730 if (!rtps) {
1731 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP connect for trans without ongoing call\n");
1732 mncc_recv_rtp_err(net, trans, rtp->callref, MNCC_RTP_CONNECT);
1733 return -EINVAL;
1734 }
1735
1736 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(MNCC_RTP_CONNECT));
1737
1738 osmo_sockaddr_str_from_32n(&rtp_addr, rtp->ip, rtp->port);
1739 rtp_stream_set_remote_addr(rtps, &rtp_addr);
1740 rtp_stream_commit(rtps);
1741 return 0;
Harald Welte27989d42018-06-21 20:39:20 +02001742}
1743
1744static struct downstate {
1745 uint32_t states;
1746 int type;
1747 int (*rout) (struct gsm_trans *trans, void *arg);
1748} downstatelist[] = {
1749 /* mobile originating call establishment */
1750 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
1751 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
1752 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
1753 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
1754 {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 */
1755 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
1756 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
1757 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
1758 /* mobile terminating call establishment */
1759 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
1760 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
1761 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
1762 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
1763 /* signalling during call */
1764 {SBIT(GSM_CSTATE_ACTIVE),
1765 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
1766 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
1767 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
1768 {ALL_STATES,
1769 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
1770 {ALL_STATES,
1771 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
1772 {ALL_STATES,
1773 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
1774 {SBIT(GSM_CSTATE_ACTIVE),
1775 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
1776 {SBIT(GSM_CSTATE_ACTIVE),
1777 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
1778 {SBIT(GSM_CSTATE_ACTIVE),
1779 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
1780 {SBIT(GSM_CSTATE_ACTIVE),
1781 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
1782 {SBIT(GSM_CSTATE_ACTIVE),
1783 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
1784 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1785 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
1786 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
1787 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
1788 {SBIT(GSM_CSTATE_ACTIVE),
1789 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
1790 /* clearing */
1791 {SBIT(GSM_CSTATE_INITIATED),
1792 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
1793 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
1794 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
1795 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
1796 MNCC_REL_REQ, gsm48_cc_tx_release},
1797};
1798
1799#define DOWNSLLEN \
1800 (sizeof(downstatelist) / sizeof(struct downstate))
1801
1802
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001803int mncc_tx_to_gsm_cc(struct gsm_network *net, const union mncc_msg *msg)
Harald Welte27989d42018-06-21 20:39:20 +02001804{
1805 int i, rc = 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001806 struct msc_a *msc_a = NULL;
1807 struct gsm_trans *trans = NULL;
1808 const struct gsm_mncc *data;
Harald Welte27989d42018-06-21 20:39:20 +02001809
Harald Welte27989d42018-06-21 20:39:20 +02001810 /* handle special messages */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001811 switch(msg->msg_type) {
Harald Welte27989d42018-06-21 20:39:20 +02001812 case MNCC_BRIDGE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001813 rc = tch_bridge(net, &msg->bridge);
Harald Welte27989d42018-06-21 20:39:20 +02001814 if (rc < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001815 disconnect_bridge(net, &msg->bridge, -rc);
Harald Welte27989d42018-06-21 20:39:20 +02001816 return rc;
1817 case MNCC_RTP_CREATE:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001818 return tch_rtp_create(net, msg->rtp.callref);
Harald Welte27989d42018-06-21 20:39:20 +02001819 case MNCC_RTP_CONNECT:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001820 return tch_rtp_connect(net, &msg->rtp);
Harald Welte27989d42018-06-21 20:39:20 +02001821 case MNCC_RTP_FREE:
1822 /* unused right now */
1823 return -EIO;
1824
1825 case MNCC_FRAME_DROP:
1826 case MNCC_FRAME_RECV:
1827 case GSM_TCHF_FRAME:
1828 case GSM_TCHF_FRAME_EFR:
1829 case GSM_TCHH_FRAME:
1830 case GSM_TCH_FRAME_AMR:
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001831 LOG_TRANS_CAT(trans, DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001832 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001833 return -ENOTSUP;
1834 }
1835
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001836 data = &msg->signal;
Harald Welte27989d42018-06-21 20:39:20 +02001837
1838 /* Find callref */
1839 trans = trans_find_by_callref(net, data->callref);
1840
1841 /* Callref unknown */
1842 if (!trans) {
1843 struct vlr_subscr *vsub;
1844
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001845 if (msg->msg_type != MNCC_SETUP_REQ) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001846 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Unknown call reference for %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001847 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001848 /* Invalid call reference */
1849 return mncc_release_ind(net, NULL, data->callref,
1850 GSM48_CAUSE_LOC_PRN_S_LU,
1851 GSM48_CC_CAUSE_INVAL_TRANS_ID);
1852 }
1853 if (!data->called.number[0] && !data->imsi[0]) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001854 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "Neither number nor IMSI in %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001855 get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001856 /* Invalid number */
1857 return mncc_release_ind(net, NULL, data->callref,
1858 GSM48_CAUSE_LOC_PRN_S_LU,
1859 GSM48_CC_CAUSE_INV_NR_FORMAT);
1860 }
1861 /* New transaction due to setup, find subscriber */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001862 if (data->called.number[0]) {
1863 vsub = vlr_subscr_find_by_msisdn(net->vlr, data->called.number, __func__);
1864 if (!vsub)
1865 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber number '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001866 get_mncc_name(msg->msg_type), data->called.number);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001867 } else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001868 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi, __func__);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001869 if (!vsub)
1870 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for unknown subscriber IMSI '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001871 get_mncc_name(msg->msg_type), data->imsi);
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001872 }
1873 if (!vsub)
1874 return mncc_release_ind(net, NULL, data->callref, GSM48_CAUSE_LOC_PRN_S_LU,
1875 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte27989d42018-06-21 20:39:20 +02001876 /* update the subscriber we deal with */
1877 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
1878
Harald Welte27989d42018-06-21 20:39:20 +02001879 /* If subscriber is not "attached" */
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001880 if (!vsub->lu_complete) {
1881 LOG_TRANS_CAT(trans, DCC, LOGL_ERROR, "rx %s for subscriber that is not attached: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001882 get_mncc_name(msg->msg_type), vlr_subscr_name(vsub));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001883 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001884 /* Temporarily out of order */
1885 return mncc_release_ind(net, NULL, data->callref,
1886 GSM48_CAUSE_LOC_PRN_S_LU,
1887 GSM48_CC_CAUSE_DEST_OOO);
1888 }
1889 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001890 trans = trans_alloc(net, vsub, TRANS_CC,
Maxd8daaae2019-02-14 16:54:10 +07001891 TRANS_ID_UNASSIGNED, data->callref);
Harald Welte27989d42018-06-21 20:39:20 +02001892 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001893 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001894 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001895 /* Ressource unavailable */
1896 mncc_release_ind(net, NULL, data->callref,
1897 GSM48_CAUSE_LOC_PRN_S_LU,
1898 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
1899 return -ENOMEM;
1900 }
1901
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902 /* Find valid conn */
1903 msc_a = msc_a_for_vsub(vsub, true);
Harald Welte27989d42018-06-21 20:39:20 +02001904
1905 /* If subscriber has no conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001906 if (!msc_a) {
1907
1908 if (vsub->cs.is_paging) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001909 LOG_TRANS(trans, LOGL_DEBUG,
1910 "rx %s, subscriber not yet connected, paging already started\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001911 get_mncc_name(msg->msg_type));
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001912 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001913 trans_free(trans);
1914 return 0;
1915 }
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001916
Harald Welte27989d42018-06-21 20:39:20 +02001917 /* store setup information until paging succeeds */
1918 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
1919
1920 /* Request a channel */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001921 trans->paging_request = paging_request_start(vsub, PAGING_CAUSE_CALL_CONVERSATIONAL,
1922 cc_paging_cb, trans, "MNCC: establish call");
Harald Welte27989d42018-06-21 20:39:20 +02001923 if (!trans->paging_request) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001924 LOG_TRANS(trans, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte27989d42018-06-21 20:39:20 +02001925 trans_free(trans);
Harald Welte27989d42018-06-21 20:39:20 +02001926 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001927 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001928 return 0;
1929 }
1930
1931 /* Assign conn */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001932 trans->msc_a = msc_a;
1933 msc_a_get(msc_a, MSC_A_USE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02001934 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001935 vlr_subscr_put(vsub, __func__);
Harald Welte27989d42018-06-21 20:39:20 +02001936 } else {
1937 /* update the subscriber we deal with */
1938 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
1939 }
1940
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001941 LOG_TRANS_CAT(trans, DMNCC, LOGL_DEBUG, "rx %s\n", get_mncc_name(msg->msg_type));
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001942
Philipp Maier9ca7b312018-10-10 17:00:49 +02001943 gsm48_start_guard_timer(trans);
1944
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001945 if (trans->msc_a)
1946 msc_a = trans->msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02001947
1948 /* if paging did not respond yet */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001949 if (!msc_a) {
1950 struct gsm_mncc rel = {
1951 .callref = data->callref,
1952 };
1953 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in paging state\n", get_mncc_name(msg->msg_type));
Harald Welte27989d42018-06-21 20:39:20 +02001954 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
1955 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001956 if (msg->msg_type == MNCC_REL_REQ)
Harald Welte27989d42018-06-21 20:39:20 +02001957 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
1958 else
1959 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1960 trans->callref = 0;
1961 trans_free(trans);
1962 return rc;
1963 } else {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001964 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001965 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02001966 }
1967
1968 /* Find function for current state and message */
1969 for (i = 0; i < DOWNSLLEN; i++)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001970 if ((msg->msg_type == downstatelist[i].type)
Harald Welte27989d42018-06-21 20:39:20 +02001971 && ((1 << trans->cc.state) & downstatelist[i].states))
1972 break;
1973 if (i == DOWNSLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01001974 LOG_TRANS(trans, LOGL_DEBUG, "Message '%s' unhandled at state '%s'\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001975 get_mncc_name(msg->msg_type), gsm48_cc_state_name(trans->cc.state));
Harald Welte27989d42018-06-21 20:39:20 +02001976 return 0;
1977 }
1978
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001979 rc = downstatelist[i].rout(trans, (void*)msg);
Harald Welte27989d42018-06-21 20:39:20 +02001980
1981 return rc;
1982}
1983
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001984struct mncc_call *mncc_find_by_callref_from_msg(const union mncc_msg *msg)
1985{
1986 uint32_t callref;
1987
1988 switch (msg->msg_type) {
1989 case MNCC_BRIDGE:
1990 callref = msg->bridge.callref[0];
1991 break;
1992 case MNCC_RTP_CREATE:
1993 case MNCC_RTP_CONNECT:
1994 callref = msg->rtp.callref;
1995 break;
1996
1997 case MNCC_RTP_FREE:
1998 case MNCC_FRAME_DROP:
1999 case MNCC_FRAME_RECV:
2000 case GSM_TCHF_FRAME:
2001 case GSM_TCHF_FRAME_EFR:
2002 case GSM_TCHH_FRAME:
2003 case GSM_TCH_FRAME_AMR:
2004 return NULL;
2005
2006 default:
2007 callref = msg->signal.callref;
2008 break;
2009 }
2010
2011 return mncc_call_find_by_callref(callref);
2012}
2013
2014/* Demux incoming genuine calls to GSM CC from MNCC forwarding for inter-MSC handover */
2015int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
2016{
2017 const union mncc_msg *msg = arg;
2018 struct mncc_call *mncc_call = NULL;
2019 OSMO_ASSERT(msg_type == msg->msg_type);
2020
2021 if (msg->msg_type == MNCC_SETUP_REQ) {
2022 /* Incoming call to forward for inter-MSC Handover? */
2023 mncc_call = msc_t_check_call_to_handover_number(&msg->signal);
2024 if (mncc_call)
2025 LOG_MNCC_CALL(mncc_call, LOGL_DEBUG,
2026 "Incoming call matches pending inter-MSC Handover Number\n");
2027 }
2028 if (!mncc_call) {
2029 /* Find already active MNCC FSM for this callref.
2030 * Currently only for inter-MSC call forwarding, but mncc_fsm could at some point also be used for direct
2031 * MNCC<->GSM-CC call handling. */
2032 mncc_call = mncc_find_by_callref_from_msg(msg);
2033 }
2034 if (mncc_call) {
2035 mncc_call_rx(mncc_call, msg);
2036 return 0;
2037 }
2038
2039 /* None of the above? Then it must be a normal GSM CC call related message. */
2040 return mncc_tx_to_gsm_cc(net, msg);
2041}
Harald Welte27989d42018-06-21 20:39:20 +02002042
2043static struct datastate {
2044 uint32_t states;
2045 int type;
2046 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2047} datastatelist[] = {
2048 /* mobile originating call establishment */
2049 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2050 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2051 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2052 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2053 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2054 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2055 /* mobile terminating call establishment */
2056 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2057 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2058 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2059 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
2060 {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 */
2061 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2062 /* signalling during call */
2063 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2064 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2065 {SBIT(GSM_CSTATE_ACTIVE),
2066 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2067 {ALL_STATES,
2068 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2069 {ALL_STATES,
2070 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2071 {ALL_STATES,
2072 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2073 {SBIT(GSM_CSTATE_ACTIVE),
2074 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2075 {SBIT(GSM_CSTATE_ACTIVE),
2076 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2077 {SBIT(GSM_CSTATE_ACTIVE),
2078 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2079 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2080 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2081 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2082 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
2083 {SBIT(GSM_CSTATE_ACTIVE),
2084 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
2085 /* clearing */
2086 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2087 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
2088 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
2089 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
2090 {ALL_STATES, /* 5.4.3.4 */
2091 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
2092};
2093
2094#define DATASLLEN \
2095 (sizeof(datastatelist) / sizeof(struct datastate))
2096
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002097int gsm0408_rcv_cc(struct msc_a *msc_a, struct msgb *msg)
Harald Welte27989d42018-06-21 20:39:20 +02002098{
2099 struct gsm48_hdr *gh = msgb_l3(msg);
2100 uint8_t msg_type = gsm48_hdr_msg_type(gh);
2101 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
2102 struct gsm_trans *trans = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002103 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
2104 struct gsm_network *net = msc_a_net(msc_a);
Harald Welte27989d42018-06-21 20:39:20 +02002105 int i, rc = 0;
2106
2107 if (msg_type & 0x80) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002108 LOG_TRANS(trans, LOGL_DEBUG, "MSG 0x%2x not defined for PD error\n", msg_type);
Harald Welte27989d42018-06-21 20:39:20 +02002109 return -EINVAL;
2110 }
2111
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002112 if (!vsub) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002113 LOG_TRANS(trans, LOGL_ERROR, "Invalid conn: no subscriber\n");
Harald Welte27989d42018-06-21 20:39:20 +02002114 return -EINVAL;
2115 }
2116
2117 /* Find transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002118 trans = trans_find_by_id(msc_a, TRANS_CC, transaction_id);
Harald Welte27989d42018-06-21 20:39:20 +02002119
Harald Welte27989d42018-06-21 20:39:20 +02002120 /* Create transaction */
2121 if (!trans) {
Harald Welte27989d42018-06-21 20:39:20 +02002122 /* Create transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002123 trans = trans_alloc(net, vsub,
2124 TRANS_CC,
2125 transaction_id, msc_cc_next_outgoing_callref());
Harald Welte27989d42018-06-21 20:39:20 +02002126 if (!trans) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002127 LOG_TRANS(trans, LOGL_ERROR, "No memory for trans.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002128 rc = gsm48_tx_simple(msc_a,
Harald Welte27989d42018-06-21 20:39:20 +02002129 GSM48_PDISC_CC | (transaction_id << 4),
2130 GSM48_MT_CC_RELEASE_COMPL);
2131 return -ENOMEM;
2132 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002133 if (osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, trans)) {
2134 LOG_MSC_A(msc_a, LOGL_ERROR, "Not allowed to accept CC transaction\n");
2135 trans_free(trans);
2136 return -EINVAL;
2137 }
2138
Harald Welte27989d42018-06-21 20:39:20 +02002139 /* Assign transaction */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002140 msc_a_get(msc_a, MSC_A_USE_CC);
2141 trans->msc_a = msc_a;
Harald Welte27989d42018-06-21 20:39:20 +02002142 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002143
2144 /* An earlier CM Service Request for this CC message now has concluded */
2145 if (!osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC))
2146 LOG_MSC_A(msc_a, LOGL_ERROR,
2147 "Creating new CC transaction without prior CM Service Request\n");
2148 else
2149 msc_a_put(msc_a, MSC_A_USE_CM_SERVICE_CC);
Harald Welte27989d42018-06-21 20:39:20 +02002150 }
2151
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002152 LOG_TRANS(trans, LOGL_DEBUG, "rx %s in state %s\n", gsm48_cc_msg_name(msg_type),
2153 gsm48_cc_state_name(trans->cc.state));
2154
Harald Welte27989d42018-06-21 20:39:20 +02002155 /* find function for current state and message */
2156 for (i = 0; i < DATASLLEN; i++)
2157 if ((msg_type == datastatelist[i].type)
2158 && ((1 << trans->cc.state) & datastatelist[i].states))
2159 break;
2160 if (i == DATASLLEN) {
Neels Hofmeyrff7074a2019-02-28 05:50:06 +01002161 LOG_TRANS(trans, LOGL_ERROR, "Message unhandled at this state.\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002162
2163 /* If a transaction was just now created, it was a bogus transaction ID, and we need to clean up the
2164 * transaction right away. */
2165 if (trans->cc.state == GSM_CSTATE_NULL) {
2166 LOG_TRANS(trans, LOGL_ERROR, "Unknown transaction ID for non-SETUP message is not allowed"
2167 " -- disarding new CC transaction right away\n");
2168 trans_free(trans);
2169 }
Harald Welte27989d42018-06-21 20:39:20 +02002170 return 0;
2171 }
2172
2173 assert(trans->vsub);
2174
2175 rc = datastatelist[i].rout(trans, msg);
2176
Harald Welte27989d42018-06-21 20:39:20 +02002177 return rc;
2178}