blob: fbdc5b4b5c65d37890824bb3f9fd22b7f8a304c0 [file] [log] [blame]
Harald Welte3e6376d2010-12-22 23:54:51 +01001/* mncc_builtin.c - default, minimal built-in MNCC Application for
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002 * standalone bsc_hack (network-in-the-box mode) */
Harald Welte3e6376d2010-12-22 23:54:51 +01003
4/* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2009 by Andreas Eversberg <Andreas.Eversberg@versatel.de>
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 General Public License as published by
10 * the Free Software Foundation; either version 2 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 General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Welte3e6376d2010-12-22 23:54:51 +010029
Neels Hofmeyr90843962017-09-04 15:04:35 +020030#include <osmocom/msc/gsm_04_08.h>
31#include <osmocom/msc/debug.h>
32#include <osmocom/msc/mncc.h>
33#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020035#include <osmocom/msc/gsm_data.h>
36#include <osmocom/msc/transaction.h>
Harald Welte3e6376d2010-12-22 23:54:51 +010037
Max3332ac42018-12-17 11:17:33 +010038#define DEBUGCC(l, r, fmt, args...) DEBUGP(DMNCC, "(call %x, remote %x) " fmt, l->callref, r->callref, ##args)
39
Harald Welte3e6376d2010-12-22 23:54:51 +010040void *tall_call_ctx;
41
42static LLIST_HEAD(call_list);
43
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020044static uint32_t new_callref = 0x00000001;
Harald Welte3e6376d2010-12-22 23:54:51 +010045
Harald Welteab386e62011-09-01 18:18:43 +020046struct mncc_int mncc_int = {
Andreas Eversberga83d5112013-12-07 18:32:28 +010047 .def_codec = { GSM48_CMODE_SPEECH_V1, GSM48_CMODE_SPEECH_V1 },
Harald Welteab386e62011-09-01 18:18:43 +020048};
49
Harald Welte3e6376d2010-12-22 23:54:51 +010050static void free_call(struct gsm_call *call)
51{
52 llist_del(&call->entry);
53 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
54 talloc_free(call);
55}
56
57
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020058static struct gsm_call *get_call_ref(uint32_t callref)
Harald Welte3e6376d2010-12-22 23:54:51 +010059{
60 struct gsm_call *callt;
61
62 llist_for_each_entry(callt, &call_list, entry) {
63 if (callt->callref == callref)
64 return callt;
65 }
66 return NULL;
67}
68
Harald Welte3e6376d2010-12-22 23:54:51 +010069/* on incoming call, look up database and send setup to remote subscr. */
70static int mncc_setup_ind(struct gsm_call *call, int msg_type,
71 struct gsm_mncc *setup)
72{
73 struct gsm_mncc mncc;
74 struct gsm_call *remote;
75
76 memset(&mncc, 0, sizeof(struct gsm_mncc));
77 mncc.callref = call->callref;
78
79 /* already have remote call */
80 if (call->remote_ref)
81 return 0;
82
83 /* transfer mode 1 would be packet mode, which was never specified */
84 if (setup->bearer_cap.mode != 0) {
85 LOGP(DMNCC, LOGL_NOTICE, "(call %x) We don't support "
86 "packet mode\n", call->callref);
87 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
88 GSM48_CC_CAUSE_BEARER_CA_UNAVAIL);
89 goto out_reject;
90 }
91
92 /* we currently only do speech */
93 if (setup->bearer_cap.transfer != GSM_MNCC_BCAP_SPEECH) {
94 LOGP(DMNCC, LOGL_NOTICE, "(call %x) We only support "
95 "voice calls\n", call->callref);
96 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
97 GSM48_CC_CAUSE_BEARER_CA_UNAVAIL);
98 goto out_reject;
99 }
100
101 /* create remote call */
Andreas Eversbergc5e08512013-01-25 08:36:32 +0100102 if (!(remote = talloc_zero(tall_call_ctx, struct gsm_call))) {
Harald Welte3e6376d2010-12-22 23:54:51 +0100103 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
104 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
105 goto out_reject;
106 }
107 llist_add_tail(&remote->entry, &call_list);
108 remote->net = call->net;
109 remote->callref = new_callref++;
Max3332ac42018-12-17 11:17:33 +0100110 DEBUGCC(call, remote, "Creating new remote instance.\n");
Harald Welte3e6376d2010-12-22 23:54:51 +0100111
112 /* link remote call */
113 call->remote_ref = remote->callref;
114 remote->remote_ref = call->callref;
115
Harald Weltebf0a7c92012-08-24 16:48:21 +0200116 /* send call proceeding */
117 memset(&mncc, 0, sizeof(struct gsm_mncc));
118 mncc.callref = call->callref;
Max3332ac42018-12-17 11:17:33 +0100119 DEBUGCC(call, remote, "Accepting call.\n");
Harald Weltebf0a7c92012-08-24 16:48:21 +0200120 mncc_tx_to_cc(call->net, MNCC_CALL_PROC_REQ, &mncc);
121
Harald Welte3e6376d2010-12-22 23:54:51 +0100122 /* modify mode */
123 memset(&mncc, 0, sizeof(struct gsm_mncc));
124 mncc.callref = call->callref;
Max3332ac42018-12-17 11:17:33 +0100125 DEBUGCC(call, remote, "Modify channel mode.\n");
Harald Welte76556372010-12-22 23:57:45 +0100126 mncc_tx_to_cc(call->net, MNCC_LCHAN_MODIFY, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100127
Harald Welte3e6376d2010-12-22 23:54:51 +0100128 /* send setup to remote */
129// setup->fields |= MNCC_F_SIGNAL;
130// setup->signal = GSM48_SIGNAL_DIALTONE;
131 setup->callref = remote->callref;
Max3332ac42018-12-17 11:17:33 +0100132 DEBUGCC(call, remote, "Forwarding SETUP to remote.\n");
Harald Welte76556372010-12-22 23:57:45 +0100133 return mncc_tx_to_cc(remote->net, MNCC_SETUP_REQ, setup);
Harald Welte3e6376d2010-12-22 23:54:51 +0100134
135out_reject:
Harald Welte76556372010-12-22 23:57:45 +0100136 mncc_tx_to_cc(call->net, MNCC_REJ_REQ, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100137 free_call(call);
138 return 0;
139}
140
141static int mncc_alert_ind(struct gsm_call *call, int msg_type,
142 struct gsm_mncc *alert)
143{
144 struct gsm_call *remote;
145
146 /* send alerting to remote */
147 if (!(remote = get_call_ref(call->remote_ref)))
148 return 0;
149 alert->callref = remote->callref;
Max3332ac42018-12-17 11:17:33 +0100150 DEBUGCC(call, remote, "Forwarding ALERT to remote.\n");
Harald Welte76556372010-12-22 23:57:45 +0100151 return mncc_tx_to_cc(remote->net, MNCC_ALERT_REQ, alert);
Harald Welte3e6376d2010-12-22 23:54:51 +0100152}
153
154static int mncc_notify_ind(struct gsm_call *call, int msg_type,
155 struct gsm_mncc *notify)
156{
157 struct gsm_call *remote;
158
159 /* send notify to remote */
160 if (!(remote = get_call_ref(call->remote_ref)))
161 return 0;
162 notify->callref = remote->callref;
Max3332ac42018-12-17 11:17:33 +0100163 DEBUGCC(call, remote, "Forwarding NOTIF to remote.\n");
Harald Welte76556372010-12-22 23:57:45 +0100164 return mncc_tx_to_cc(remote->net, MNCC_NOTIFY_REQ, notify);
Harald Welte3e6376d2010-12-22 23:54:51 +0100165}
166
167static int mncc_setup_cnf(struct gsm_call *call, int msg_type,
168 struct gsm_mncc *connect)
169{
Neels Hofmeyrf9773b02018-11-29 23:01:58 +0100170 struct gsm_mncc connect_ack;
Harald Welte3e6376d2010-12-22 23:54:51 +0100171 struct gsm_call *remote;
Harald Welte53d51f52015-12-03 14:59:04 +0100172 struct gsm_mncc_bridge bridge = { .msg_type = MNCC_BRIDGE };
Harald Welte3e6376d2010-12-22 23:54:51 +0100173
174 /* acknowledge connect */
175 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
176 connect_ack.callref = call->callref;
177 DEBUGP(DMNCC, "(call %x) Acknowledge SETUP.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100178 mncc_tx_to_cc(call->net, MNCC_SETUP_COMPL_REQ, &connect_ack);
Harald Welte3e6376d2010-12-22 23:54:51 +0100179
180 /* send connect message to remote */
181 if (!(remote = get_call_ref(call->remote_ref)))
182 return 0;
183 connect->callref = remote->callref;
Max3332ac42018-12-17 11:17:33 +0100184 DEBUGCC(call, remote, "Sending CONNECT to remote.\n");
Harald Welte76556372010-12-22 23:57:45 +0100185 mncc_tx_to_cc(remote->net, MNCC_SETUP_RSP, connect);
Harald Welte3e6376d2010-12-22 23:54:51 +0100186
187 /* bridge tch */
Harald Welte53d51f52015-12-03 14:59:04 +0100188 bridge.callref[0] = call->callref;
189 bridge.callref[1] = call->remote_ref;
Max3332ac42018-12-17 11:17:33 +0100190 DEBUGCC(call, remote, "Bridging with remote.\n");
Harald Welte3e6376d2010-12-22 23:54:51 +0100191
Neels Hofmeyrf9773b02018-11-29 23:01:58 +0100192 return mncc_tx_to_cc(call->net, MNCC_BRIDGE, &bridge);
Harald Welte3e6376d2010-12-22 23:54:51 +0100193}
194
195static int mncc_disc_ind(struct gsm_call *call, int msg_type,
196 struct gsm_mncc *disc)
197{
198 struct gsm_call *remote;
199
200 /* send release */
201 DEBUGP(DMNCC, "(call %x) Releasing call with cause %d\n",
202 call->callref, disc->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100203 mncc_tx_to_cc(call->net, MNCC_REL_REQ, disc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100204
205 /* send disc to remote */
206 if (!(remote = get_call_ref(call->remote_ref))) {
207 return 0;
208 }
209 disc->callref = remote->callref;
Max3332ac42018-12-17 11:17:33 +0100210 DEBUGCC(call, remote, "Disconnecting remote with cause %d\n", disc->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100211 return mncc_tx_to_cc(remote->net, MNCC_DISC_REQ, disc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100212}
213
214static int mncc_rel_ind(struct gsm_call *call, int msg_type, struct gsm_mncc *rel)
215{
216 struct gsm_call *remote;
217
218 /* send release to remote */
219 if (!(remote = get_call_ref(call->remote_ref))) {
220 free_call(call);
221 return 0;
222 }
Holger Hans Peter Freythera61c7092011-01-27 15:05:45 +0100223
Harald Welte3e6376d2010-12-22 23:54:51 +0100224 rel->callref = remote->callref;
Max3332ac42018-12-17 11:17:33 +0100225 DEBUGCC(call, remote, "Releasing remote with cause %d\n", rel->cause.value);
Harald Welte3e6376d2010-12-22 23:54:51 +0100226
Holger Hans Peter Freythera61c7092011-01-27 15:05:45 +0100227 /*
228 * Release this side of the call right now. Otherwise we end up
229 * in this method for the other call and will also try to release
230 * it and then we will end up with a double free and a crash
231 */
Harald Welte3e6376d2010-12-22 23:54:51 +0100232 free_call(call);
Holger Hans Peter Freythera61c7092011-01-27 15:05:45 +0100233 mncc_tx_to_cc(remote->net, MNCC_REL_REQ, rel);
Harald Welte3e6376d2010-12-22 23:54:51 +0100234
235 return 0;
236}
237
238static int mncc_rel_cnf(struct gsm_call *call, int msg_type, struct gsm_mncc *rel)
239{
240 free_call(call);
241 return 0;
242}
243
Harald Welte3e6376d2010-12-22 23:54:51 +0100244/* Internal MNCC handler input function (from CC -> MNCC -> here) */
Harald Welte29b64e92010-12-23 01:07:46 +0100245int int_mncc_recv(struct gsm_network *net, struct msgb *msg)
Harald Welte3e6376d2010-12-22 23:54:51 +0100246{
Harald Welte29b64e92010-12-23 01:07:46 +0100247 void *arg = msgb_data(msg);
Harald Welte3e6376d2010-12-22 23:54:51 +0100248 struct gsm_mncc *data = arg;
Harald Welte29b64e92010-12-23 01:07:46 +0100249 int msg_type = data->msg_type;
Harald Welte3e6376d2010-12-22 23:54:51 +0100250 int callref;
251 struct gsm_call *call = NULL, *callt;
252 int rc = 0;
253
254 /* Special messages */
255 switch(msg_type) {
256 }
257
258 /* find callref */
259 callref = data->callref;
260 llist_for_each_entry(callt, &call_list, entry) {
261 if (callt->callref == callref) {
262 call = callt;
263 break;
264 }
265 }
266
267 /* create callref, if setup is received */
268 if (!call) {
269 if (msg_type != MNCC_SETUP_IND)
Harald Welte29b64e92010-12-23 01:07:46 +0100270 goto out_free; /* drop */
Harald Welte3e6376d2010-12-22 23:54:51 +0100271 /* create call */
272 if (!(call = talloc_zero(tall_call_ctx, struct gsm_call))) {
273 struct gsm_mncc rel;
274
275 memset(&rel, 0, sizeof(struct gsm_mncc));
276 rel.callref = callref;
277 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
278 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte76556372010-12-22 23:57:45 +0100279 mncc_tx_to_cc(net, MNCC_REL_REQ, &rel);
Harald Welte29b64e92010-12-23 01:07:46 +0100280 goto out_free;
Harald Welte3e6376d2010-12-22 23:54:51 +0100281 }
282 llist_add_tail(&call->entry, &call_list);
283 call->net = net;
284 call->callref = callref;
285 DEBUGP(DMNCC, "(call %x) Call created.\n", call->callref);
286 }
287
Andreas Eversberg9acbe4c2013-03-11 08:12:43 +0100288 if (mncc_is_data_frame(msg_type)) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200289 LOGP(DMNCC, LOGL_ERROR, "(call %x) Received data frame, which is not supported.\n",
290 call->callref);
Andreas Eversberg9acbe4c2013-03-11 08:12:43 +0100291 goto out_free;
Harald Welte3e6376d2010-12-22 23:54:51 +0100292 }
293
Andreas Eversberg9acbe4c2013-03-11 08:12:43 +0100294 DEBUGP(DMNCC, "(call %x) Received message %s\n", call->callref,
295 get_mncc_name(msg_type));
296
Harald Welte3e6376d2010-12-22 23:54:51 +0100297 switch(msg_type) {
298 case MNCC_SETUP_IND:
299 rc = mncc_setup_ind(call, msg_type, arg);
300 break;
301 case MNCC_SETUP_CNF:
302 rc = mncc_setup_cnf(call, msg_type, arg);
303 break;
304 case MNCC_SETUP_COMPL_IND:
305 break;
306 case MNCC_CALL_CONF_IND:
307 /* we now need to MODIFY the channel */
Harald Welte76556372010-12-22 23:57:45 +0100308 mncc_tx_to_cc(call->net, MNCC_LCHAN_MODIFY, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100309 break;
310 case MNCC_ALERT_IND:
311 rc = mncc_alert_ind(call, msg_type, arg);
312 break;
313 case MNCC_NOTIFY_IND:
314 rc = mncc_notify_ind(call, msg_type, arg);
315 break;
316 case MNCC_DISC_IND:
317 rc = mncc_disc_ind(call, msg_type, arg);
318 break;
319 case MNCC_REL_IND:
320 case MNCC_REJ_IND:
321 rc = mncc_rel_ind(call, msg_type, arg);
322 break;
323 case MNCC_REL_CNF:
324 rc = mncc_rel_cnf(call, msg_type, arg);
325 break;
326 case MNCC_FACILITY_IND:
327 break;
328 case MNCC_START_DTMF_IND:
Harald Welte0c566a42016-10-29 22:23:19 +0200329 rc = mncc_tx_to_cc(net, MNCC_START_DTMF_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100330 break;
331 case MNCC_STOP_DTMF_IND:
Harald Welte0c566a42016-10-29 22:23:19 +0200332 rc = mncc_tx_to_cc(net, MNCC_STOP_DTMF_RSP, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100333 break;
334 case MNCC_MODIFY_IND:
335 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
336 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
337 DEBUGP(DMNCC, "(call %x) Rejecting MODIFY with cause %d\n",
338 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100339 rc = mncc_tx_to_cc(net, MNCC_MODIFY_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100340 break;
341 case MNCC_MODIFY_CNF:
342 break;
343 case MNCC_HOLD_IND:
344 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
345 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
346 DEBUGP(DMNCC, "(call %x) Rejecting HOLD with cause %d\n",
347 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100348 rc = mncc_tx_to_cc(net, MNCC_HOLD_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100349 break;
350 case MNCC_RETRIEVE_IND:
351 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
352 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
353 DEBUGP(DMNCC, "(call %x) Rejecting RETRIEVE with cause %d\n",
354 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100355 rc = mncc_tx_to_cc(net, MNCC_RETRIEVE_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100356 break;
Harald Welte3e6376d2010-12-22 23:54:51 +0100357 default:
358 LOGP(DMNCC, LOGL_NOTICE, "(call %x) Message unhandled\n", callref);
359 break;
360 }
361
Harald Welte29b64e92010-12-23 01:07:46 +0100362out_free:
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100363 msgb_free(msg);
Harald Welte29b64e92010-12-23 01:07:46 +0100364
Harald Welte3e6376d2010-12-22 23:54:51 +0100365 return rc;
366}