blob: 7d5a7f163024d09056667a437b8fe485fbfdf8d6 [file] [log] [blame]
Harald Welte3e6376d2010-12-22 23:54:51 +01001/* mncc_builtin.c - default, minimal built-in MNCC Application for
2 * standalone bsc_hack (netowrk-in-the-box mode) */
3
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
30#include <openbsc/gsm_04_08.h>
31#include <openbsc/debug.h>
32#include <openbsc/mncc.h>
Harald Welteab386e62011-09-01 18:18:43 +020033#include <openbsc/mncc_int.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010034#include <osmocom/core/talloc.h>
Harald Welte3e6376d2010-12-22 23:54:51 +010035#include <openbsc/gsm_data.h>
36#include <openbsc/transaction.h>
37#include <openbsc/rtp_proxy.h>
38
39void *tall_call_ctx;
40
41static LLIST_HEAD(call_list);
42
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020043static uint32_t new_callref = 0x00000001;
Harald Welte3e6376d2010-12-22 23:54:51 +010044
Harald Welteab386e62011-09-01 18:18:43 +020045struct mncc_int mncc_int = {
Andreas Eversberga83d5112013-12-07 18:32:28 +010046 .def_codec = { GSM48_CMODE_SPEECH_V1, GSM48_CMODE_SPEECH_V1 },
Harald Welteab386e62011-09-01 18:18:43 +020047};
48
Harald Welte3e6376d2010-12-22 23:54:51 +010049static void free_call(struct gsm_call *call)
50{
51 llist_del(&call->entry);
52 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
53 talloc_free(call);
54}
55
56
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020057static struct gsm_call *get_call_ref(uint32_t callref)
Harald Welte3e6376d2010-12-22 23:54:51 +010058{
59 struct gsm_call *callt;
60
61 llist_for_each_entry(callt, &call_list, entry) {
62 if (callt->callref == callref)
63 return callt;
64 }
65 return NULL;
66}
67
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020068uint8_t mncc_codec_for_mode(int lchan_type)
Harald Welteab386e62011-09-01 18:18:43 +020069{
70 /* FIXME: check codec capabilities of the phone */
71
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020072 if (lchan_type != GSM_LCHAN_TCH_H)
Harald Welteab386e62011-09-01 18:18:43 +020073 return mncc_int.def_codec[0];
74 else
75 return mncc_int.def_codec[1];
76}
Harald Welte3e6376d2010-12-22 23:54:51 +010077
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020078static uint8_t determine_lchan_mode(struct gsm_mncc *setup)
79{
80 return mncc_codec_for_mode(setup->lchan_type);
81}
82
Harald Welte3e6376d2010-12-22 23:54:51 +010083/* on incoming call, look up database and send setup to remote subscr. */
84static int mncc_setup_ind(struct gsm_call *call, int msg_type,
85 struct gsm_mncc *setup)
86{
87 struct gsm_mncc mncc;
88 struct gsm_call *remote;
89
90 memset(&mncc, 0, sizeof(struct gsm_mncc));
91 mncc.callref = call->callref;
92
93 /* already have remote call */
94 if (call->remote_ref)
95 return 0;
96
97 /* transfer mode 1 would be packet mode, which was never specified */
98 if (setup->bearer_cap.mode != 0) {
99 LOGP(DMNCC, LOGL_NOTICE, "(call %x) We don't support "
100 "packet mode\n", call->callref);
101 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
102 GSM48_CC_CAUSE_BEARER_CA_UNAVAIL);
103 goto out_reject;
104 }
105
106 /* we currently only do speech */
107 if (setup->bearer_cap.transfer != GSM_MNCC_BCAP_SPEECH) {
108 LOGP(DMNCC, LOGL_NOTICE, "(call %x) We only support "
109 "voice calls\n", call->callref);
110 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
111 GSM48_CC_CAUSE_BEARER_CA_UNAVAIL);
112 goto out_reject;
113 }
114
115 /* create remote call */
Andreas Eversbergc5e08512013-01-25 08:36:32 +0100116 if (!(remote = talloc_zero(tall_call_ctx, struct gsm_call))) {
Harald Welte3e6376d2010-12-22 23:54:51 +0100117 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
118 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
119 goto out_reject;
120 }
121 llist_add_tail(&remote->entry, &call_list);
122 remote->net = call->net;
123 remote->callref = new_callref++;
124 DEBUGP(DMNCC, "(call %x) Creating new remote instance %x.\n",
125 call->callref, remote->callref);
126
127 /* link remote call */
128 call->remote_ref = remote->callref;
129 remote->remote_ref = call->callref;
130
Harald Weltebf0a7c92012-08-24 16:48:21 +0200131 /* send call proceeding */
132 memset(&mncc, 0, sizeof(struct gsm_mncc));
133 mncc.callref = call->callref;
134 DEBUGP(DMNCC, "(call %x) Accepting call.\n", call->callref);
135 mncc_tx_to_cc(call->net, MNCC_CALL_PROC_REQ, &mncc);
136
Harald Welte3e6376d2010-12-22 23:54:51 +0100137 /* modify mode */
138 memset(&mncc, 0, sizeof(struct gsm_mncc));
139 mncc.callref = call->callref;
Harald Welteab386e62011-09-01 18:18:43 +0200140 mncc.lchan_mode = determine_lchan_mode(setup);
Harald Welte3e6376d2010-12-22 23:54:51 +0100141 DEBUGP(DMNCC, "(call %x) Modify channel mode.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100142 mncc_tx_to_cc(call->net, MNCC_LCHAN_MODIFY, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100143
Harald Welte3e6376d2010-12-22 23:54:51 +0100144 /* send setup to remote */
145// setup->fields |= MNCC_F_SIGNAL;
146// setup->signal = GSM48_SIGNAL_DIALTONE;
147 setup->callref = remote->callref;
148 DEBUGP(DMNCC, "(call %x) Forwarding SETUP to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100149 return mncc_tx_to_cc(remote->net, MNCC_SETUP_REQ, setup);
Harald Welte3e6376d2010-12-22 23:54:51 +0100150
151out_reject:
Harald Welte76556372010-12-22 23:57:45 +0100152 mncc_tx_to_cc(call->net, MNCC_REJ_REQ, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100153 free_call(call);
154 return 0;
155}
156
157static int mncc_alert_ind(struct gsm_call *call, int msg_type,
158 struct gsm_mncc *alert)
159{
160 struct gsm_call *remote;
161
162 /* send alerting to remote */
163 if (!(remote = get_call_ref(call->remote_ref)))
164 return 0;
165 alert->callref = remote->callref;
166 DEBUGP(DMNCC, "(call %x) Forwarding ALERT to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100167 return mncc_tx_to_cc(remote->net, MNCC_ALERT_REQ, alert);
Harald Welte3e6376d2010-12-22 23:54:51 +0100168}
169
170static int mncc_notify_ind(struct gsm_call *call, int msg_type,
171 struct gsm_mncc *notify)
172{
173 struct gsm_call *remote;
174
175 /* send notify to remote */
176 if (!(remote = get_call_ref(call->remote_ref)))
177 return 0;
178 notify->callref = remote->callref;
179 DEBUGP(DMNCC, "(call %x) Forwarding NOTIF to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100180 return mncc_tx_to_cc(remote->net, MNCC_NOTIFY_REQ, notify);
Harald Welte3e6376d2010-12-22 23:54:51 +0100181}
182
183static int mncc_setup_cnf(struct gsm_call *call, int msg_type,
184 struct gsm_mncc *connect)
185{
186 struct gsm_mncc connect_ack, frame_recv;
187 struct gsm_network *net = call->net;
188 struct gsm_call *remote;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200189 uint32_t refs[2];
Harald Welte3e6376d2010-12-22 23:54:51 +0100190
191 /* acknowledge connect */
192 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
193 connect_ack.callref = call->callref;
194 DEBUGP(DMNCC, "(call %x) Acknowledge SETUP.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100195 mncc_tx_to_cc(call->net, MNCC_SETUP_COMPL_REQ, &connect_ack);
Harald Welte3e6376d2010-12-22 23:54:51 +0100196
197 /* send connect message to remote */
198 if (!(remote = get_call_ref(call->remote_ref)))
199 return 0;
200 connect->callref = remote->callref;
201 DEBUGP(DMNCC, "(call %x) Sending CONNECT to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100202 mncc_tx_to_cc(remote->net, MNCC_SETUP_RSP, connect);
Harald Welte3e6376d2010-12-22 23:54:51 +0100203
204 /* bridge tch */
205 refs[0] = call->callref;
206 refs[1] = call->remote_ref;
207 DEBUGP(DMNCC, "(call %x) Bridging with remote.\n", call->callref);
208
209 /* in direct mode, we always have to bridge the channels */
210 if (ipacc_rtp_direct)
Harald Welte76556372010-12-22 23:57:45 +0100211 return mncc_tx_to_cc(call->net, MNCC_BRIDGE, refs);
Harald Welte3e6376d2010-12-22 23:54:51 +0100212
213 /* proxy mode */
214 if (!net->handover.active) {
215 /* in the no-handover case, we can bridge, i.e. use
216 * the old RTP proxy code */
Harald Welte76556372010-12-22 23:57:45 +0100217 return mncc_tx_to_cc(call->net, MNCC_BRIDGE, refs);
Harald Welte3e6376d2010-12-22 23:54:51 +0100218 } else {
219 /* in case of handover, we need to re-write the RTP
220 * SSRC, sequence and timestamp values and thus
221 * need to enable RTP receive for both directions */
222 memset(&frame_recv, 0, sizeof(struct gsm_mncc));
223 frame_recv.callref = call->callref;
Harald Welte76556372010-12-22 23:57:45 +0100224 mncc_tx_to_cc(call->net, MNCC_FRAME_RECV, &frame_recv);
Harald Welte3e6376d2010-12-22 23:54:51 +0100225 frame_recv.callref = call->remote_ref;
Harald Welte76556372010-12-22 23:57:45 +0100226 return mncc_tx_to_cc(call->net, MNCC_FRAME_RECV, &frame_recv);
Harald Welte3e6376d2010-12-22 23:54:51 +0100227 }
228}
229
230static int mncc_disc_ind(struct gsm_call *call, int msg_type,
231 struct gsm_mncc *disc)
232{
233 struct gsm_call *remote;
234
235 /* send release */
236 DEBUGP(DMNCC, "(call %x) Releasing call with cause %d\n",
237 call->callref, disc->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100238 mncc_tx_to_cc(call->net, MNCC_REL_REQ, disc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100239
240 /* send disc to remote */
241 if (!(remote = get_call_ref(call->remote_ref))) {
242 return 0;
243 }
244 disc->callref = remote->callref;
245 DEBUGP(DMNCC, "(call %x) Disconnecting remote with cause %d\n",
246 remote->callref, disc->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100247 return mncc_tx_to_cc(remote->net, MNCC_DISC_REQ, disc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100248}
249
250static int mncc_rel_ind(struct gsm_call *call, int msg_type, struct gsm_mncc *rel)
251{
252 struct gsm_call *remote;
253
254 /* send release to remote */
255 if (!(remote = get_call_ref(call->remote_ref))) {
256 free_call(call);
257 return 0;
258 }
Holger Hans Peter Freythera61c7092011-01-27 15:05:45 +0100259
Harald Welte3e6376d2010-12-22 23:54:51 +0100260 rel->callref = remote->callref;
261 DEBUGP(DMNCC, "(call %x) Releasing remote with cause %d\n",
262 call->callref, rel->cause.value);
Harald Welte3e6376d2010-12-22 23:54:51 +0100263
Holger Hans Peter Freythera61c7092011-01-27 15:05:45 +0100264 /*
265 * Release this side of the call right now. Otherwise we end up
266 * in this method for the other call and will also try to release
267 * it and then we will end up with a double free and a crash
268 */
Harald Welte3e6376d2010-12-22 23:54:51 +0100269 free_call(call);
Holger Hans Peter Freythera61c7092011-01-27 15:05:45 +0100270 mncc_tx_to_cc(remote->net, MNCC_REL_REQ, rel);
Harald Welte3e6376d2010-12-22 23:54:51 +0100271
272 return 0;
273}
274
275static int mncc_rel_cnf(struct gsm_call *call, int msg_type, struct gsm_mncc *rel)
276{
277 free_call(call);
278 return 0;
279}
280
Andreas Eversberg9acbe4c2013-03-11 08:12:43 +0100281/* receiving a (speech) traffic frame from the BSC code */
282static int mncc_rcv_data(struct gsm_call *call, int msg_type,
Harald Welte3e6376d2010-12-22 23:54:51 +0100283 struct gsm_data_frame *dfr)
284{
285 struct gsm_trans *remote_trans;
286
287 remote_trans = trans_find_by_callref(call->net, call->remote_ref);
288
289 /* this shouldn't really happen */
290 if (!remote_trans || !remote_trans->conn) {
291 LOGP(DMNCC, LOGL_ERROR, "No transaction or transaction without lchan?!?\n");
292 return -EIO;
293 }
294
295 /* RTP socket of remote end has meanwhile died */
296 if (!remote_trans->conn->lchan->abis_ip.rtp_socket)
297 return -EIO;
298
299 return rtp_send_frame(remote_trans->conn->lchan->abis_ip.rtp_socket, dfr);
300}
301
302
303/* Internal MNCC handler input function (from CC -> MNCC -> here) */
Harald Welte29b64e92010-12-23 01:07:46 +0100304int int_mncc_recv(struct gsm_network *net, struct msgb *msg)
Harald Welte3e6376d2010-12-22 23:54:51 +0100305{
Harald Welte29b64e92010-12-23 01:07:46 +0100306 void *arg = msgb_data(msg);
Harald Welte3e6376d2010-12-22 23:54:51 +0100307 struct gsm_mncc *data = arg;
Harald Welte29b64e92010-12-23 01:07:46 +0100308 int msg_type = data->msg_type;
Harald Welte3e6376d2010-12-22 23:54:51 +0100309 int callref;
310 struct gsm_call *call = NULL, *callt;
311 int rc = 0;
312
313 /* Special messages */
314 switch(msg_type) {
315 }
316
317 /* find callref */
318 callref = data->callref;
319 llist_for_each_entry(callt, &call_list, entry) {
320 if (callt->callref == callref) {
321 call = callt;
322 break;
323 }
324 }
325
326 /* create callref, if setup is received */
327 if (!call) {
328 if (msg_type != MNCC_SETUP_IND)
Harald Welte29b64e92010-12-23 01:07:46 +0100329 goto out_free; /* drop */
Harald Welte3e6376d2010-12-22 23:54:51 +0100330 /* create call */
331 if (!(call = talloc_zero(tall_call_ctx, struct gsm_call))) {
332 struct gsm_mncc rel;
333
334 memset(&rel, 0, sizeof(struct gsm_mncc));
335 rel.callref = callref;
336 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
337 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte76556372010-12-22 23:57:45 +0100338 mncc_tx_to_cc(net, MNCC_REL_REQ, &rel);
Harald Welte29b64e92010-12-23 01:07:46 +0100339 goto out_free;
Harald Welte3e6376d2010-12-22 23:54:51 +0100340 }
341 llist_add_tail(&call->entry, &call_list);
342 call->net = net;
343 call->callref = callref;
344 DEBUGP(DMNCC, "(call %x) Call created.\n", call->callref);
345 }
346
Andreas Eversberg9acbe4c2013-03-11 08:12:43 +0100347 if (mncc_is_data_frame(msg_type)) {
348 rc = mncc_rcv_data(call, msg_type, arg);
349 goto out_free;
Harald Welte3e6376d2010-12-22 23:54:51 +0100350 }
351
Andreas Eversberg9acbe4c2013-03-11 08:12:43 +0100352 DEBUGP(DMNCC, "(call %x) Received message %s\n", call->callref,
353 get_mncc_name(msg_type));
354
Harald Welte3e6376d2010-12-22 23:54:51 +0100355 switch(msg_type) {
356 case MNCC_SETUP_IND:
357 rc = mncc_setup_ind(call, msg_type, arg);
358 break;
359 case MNCC_SETUP_CNF:
360 rc = mncc_setup_cnf(call, msg_type, arg);
361 break;
362 case MNCC_SETUP_COMPL_IND:
363 break;
364 case MNCC_CALL_CONF_IND:
365 /* we now need to MODIFY the channel */
Harald Welteab386e62011-09-01 18:18:43 +0200366 data->lchan_mode = determine_lchan_mode(data);
Harald Welte76556372010-12-22 23:57:45 +0100367 mncc_tx_to_cc(call->net, MNCC_LCHAN_MODIFY, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100368 break;
369 case MNCC_ALERT_IND:
370 rc = mncc_alert_ind(call, msg_type, arg);
371 break;
372 case MNCC_NOTIFY_IND:
373 rc = mncc_notify_ind(call, msg_type, arg);
374 break;
375 case MNCC_DISC_IND:
376 rc = mncc_disc_ind(call, msg_type, arg);
377 break;
378 case MNCC_REL_IND:
379 case MNCC_REJ_IND:
380 rc = mncc_rel_ind(call, msg_type, arg);
381 break;
382 case MNCC_REL_CNF:
383 rc = mncc_rel_cnf(call, msg_type, arg);
384 break;
385 case MNCC_FACILITY_IND:
386 break;
387 case MNCC_START_DTMF_IND:
388 break;
389 case MNCC_STOP_DTMF_IND:
390 break;
391 case MNCC_MODIFY_IND:
392 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
393 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
394 DEBUGP(DMNCC, "(call %x) Rejecting MODIFY with cause %d\n",
395 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100396 rc = mncc_tx_to_cc(net, MNCC_MODIFY_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100397 break;
398 case MNCC_MODIFY_CNF:
399 break;
400 case MNCC_HOLD_IND:
401 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
402 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
403 DEBUGP(DMNCC, "(call %x) Rejecting HOLD with cause %d\n",
404 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100405 rc = mncc_tx_to_cc(net, MNCC_HOLD_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100406 break;
407 case MNCC_RETRIEVE_IND:
408 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
409 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
410 DEBUGP(DMNCC, "(call %x) Rejecting RETRIEVE with cause %d\n",
411 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100412 rc = mncc_tx_to_cc(net, MNCC_RETRIEVE_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100413 break;
Harald Welte3e6376d2010-12-22 23:54:51 +0100414 default:
415 LOGP(DMNCC, LOGL_NOTICE, "(call %x) Message unhandled\n", callref);
416 break;
417 }
418
Harald Welte29b64e92010-12-23 01:07:46 +0100419out_free:
Holger Hans Peter Freythera8ddb082012-03-01 20:30:32 +0100420 msgb_free(msg);
Harald Welte29b64e92010-12-23 01:07:46 +0100421
Harald Welte3e6376d2010-12-22 23:54:51 +0100422 return rc;
423}