blob: 640a286bc384a2c7de6f635c9239a274f74fae81 [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>
29#include <sys/types.h>
30
31#include <openbsc/gsm_04_08.h>
32#include <openbsc/debug.h>
33#include <openbsc/mncc.h>
34#include <osmocore/talloc.h>
35#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
43static u_int32_t new_callref = 0x00000001;
44
45static void free_call(struct gsm_call *call)
46{
47 llist_del(&call->entry);
48 DEBUGP(DMNCC, "(call %x) Call removed.\n", call->callref);
49 talloc_free(call);
50}
51
52
53static struct gsm_call *get_call_ref(u_int32_t callref)
54{
55 struct gsm_call *callt;
56
57 llist_for_each_entry(callt, &call_list, entry) {
58 if (callt->callref == callref)
59 return callt;
60 }
61 return NULL;
62}
63
64
65/* on incoming call, look up database and send setup to remote subscr. */
66static int mncc_setup_ind(struct gsm_call *call, int msg_type,
67 struct gsm_mncc *setup)
68{
69 struct gsm_mncc mncc;
70 struct gsm_call *remote;
71
72 memset(&mncc, 0, sizeof(struct gsm_mncc));
73 mncc.callref = call->callref;
74
75 /* already have remote call */
76 if (call->remote_ref)
77 return 0;
78
79 /* transfer mode 1 would be packet mode, which was never specified */
80 if (setup->bearer_cap.mode != 0) {
81 LOGP(DMNCC, LOGL_NOTICE, "(call %x) We don't support "
82 "packet mode\n", call->callref);
83 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
84 GSM48_CC_CAUSE_BEARER_CA_UNAVAIL);
85 goto out_reject;
86 }
87
88 /* we currently only do speech */
89 if (setup->bearer_cap.transfer != GSM_MNCC_BCAP_SPEECH) {
90 LOGP(DMNCC, LOGL_NOTICE, "(call %x) We only support "
91 "voice calls\n", call->callref);
92 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
93 GSM48_CC_CAUSE_BEARER_CA_UNAVAIL);
94 goto out_reject;
95 }
96
97 /* create remote call */
98 if (!(remote = talloc(tall_call_ctx, struct gsm_call))) {
99 mncc_set_cause(&mncc, GSM48_CAUSE_LOC_PRN_S_LU,
100 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
101 goto out_reject;
102 }
103 llist_add_tail(&remote->entry, &call_list);
104 remote->net = call->net;
105 remote->callref = new_callref++;
106 DEBUGP(DMNCC, "(call %x) Creating new remote instance %x.\n",
107 call->callref, remote->callref);
108
109 /* link remote call */
110 call->remote_ref = remote->callref;
111 remote->remote_ref = call->callref;
112
113 /* modify mode */
114 memset(&mncc, 0, sizeof(struct gsm_mncc));
115 mncc.callref = call->callref;
116 mncc.lchan_mode = GSM48_CMODE_SPEECH_EFR;
117 DEBUGP(DMNCC, "(call %x) Modify channel mode.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100118 mncc_tx_to_cc(call->net, MNCC_LCHAN_MODIFY, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100119
120 /* send call proceeding */
121 memset(&mncc, 0, sizeof(struct gsm_mncc));
122 mncc.callref = call->callref;
123 DEBUGP(DMNCC, "(call %x) Accepting call.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100124 mncc_tx_to_cc(call->net, MNCC_CALL_PROC_REQ, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100125
126 /* send setup to remote */
127// setup->fields |= MNCC_F_SIGNAL;
128// setup->signal = GSM48_SIGNAL_DIALTONE;
129 setup->callref = remote->callref;
130 DEBUGP(DMNCC, "(call %x) Forwarding SETUP to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100131 return mncc_tx_to_cc(remote->net, MNCC_SETUP_REQ, setup);
Harald Welte3e6376d2010-12-22 23:54:51 +0100132
133out_reject:
Harald Welte76556372010-12-22 23:57:45 +0100134 mncc_tx_to_cc(call->net, MNCC_REJ_REQ, &mncc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100135 free_call(call);
136 return 0;
137}
138
139static int mncc_alert_ind(struct gsm_call *call, int msg_type,
140 struct gsm_mncc *alert)
141{
142 struct gsm_call *remote;
143
144 /* send alerting to remote */
145 if (!(remote = get_call_ref(call->remote_ref)))
146 return 0;
147 alert->callref = remote->callref;
148 DEBUGP(DMNCC, "(call %x) Forwarding ALERT to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100149 return mncc_tx_to_cc(remote->net, MNCC_ALERT_REQ, alert);
Harald Welte3e6376d2010-12-22 23:54:51 +0100150}
151
152static int mncc_notify_ind(struct gsm_call *call, int msg_type,
153 struct gsm_mncc *notify)
154{
155 struct gsm_call *remote;
156
157 /* send notify to remote */
158 if (!(remote = get_call_ref(call->remote_ref)))
159 return 0;
160 notify->callref = remote->callref;
161 DEBUGP(DMNCC, "(call %x) Forwarding NOTIF to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100162 return mncc_tx_to_cc(remote->net, MNCC_NOTIFY_REQ, notify);
Harald Welte3e6376d2010-12-22 23:54:51 +0100163}
164
165static int mncc_setup_cnf(struct gsm_call *call, int msg_type,
166 struct gsm_mncc *connect)
167{
168 struct gsm_mncc connect_ack, frame_recv;
169 struct gsm_network *net = call->net;
170 struct gsm_call *remote;
171 u_int32_t refs[2];
172
173 /* acknowledge connect */
174 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
175 connect_ack.callref = call->callref;
176 DEBUGP(DMNCC, "(call %x) Acknowledge SETUP.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100177 mncc_tx_to_cc(call->net, MNCC_SETUP_COMPL_REQ, &connect_ack);
Harald Welte3e6376d2010-12-22 23:54:51 +0100178
179 /* send connect message to remote */
180 if (!(remote = get_call_ref(call->remote_ref)))
181 return 0;
182 connect->callref = remote->callref;
183 DEBUGP(DMNCC, "(call %x) Sending CONNECT to remote.\n", call->callref);
Harald Welte76556372010-12-22 23:57:45 +0100184 mncc_tx_to_cc(remote->net, MNCC_SETUP_RSP, connect);
Harald Welte3e6376d2010-12-22 23:54:51 +0100185
186 /* bridge tch */
187 refs[0] = call->callref;
188 refs[1] = call->remote_ref;
189 DEBUGP(DMNCC, "(call %x) Bridging with remote.\n", call->callref);
190
191 /* in direct mode, we always have to bridge the channels */
192 if (ipacc_rtp_direct)
Harald Welte76556372010-12-22 23:57:45 +0100193 return mncc_tx_to_cc(call->net, MNCC_BRIDGE, refs);
Harald Welte3e6376d2010-12-22 23:54:51 +0100194
195 /* proxy mode */
196 if (!net->handover.active) {
197 /* in the no-handover case, we can bridge, i.e. use
198 * the old RTP proxy code */
Harald Welte76556372010-12-22 23:57:45 +0100199 return mncc_tx_to_cc(call->net, MNCC_BRIDGE, refs);
Harald Welte3e6376d2010-12-22 23:54:51 +0100200 } else {
201 /* in case of handover, we need to re-write the RTP
202 * SSRC, sequence and timestamp values and thus
203 * need to enable RTP receive for both directions */
204 memset(&frame_recv, 0, sizeof(struct gsm_mncc));
205 frame_recv.callref = call->callref;
Harald Welte76556372010-12-22 23:57:45 +0100206 mncc_tx_to_cc(call->net, MNCC_FRAME_RECV, &frame_recv);
Harald Welte3e6376d2010-12-22 23:54:51 +0100207 frame_recv.callref = call->remote_ref;
Harald Welte76556372010-12-22 23:57:45 +0100208 return mncc_tx_to_cc(call->net, MNCC_FRAME_RECV, &frame_recv);
Harald Welte3e6376d2010-12-22 23:54:51 +0100209 }
210}
211
212static int mncc_disc_ind(struct gsm_call *call, int msg_type,
213 struct gsm_mncc *disc)
214{
215 struct gsm_call *remote;
216
217 /* send release */
218 DEBUGP(DMNCC, "(call %x) Releasing call with cause %d\n",
219 call->callref, disc->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100220 mncc_tx_to_cc(call->net, MNCC_REL_REQ, disc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100221
222 /* send disc to remote */
223 if (!(remote = get_call_ref(call->remote_ref))) {
224 return 0;
225 }
226 disc->callref = remote->callref;
227 DEBUGP(DMNCC, "(call %x) Disconnecting remote with cause %d\n",
228 remote->callref, disc->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100229 return mncc_tx_to_cc(remote->net, MNCC_DISC_REQ, disc);
Harald Welte3e6376d2010-12-22 23:54:51 +0100230}
231
232static int mncc_rel_ind(struct gsm_call *call, int msg_type, struct gsm_mncc *rel)
233{
234 struct gsm_call *remote;
235
236 /* send release to remote */
237 if (!(remote = get_call_ref(call->remote_ref))) {
238 free_call(call);
239 return 0;
240 }
241 rel->callref = remote->callref;
242 DEBUGP(DMNCC, "(call %x) Releasing remote with cause %d\n",
243 call->callref, rel->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100244 mncc_tx_to_cc(remote->net, MNCC_REL_REQ, rel);
Harald Welte3e6376d2010-12-22 23:54:51 +0100245
246 free_call(call);
247
248 return 0;
249}
250
251static int mncc_rel_cnf(struct gsm_call *call, int msg_type, struct gsm_mncc *rel)
252{
253 free_call(call);
254 return 0;
255}
256
257/* receiving a TCH/F frame from the BSC code */
258static int mncc_rcv_tchf(struct gsm_call *call, int msg_type,
259 struct gsm_data_frame *dfr)
260{
261 struct gsm_trans *remote_trans;
262
263 remote_trans = trans_find_by_callref(call->net, call->remote_ref);
264
265 /* this shouldn't really happen */
266 if (!remote_trans || !remote_trans->conn) {
267 LOGP(DMNCC, LOGL_ERROR, "No transaction or transaction without lchan?!?\n");
268 return -EIO;
269 }
270
271 /* RTP socket of remote end has meanwhile died */
272 if (!remote_trans->conn->lchan->abis_ip.rtp_socket)
273 return -EIO;
274
275 return rtp_send_frame(remote_trans->conn->lchan->abis_ip.rtp_socket, dfr);
276}
277
278
279/* Internal MNCC handler input function (from CC -> MNCC -> here) */
Harald Welte29b64e92010-12-23 01:07:46 +0100280int int_mncc_recv(struct gsm_network *net, struct msgb *msg)
Harald Welte3e6376d2010-12-22 23:54:51 +0100281{
Harald Welte29b64e92010-12-23 01:07:46 +0100282 void *arg = msgb_data(msg);
Harald Welte3e6376d2010-12-22 23:54:51 +0100283 struct gsm_mncc *data = arg;
Harald Welte29b64e92010-12-23 01:07:46 +0100284 int msg_type = data->msg_type;
Harald Welte3e6376d2010-12-22 23:54:51 +0100285 int callref;
286 struct gsm_call *call = NULL, *callt;
287 int rc = 0;
288
289 /* Special messages */
290 switch(msg_type) {
291 }
292
293 /* find callref */
294 callref = data->callref;
295 llist_for_each_entry(callt, &call_list, entry) {
296 if (callt->callref == callref) {
297 call = callt;
298 break;
299 }
300 }
301
302 /* create callref, if setup is received */
303 if (!call) {
304 if (msg_type != MNCC_SETUP_IND)
Harald Welte29b64e92010-12-23 01:07:46 +0100305 goto out_free; /* drop */
Harald Welte3e6376d2010-12-22 23:54:51 +0100306 /* create call */
307 if (!(call = talloc_zero(tall_call_ctx, struct gsm_call))) {
308 struct gsm_mncc rel;
309
310 memset(&rel, 0, sizeof(struct gsm_mncc));
311 rel.callref = callref;
312 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
313 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte76556372010-12-22 23:57:45 +0100314 mncc_tx_to_cc(net, MNCC_REL_REQ, &rel);
Harald Welte29b64e92010-12-23 01:07:46 +0100315 goto out_free;
Harald Welte3e6376d2010-12-22 23:54:51 +0100316 }
317 llist_add_tail(&call->entry, &call_list);
318 call->net = net;
319 call->callref = callref;
320 DEBUGP(DMNCC, "(call %x) Call created.\n", call->callref);
321 }
322
323 switch (msg_type) {
324 case GSM_TCHF_FRAME:
325 case GSM_TCHF_FRAME_EFR:
326 break;
327 default:
328 DEBUGP(DMNCC, "(call %x) Received message %s\n", call->callref,
329 get_mncc_name(msg_type));
330 break;
331 }
332
333 switch(msg_type) {
334 case MNCC_SETUP_IND:
335 rc = mncc_setup_ind(call, msg_type, arg);
336 break;
337 case MNCC_SETUP_CNF:
338 rc = mncc_setup_cnf(call, msg_type, arg);
339 break;
340 case MNCC_SETUP_COMPL_IND:
341 break;
342 case MNCC_CALL_CONF_IND:
343 /* we now need to MODIFY the channel */
344 data->lchan_mode = GSM48_CMODE_SPEECH_EFR;
Harald Welte76556372010-12-22 23:57:45 +0100345 mncc_tx_to_cc(call->net, MNCC_LCHAN_MODIFY, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100346 break;
347 case MNCC_ALERT_IND:
348 rc = mncc_alert_ind(call, msg_type, arg);
349 break;
350 case MNCC_NOTIFY_IND:
351 rc = mncc_notify_ind(call, msg_type, arg);
352 break;
353 case MNCC_DISC_IND:
354 rc = mncc_disc_ind(call, msg_type, arg);
355 break;
356 case MNCC_REL_IND:
357 case MNCC_REJ_IND:
358 rc = mncc_rel_ind(call, msg_type, arg);
359 break;
360 case MNCC_REL_CNF:
361 rc = mncc_rel_cnf(call, msg_type, arg);
362 break;
363 case MNCC_FACILITY_IND:
364 break;
365 case MNCC_START_DTMF_IND:
366 break;
367 case MNCC_STOP_DTMF_IND:
368 break;
369 case MNCC_MODIFY_IND:
370 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
371 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
372 DEBUGP(DMNCC, "(call %x) Rejecting MODIFY with cause %d\n",
373 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100374 rc = mncc_tx_to_cc(net, MNCC_MODIFY_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100375 break;
376 case MNCC_MODIFY_CNF:
377 break;
378 case MNCC_HOLD_IND:
379 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
380 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
381 DEBUGP(DMNCC, "(call %x) Rejecting HOLD with cause %d\n",
382 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100383 rc = mncc_tx_to_cc(net, MNCC_HOLD_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100384 break;
385 case MNCC_RETRIEVE_IND:
386 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
387 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
388 DEBUGP(DMNCC, "(call %x) Rejecting RETRIEVE with cause %d\n",
389 call->callref, data->cause.value);
Harald Welte76556372010-12-22 23:57:45 +0100390 rc = mncc_tx_to_cc(net, MNCC_RETRIEVE_REJ, data);
Harald Welte3e6376d2010-12-22 23:54:51 +0100391 break;
392 case GSM_TCHF_FRAME:
393 case GSM_TCHF_FRAME_EFR:
394 rc = mncc_rcv_tchf(call, msg_type, arg);
395 break;
396 default:
397 LOGP(DMNCC, LOGL_NOTICE, "(call %x) Message unhandled\n", callref);
398 break;
399 }
400
Harald Welte29b64e92010-12-23 01:07:46 +0100401out_free:
402 talloc_free(msg);
403
Harald Welte3e6376d2010-12-22 23:54:51 +0100404 return rc;
405}