blob: 87f724cf6e24cc7cf4ff1f524109eebec05f98b3 [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);
118 mncc_send(call->net, MNCC_LCHAN_MODIFY, &mncc);
119
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);
124 mncc_send(call->net, MNCC_CALL_PROC_REQ, &mncc);
125
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);
131 return mncc_send(remote->net, MNCC_SETUP_REQ, setup);
132
133out_reject:
134 mncc_send(call->net, MNCC_REJ_REQ, &mncc);
135 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);
149 return mncc_send(remote->net, MNCC_ALERT_REQ, alert);
150}
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);
162 return mncc_send(remote->net, MNCC_NOTIFY_REQ, notify);
163}
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);
177 mncc_send(call->net, MNCC_SETUP_COMPL_REQ, &connect_ack);
178
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);
184 mncc_send(remote->net, MNCC_SETUP_RSP, connect);
185
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)
193 return mncc_send(call->net, MNCC_BRIDGE, refs);
194
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 */
199 return mncc_send(call->net, MNCC_BRIDGE, refs);
200 } 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;
206 mncc_send(call->net, MNCC_FRAME_RECV, &frame_recv);
207 frame_recv.callref = call->remote_ref;
208 return mncc_send(call->net, MNCC_FRAME_RECV, &frame_recv);
209 }
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);
220 mncc_send(call->net, MNCC_REL_REQ, disc);
221
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);
229 return mncc_send(remote->net, MNCC_DISC_REQ, disc);
230}
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);
244 mncc_send(remote->net, MNCC_REL_REQ, rel);
245
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) */
280int int_mncc_recv(struct gsm_network *net, int msg_type, void *arg)
281{
282 struct gsm_mncc *data = arg;
283 int callref;
284 struct gsm_call *call = NULL, *callt;
285 int rc = 0;
286
287 /* Special messages */
288 switch(msg_type) {
289 }
290
291 /* find callref */
292 callref = data->callref;
293 llist_for_each_entry(callt, &call_list, entry) {
294 if (callt->callref == callref) {
295 call = callt;
296 break;
297 }
298 }
299
300 /* create callref, if setup is received */
301 if (!call) {
302 if (msg_type != MNCC_SETUP_IND)
303 return 0; /* drop */
304 /* create call */
305 if (!(call = talloc_zero(tall_call_ctx, struct gsm_call))) {
306 struct gsm_mncc rel;
307
308 memset(&rel, 0, sizeof(struct gsm_mncc));
309 rel.callref = callref;
310 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
311 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
312 mncc_send(net, MNCC_REL_REQ, &rel);
313 return 0;
314 }
315 llist_add_tail(&call->entry, &call_list);
316 call->net = net;
317 call->callref = callref;
318 DEBUGP(DMNCC, "(call %x) Call created.\n", call->callref);
319 }
320
321 switch (msg_type) {
322 case GSM_TCHF_FRAME:
323 case GSM_TCHF_FRAME_EFR:
324 break;
325 default:
326 DEBUGP(DMNCC, "(call %x) Received message %s\n", call->callref,
327 get_mncc_name(msg_type));
328 break;
329 }
330
331 switch(msg_type) {
332 case MNCC_SETUP_IND:
333 rc = mncc_setup_ind(call, msg_type, arg);
334 break;
335 case MNCC_SETUP_CNF:
336 rc = mncc_setup_cnf(call, msg_type, arg);
337 break;
338 case MNCC_SETUP_COMPL_IND:
339 break;
340 case MNCC_CALL_CONF_IND:
341 /* we now need to MODIFY the channel */
342 data->lchan_mode = GSM48_CMODE_SPEECH_EFR;
343 mncc_send(call->net, MNCC_LCHAN_MODIFY, data);
344 break;
345 case MNCC_ALERT_IND:
346 rc = mncc_alert_ind(call, msg_type, arg);
347 break;
348 case MNCC_NOTIFY_IND:
349 rc = mncc_notify_ind(call, msg_type, arg);
350 break;
351 case MNCC_DISC_IND:
352 rc = mncc_disc_ind(call, msg_type, arg);
353 break;
354 case MNCC_REL_IND:
355 case MNCC_REJ_IND:
356 rc = mncc_rel_ind(call, msg_type, arg);
357 break;
358 case MNCC_REL_CNF:
359 rc = mncc_rel_cnf(call, msg_type, arg);
360 break;
361 case MNCC_FACILITY_IND:
362 break;
363 case MNCC_START_DTMF_IND:
364 break;
365 case MNCC_STOP_DTMF_IND:
366 break;
367 case MNCC_MODIFY_IND:
368 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
369 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
370 DEBUGP(DMNCC, "(call %x) Rejecting MODIFY with cause %d\n",
371 call->callref, data->cause.value);
372 rc = mncc_send(net, MNCC_MODIFY_REJ, data);
373 break;
374 case MNCC_MODIFY_CNF:
375 break;
376 case MNCC_HOLD_IND:
377 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
378 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
379 DEBUGP(DMNCC, "(call %x) Rejecting HOLD with cause %d\n",
380 call->callref, data->cause.value);
381 rc = mncc_send(net, MNCC_HOLD_REJ, data);
382 break;
383 case MNCC_RETRIEVE_IND:
384 mncc_set_cause(data, GSM48_CAUSE_LOC_PRN_S_LU,
385 GSM48_CC_CAUSE_SERV_OPT_UNIMPL);
386 DEBUGP(DMNCC, "(call %x) Rejecting RETRIEVE with cause %d\n",
387 call->callref, data->cause.value);
388 rc = mncc_send(net, MNCC_RETRIEVE_REJ, data);
389 break;
390 case GSM_TCHF_FRAME:
391 case GSM_TCHF_FRAME_EFR:
392 rc = mncc_rcv_tchf(call, msg_type, arg);
393 break;
394 default:
395 LOGP(DMNCC, LOGL_NOTICE, "(call %x) Message unhandled\n", callref);
396 break;
397 }
398
399 return rc;
400}