blob: c764cc24ac46eaec0f4e3d15520cc7fd76112a9a [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* Bloated main routine, refactor */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +01007 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010015 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080016 *
Holger Hans Peter Freytherde56c222011-01-16 17:45:14 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080019 *
20 */
21
22#include <mtp_data.h>
Holger Hans Peter Freytherb38b33b2010-11-26 21:21:04 +010023#include <mtp_level3.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080024#include <mtp_pcap.h>
25#include <thread.h>
26#include <bss_patch.h>
27#include <bssap_sccp.h>
28#include <bsc_data.h>
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080029#include <cellmgr_debug.h>
Holger Hans Peter Freytherb3e10682010-09-30 01:57:45 +080030#include <bsc_sccp.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080031
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080032#include <osmocore/talloc.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080033
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080034#include <osmocom/vty/vty.h>
Holger Hans Peter Freytherfdae5c92010-09-30 01:01:03 +080035#include <osmocom/vty/telnet_interface.h>
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080036
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080037#include <sys/stat.h>
38#include <sys/types.h>
39
40#include <fcntl.h>
41#include <signal.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <assert.h>
46#include <unistd.h>
47
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080048#ifndef _GNU_SOURCE
49#define _GNU_SOURCE
50#endif
51#include <getopt.h>
52
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +080053#undef PACKAGE_NAME
54#undef PACKAGE_VERSION
55#undef PACKAGE_BUGREPORT
56#undef PACKAGE_TARNAME
57#undef PACKAGE_STRING
58#include <cellmgr_config.h>
59
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +080060static struct log_target *stderr_target;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080061
62static char *config = "cellmgr_ng.cfg";
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080063
Holger Hans Peter Freyther7942abc2010-09-30 00:34:46 +080064struct bsc_data bsc;
65extern void cell_vty_init(void);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080066
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010067static void send_reset_ack(struct mtp_link_set *link, int sls);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080068static void bsc_resources_released(struct bsc_data *bsc);
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010069static void handle_local_sccp(struct mtp_link_set *link, struct msgb *inp, struct sccp_parse_result *res, int sls);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080070static void clear_connections(struct bsc_data *bsc);
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010071static void send_local_rlsd(struct mtp_link_set *link, struct sccp_parse_result *res);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080072
73/* send a RSIP to the MGCP GW */
74static void mgcp_reset(struct bsc_data *bsc)
75{
76 static const char mgcp_reset[] = {
77 "RSIP 1 13@mgw MGCP 1.0\r\n"
78 };
79
Holger Hans Peter Freyther5aa17012010-07-31 04:37:26 +080080 mgcp_forward(bsc, (const uint8_t *) mgcp_reset, strlen(mgcp_reset));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080081}
82
83/*
84 * methods called from the MTP Level3 part
85 */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +010086void mtp_link_set_forward_sccp(struct mtp_link_set *link, struct msgb *_msg, int sls)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080087{
88 int rc;
89 struct sccp_parse_result result;
90
91 rc = bss_patch_filter_msg(_msg, &result);
92 if (rc == BSS_FILTER_RESET) {
93 LOGP(DMSC, LOGL_NOTICE, "Filtering BSS Reset from the BSC\n");
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +080094 mgcp_reset(&bsc);
95 send_reset_ack(link, sls);
96 return;
97 }
98
99 /* special responder */
Holger Hans Peter Freyther0c95c6a2010-08-07 02:37:43 +0800100 if (bsc.msc_link_down) {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800101 if (rc == BSS_FILTER_RESET_ACK && bsc.reset_count > 0) {
102 LOGP(DMSC, LOGL_ERROR, "Received reset ack for closing.\n");
103 clear_connections(&bsc);
104 bsc_resources_released(&bsc);
105 return;
106 }
107
108 if (rc != 0 && rc != BSS_FILTER_RLSD && rc != BSS_FILTER_RLC) {
109 LOGP(DMSC, LOGL_ERROR, "Ignoring unparsable msg during closedown.\n");
110 return;
111 }
112
113 return handle_local_sccp(link, _msg, &result, sls);
114 }
115
116 /* update the connection state */
Holger Hans Peter Freyther5d930f82010-10-08 23:18:15 +0800117 update_con_state(link, rc, &result, _msg, 0, sls);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800118
119 if (rc == BSS_FILTER_CLEAR_COMPL) {
120 send_local_rlsd(link, &result);
121 } else if (rc == BSS_FILTER_RLC || rc == BSS_FILTER_RLSD) {
122 LOGP(DMSC, LOGL_DEBUG, "Not forwarding RLC/RLSD to the MSC.\n");
123 return;
124 }
125
126
127 msc_send_msg(&bsc, rc, &result, _msg);
128}
129
130/*
131 * handle local message in close down mode
132 */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100133static void handle_local_sccp(struct mtp_link_set *link, struct msgb *inpt, struct sccp_parse_result *result, int sls)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800134{
135 /* Handle msg with a reject */
136 if (inpt->l2h[0] == SCCP_MSG_TYPE_CR) {
137 struct sccp_connection_request *cr;
138 struct msgb *msg;
139
140 LOGP(DINP, LOGL_NOTICE, "Handling CR localy.\n");
141 cr = (struct sccp_connection_request *) inpt->l2h;
142 msg = create_sccp_refuse(&cr->source_local_reference);
143 if (msg) {
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100144 mtp_link_set_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800145 msgb_free(msg);
146 }
147 return;
148 } else if (inpt->l2h[0] == SCCP_MSG_TYPE_DT1 && result->data_len >= 3) {
149 struct active_sccp_con *con;
150 struct sccp_data_form1 *form1;
151 struct msgb *msg;
152
153 if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_CLEAR_COMPLETE) {
154 LOGP(DINP, LOGL_DEBUG, "Received Clear Complete. Sending Release.\n");
155
156 form1 = (struct sccp_data_form1 *) inpt->l2h;
157
158 llist_for_each_entry(con, &bsc.sccp_connections, entry) {
159 if (memcmp(&form1->destination_local_reference,
160 &con->dst_ref, sizeof(con->dst_ref)) == 0) {
161 LOGP(DINP, LOGL_DEBUG, "Sending a release request now.\n");
162 msg = create_sccp_rlsd(&con->dst_ref, &con->src_ref);
163 if (msg) {
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100164 mtp_link_set_submit_sccp_data(link, con->sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800165 msgb_free(msg);
166 }
167 return;
168 }
169 }
170
171 LOGP(DINP, LOGL_ERROR, "Could not find connection for the Clear Command.\n");
172 }
173 } else if (inpt->l2h[0] == SCCP_MSG_TYPE_UDT && result->data_len >= 3) {
174 if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_RESET_ACKNOWLEDGE) {
175 LOGP(DINP, LOGL_NOTICE, "Reset ACK. Connecting to the MSC again.\n");
176 bsc_resources_released(&bsc);
177 return;
178 }
179 }
180
181
182 /* Update the state, maybe the connection was released? */
Holger Hans Peter Freyther5d930f82010-10-08 23:18:15 +0800183 update_con_state(link, 0, result, inpt, 0, sls);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800184 if (llist_empty(&bsc.sccp_connections))
185 bsc_resources_released(&bsc);
186 return;
187}
188
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800189static void clear_connections(struct bsc_data *bsc)
190{
191 struct active_sccp_con *tmp, *con;
192
193 llist_for_each_entry_safe(con, tmp, &bsc->sccp_connections, entry) {
194 free_con(con);
195 }
196
197 bsc->link.clear_queue(&bsc->link);
198}
199
200void bsc_resources_released(struct bsc_data *bsc)
201{
202 bsc_del_timer(&bsc->reset_timeout);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800203}
204
205static void bsc_reset_timeout(void *_data)
206{
207 struct msgb *msg;
208 struct bsc_data *bsc = (struct bsc_data *) _data;
209
210 /* no reset */
211 if (bsc->reset_count > 0) {
212 LOGP(DINP, LOGL_ERROR, "The BSC did not answer the GSM08.08 reset. Restart MTP\n");
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100213 mtp_link_set_stop(bsc->link.the_link);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800214 clear_connections(bsc);
215 bsc->link.reset(&bsc->link);
216 bsc_resources_released(bsc);
217 return;
218 }
219
220 msg = create_reset();
221 if (!msg) {
222 bsc_schedule_timer(&bsc->reset_timeout, 10, 0);
223 return;
224 }
225
226 ++bsc->reset_count;
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100227 mtp_link_set_submit_sccp_data(bsc->link.the_link, -1, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800228 msgb_free(msg);
229 bsc_schedule_timer(&bsc->reset_timeout, 20, 0);
230}
231
232/*
233 * We have lost the connection to the MSC. This is tough. We
234 * can not just bring down the MTP link as this will disable
235 * the BTS radio. We will have to do the following:
236 *
237 * 1.) Bring down all open SCCP connections. As this will close
238 * all radio resources
239 * 2.) Bring down all MGCP endpoints
240 * 3.) Clear the connection data.
241 *
242 * To make things worse we need to buffer the BSC messages... atfer
243 * everything has been sent we will try to connect to the MSC again.
244 *
245 * We will have to veriy that all connections are closed properly..
246 * this means we need to parse response message. In the case the
247 * MTP link is going down while we are sending. We will simply
248 * reconnect to the MSC.
249 */
250void release_bsc_resources(struct bsc_data *bsc)
251{
252 struct active_sccp_con *tmp;
253 struct active_sccp_con *con;
254
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800255 bsc_del_timer(&bsc->reset_timeout);
256
257 /* 2. clear the MGCP endpoints */
258 mgcp_reset(bsc);
259
260 /* 1. send BSSMAP Cleanup.. if we have any connection */
261 llist_for_each_entry_safe(con, tmp, &bsc->sccp_connections, entry) {
262 if (!con->has_dst_ref) {
263 free_con(con);
264 continue;
265 }
266
267 struct msgb *msg = create_clear_command(&con->src_ref);
268 if (!msg)
269 continue;
270
271 /* wait for the clear commands */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100272 mtp_link_set_submit_sccp_data(bsc->link.the_link, con->sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800273 msgb_free(msg);
274 }
275
276 if (llist_empty(&bsc->sccp_connections)) {
277 bsc_resources_released(bsc);
278 } else {
279 /* Send a reset in 20 seconds if we fail to bring everything down */
280 bsc->reset_timeout.cb = bsc_reset_timeout;
281 bsc->reset_timeout.data = bsc;
282 bsc->reset_count = 0;
283 bsc_schedule_timer(&bsc->reset_timeout, 10, 0);
284 }
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800285}
286
287void bsc_link_down(struct link_data *data)
288{
289 int was_up;
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100290 struct mtp_link_set *link = data->the_link;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800291
292 link->available = 0;
293 was_up = link->sccp_up;
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100294 mtp_link_set_stop(link);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800295 clear_connections(data->bsc);
296 mgcp_reset(data->bsc);
297
298 data->clear_queue(data);
299
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +0800300 /* If we have an A link send a reset to the MSC */
301 msc_send_reset(data->bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800302}
303
304void bsc_link_up(struct link_data *data)
305{
306 data->the_link->available = 1;
307
308 /* we have not gone through link down */
Holger Hans Peter Freyther0c95c6a2010-08-07 02:37:43 +0800309 if (data->bsc->msc_link_down) {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800310 clear_connections(data->bsc);
311 bsc_resources_released(data->bsc);
312 }
313
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100314 mtp_link_set_reset(data->the_link);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800315}
316
317/**
318 * update the connection state and helpers below
319 */
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800320static void send_rlc_to_bsc(unsigned int sls, struct sccp_source_reference *src, struct sccp_source_reference *dst)
321{
322 struct msgb *msg;
323
324 msg = create_sccp_rlc(src, dst);
325 if (!msg)
326 return;
327
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100328 mtp_link_set_submit_sccp_data(bsc.link.the_link, sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800329 msgb_free(msg);
330}
331
332static void handle_rlsd(struct sccp_connection_released *rlsd, int from_msc)
333{
334 struct active_sccp_con *con;
335
336 if (from_msc) {
337 /* search for a connection, reverse src/dest for MSC */
338 con = find_con_by_src_dest_ref(&rlsd->destination_local_reference,
339 &rlsd->source_local_reference);
340 if (con) {
341 LOGP(DINP, LOGL_DEBUG, "RLSD conn still alive: local: 0x%x remote: 0x%x\n",
342 sccp_src_ref_to_int(&con->src_ref),
343 sccp_src_ref_to_int(&con->dst_ref));
344 con->released_from_msc = 1;
345 } else {
346 /* send RLC */
347 LOGP(DINP, LOGL_DEBUG, "Sending RLC for MSC: src: 0x%x dst: 0x%x\n",
348 sccp_src_ref_to_int(&rlsd->destination_local_reference),
349 sccp_src_ref_to_int(&rlsd->source_local_reference));
350 msc_send_rlc(&bsc, &rlsd->destination_local_reference,
351 &rlsd->source_local_reference);
352 }
353 } else {
Holger Hans Peter Freyther346e1c42011-01-02 18:11:37 +0100354 unsigned int sls = -1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800355 con = find_con_by_src_dest_ref(&rlsd->source_local_reference,
356 &rlsd->destination_local_reference);
357 if (con) {
358 LOGP(DINP, LOGL_DEBUG, "Timeout on BSC. Sending RLC. src: 0x%x\n",
359 sccp_src_ref_to_int(&rlsd->source_local_reference));
360
361 if (con->released_from_msc)
362 msc_send_rlc(&bsc, &con->src_ref, &con->dst_ref);
363 sls = con->sls;
364 free_con(con);
365 } else {
366 LOGP(DINP, LOGL_ERROR, "Timeout on BSC for unknown connection. src: 0x%x\n",
367 sccp_src_ref_to_int(&rlsd->source_local_reference));
368 }
369
370 /* now send a rlc back to the BSC */
371 send_rlc_to_bsc(sls, &rlsd->destination_local_reference, &rlsd->source_local_reference);
372 }
373}
374
375/**
376 * Update connection state and also send message.....
377 *
378 * RLSD from MSC:
379 * 1.) We don't find the entry in this case we will send a
380 * forged RLC to the MSC and we are done.
381 * 2.) We find an entry in this we will need to register that
382 * we need to send a RLC and we are done for now.
383 * RLSD from BSC:
384 * 1.) This is an error we are ignoring for now.
385 * RLC from BSC:
386 * 1.) We are destroying the connection, we might send a RLC to
387 * the MSC if we are waiting for one.
388 */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100389void update_con_state(struct mtp_link_set *link, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800390{
391 struct active_sccp_con *con;
392 struct sccp_connection_request *cr;
393 struct sccp_connection_confirm *cc;
394 struct sccp_connection_release_complete *rlc;
395 struct sccp_connection_refused *cref;
396
397 /* was the header okay? */
398 if (rc < 0)
399 return;
400
401 /* the header was size checked */
402 switch (msg->l2h[0]) {
403 case SCCP_MSG_TYPE_CR:
404 if (from_msc) {
405 LOGP(DMSC, LOGL_ERROR, "CR from MSC is not handled.\n");
406 return;
407 }
408
409 cr = (struct sccp_connection_request *) msg->l2h;
410 con = find_con_by_src_ref(&cr->source_local_reference);
411 if (con) {
412 LOGP(DINP, LOGL_ERROR, "Duplicate SRC reference for: 0x%x. Reusing\n",
413 sccp_src_ref_to_int(&con->src_ref));
414 free_con(con);
415 }
416
417 con = talloc_zero(NULL, struct active_sccp_con);
418 if (!con) {
419 LOGP(DINP, LOGL_ERROR, "Failed to allocate\n");
420 return;
421 }
422
423 con->src_ref = cr->source_local_reference;
424 con->sls = sls;
Holger Hans Peter Freyther5d930f82010-10-08 23:18:15 +0800425 con->link = link;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800426 llist_add_tail(&con->entry, &bsc.sccp_connections);
427 LOGP(DINP, LOGL_DEBUG, "Adding CR: local ref: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
428 break;
429 case SCCP_MSG_TYPE_CC:
430 if (!from_msc) {
431 LOGP(DINP, LOGL_ERROR, "CC from BSC is not handled.\n");
432 return;
433 }
434
435 cc = (struct sccp_connection_confirm *) msg->l2h;
436 con = find_con_by_src_ref(&cc->destination_local_reference);
437 if (con) {
438 con->dst_ref = cc->source_local_reference;
439 con->has_dst_ref = 1;
440 LOGP(DINP, LOGL_DEBUG, "Updating CC: local: 0x%x remote: 0x%x\n",
441 sccp_src_ref_to_int(&con->src_ref), sccp_src_ref_to_int(&con->dst_ref));
442 return;
443 }
444
445 LOGP(DINP, LOGL_ERROR, "CCed connection can not be found: 0x%x\n",
446 sccp_src_ref_to_int(&cc->destination_local_reference));
447 break;
448 case SCCP_MSG_TYPE_CREF:
449 if (!from_msc) {
450 LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n");
451 return;
452 }
453
454 cref = (struct sccp_connection_refused *) msg->l2h;
455 con = find_con_by_src_ref(&cref->destination_local_reference);
456 if (con) {
457 LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
458 free_con(con);
459 return;
460 }
461
462 LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n");
463 break;
464 case SCCP_MSG_TYPE_RLSD:
465 handle_rlsd((struct sccp_connection_released *) msg->l2h, from_msc);
466 break;
467 case SCCP_MSG_TYPE_RLC:
468 if (from_msc) {
469 LOGP(DINP, LOGL_ERROR, "RLC from MSC is wrong.\n");
470 return;
471 }
472
473 rlc = (struct sccp_connection_release_complete *) msg->l2h;
474 con = find_con_by_src_dest_ref(&rlc->source_local_reference,
475 &rlc->destination_local_reference);
476 if (con) {
477 LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
478 if (con->released_from_msc)
479 msc_send_rlc(&bsc, &con->src_ref, &con->dst_ref);
480 free_con(con);
481 return;
482 }
483
484 LOGP(DINP, LOGL_ERROR, "RLC can not be found. 0x%x 0x%x\n",
485 sccp_src_ref_to_int(&rlc->source_local_reference),
486 sccp_src_ref_to_int(&rlc->destination_local_reference));
487 break;
488 }
489}
490
491static void send_local_rlsd_for_con(void *data)
492{
493 struct msgb *rlsd;
494 struct active_sccp_con *con = (struct active_sccp_con *) data;
495
496 /* try again in three seconds */
497 con->rlc_timeout.data = con;
498 con->rlc_timeout.cb = send_local_rlsd_for_con;
499 bsc_schedule_timer(&con->rlc_timeout, 3, 0);
500
501 /* we send this to the BSC so we need to switch src and dest */
502 rlsd = create_sccp_rlsd(&con->dst_ref, &con->src_ref);
503 if (!rlsd)
504 return;
505
506 ++con->rls_tries;
507 LOGP(DINP, LOGL_DEBUG, "Sending RLSD for 0x%x the %d time.\n",
508 sccp_src_ref_to_int(&con->src_ref), con->rls_tries);
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100509 mtp_link_set_submit_sccp_data(bsc.link.the_link, con->sls, rlsd->l2h, msgb_l2len(rlsd));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800510 msgb_free(rlsd);
511}
512
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100513static void send_local_rlsd(struct mtp_link_set *link, struct sccp_parse_result *res)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800514{
515 struct active_sccp_con *con;
516
517 LOGP(DINP, LOGL_DEBUG, "Received GSM Clear Complete. Sending RLSD locally.\n");
518
519 con = find_con_by_dest_ref(res->destination_local_reference);
520 if (!con)
521 return;
522 con->rls_tries = 0;
523 send_local_rlsd_for_con(con);
524}
525
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100526static void send_reset_ack(struct mtp_link_set *link, int sls)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800527{
Holger Hans Peter Freyther5aa17012010-07-31 04:37:26 +0800528 static const uint8_t reset_ack[] = {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800529 0x09, 0x00, 0x03, 0x05, 0x7, 0x02, 0x42, 0xfe,
530 0x02, 0x42, 0xfe, 0x03,
531 0x00, 0x01, 0x31
532 };
533
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100534 mtp_link_set_submit_sccp_data(link, sls, reset_ack, sizeof(reset_ack));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800535}
536
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800537static void print_usage()
538{
539 printf("Usage: cellmgr_ng\n");
540}
541
542static void sigint()
543{
544 static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
545 static int handled = 0;
546
547 /* failed to lock */
548 if (pthread_mutex_trylock(&exit_mutex) != 0)
549 return;
550 if (handled)
551 goto out;
552
553 printf("Terminating.\n");
554 handled = 1;
555 if (bsc.setup)
556 bsc.link.shutdown(&bsc.link);
557 exit(0);
558
559out:
560 pthread_mutex_unlock(&exit_mutex);
561}
562
563static void sigusr2()
564{
565 printf("Closing the MSC connection on demand.\n");
Holger Hans Peter Freyther43d9eec2010-08-07 01:54:19 +0800566 msc_close_connection(&bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800567}
568
569static void print_help()
570{
571 printf(" Some useful help...\n");
572 printf(" -h --help this text\n");
573 printf(" -c --config=CFG The config file to use.\n");
574 printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n");
575 printf(" -c --once. Send the SLTM msg only once.\n");
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +0800576 printf(" -v --version. Print the version number\n");
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800577}
578
579static void handle_options(int argc, char **argv)
580{
581 while (1) {
582 int option_index = 0, c;
583 static struct option long_options[] = {
584 {"help", 0, 0, 'h'},
585 {"config", 1, 0, 'c'},
586 {"pcap", 1, 0, 'p'},
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +0800587 {"version", 0, 0, 0},
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800588 {0, 0, 0, 0},
589 };
590
Holger Hans Peter Freythereefd72c2010-12-17 16:22:31 +0100591 c = getopt_long(argc, argv, "hc:p:v",
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800592 long_options, &option_index);
593 if (c == -1)
594 break;
595
596 switch (c) {
597 case 'h':
598 print_usage();
599 print_help();
600 exit(0);
601 case 'p':
602 if (bsc.link.pcap_fd >= 0)
603 close(bsc.link.pcap_fd);
604 bsc.link.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
605 if (bsc.link.pcap_fd < 0) {
606 fprintf(stderr, "Failed to open PCAP file.\n");
607 exit(0);
608 }
609 mtp_pcap_write_header(bsc.link.pcap_fd);
610 break;
611 case 'c':
612 config = optarg;
613 break;
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +0800614 case 'v':
615 printf("This is %s version %s.\n", PACKAGE, VERSION);
616 exit(0);
617 break;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800618 default:
619 fprintf(stderr, "Unknown option.\n");
620 break;
621 }
622 }
623}
624
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800625int main(int argc, char **argv)
626{
Holger Hans Peter Freytherfdae5c92010-09-30 01:01:03 +0800627 int rc;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800628 INIT_LLIST_HEAD(&bsc.sccp_connections);
629
Holger Hans Peter Freyther7942abc2010-09-30 00:34:46 +0800630 bsc.dpc = 1;
631 bsc.opc = 0;
Holger Hans Peter Freyther7a725562011-01-01 13:34:58 +0100632 bsc.sccp_opc = -1;
Holger Hans Peter Freyther7942abc2010-09-30 00:34:46 +0800633 bsc.udp_port = 3456;
634 bsc.udp_ip = NULL;
635 bsc.src_port = 1313;
Holger Hans Peter Freytherb38b33b2010-11-26 21:21:04 +0100636 bsc.ni_ni = MTP_NI_NATION_NET;
637 bsc.ni_spare = 0;
Holger Hans Peter Freyther7942abc2010-09-30 00:34:46 +0800638
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100639 mtp_link_set_init();
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800640 thread_init();
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800641
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +0800642 log_init(&log_info);
643 stderr_target = log_target_create_stderr();
644 log_add_target(stderr_target);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800645
646 /* enable filters */
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +0800647 log_set_all_filter(stderr_target, 1);
648 log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO);
649 log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO);
650 log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO);
651 log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO);
652 log_set_print_timestamp(stderr_target, 1);
653 log_set_use_color(stderr_target, 0);
654
655 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800656
657 bsc.setup = 0;
658 bsc.msc_address = "127.0.0.1";
659 bsc.link.pcap_fd = -1;
660 bsc.link.udp.reset_timeout = 180;
661 bsc.ping_time = 20;
662 bsc.pong_time = 5;
663 bsc.msc_time = 20;
664
665 handle_options(argc, argv);
666
667 signal(SIGPIPE, SIG_IGN);
668 signal(SIGINT, sigint);
669 signal(SIGUSR2, sigusr2);
670 srand(time(NULL));
671
672 cell_vty_init();
Holger Hans Peter Freythercbf7d182010-07-31 05:25:35 +0800673 if (vty_read_config_file(config, NULL) < 0) {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800674 fprintf(stderr, "Failed to read the VTY config.\n");
675 return -1;
676 }
677
Holger Hans Peter Freytherfdae5c92010-09-30 01:01:03 +0800678 rc = telnet_init(NULL, NULL, 4242);
679 if (rc < 0)
680 return rc;
681
Holger Hans Peter Freythera99b04b2011-01-02 11:23:54 +0100682 if (link_init(&bsc) != 0)
683 return -1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800684
685 while (1) {
686 bsc_select_main(0);
687 }
688
689 return 0;
690}
691