blob: 6e33c9c205eddefadb18f97f81ac4a31790ac70c [file] [log] [blame]
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08001/* Bloated main routine, refactor */
2/*
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +01003 * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010-2011 by On-Waves
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +08005 * 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
Holger Hans Peter Freyther1b6901e2011-01-17 16:13:28 +0100130void mtp_link_set_forward_isup(struct mtp_link_set *set, struct msgb *msg, int sls)
131{
132 LOGP(DINP, LOGL_ERROR, "ISUP is not handled.\n");
133}
134
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800135/*
136 * handle local message in close down mode
137 */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100138static 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 +0800139{
140 /* Handle msg with a reject */
141 if (inpt->l2h[0] == SCCP_MSG_TYPE_CR) {
142 struct sccp_connection_request *cr;
143 struct msgb *msg;
144
145 LOGP(DINP, LOGL_NOTICE, "Handling CR localy.\n");
146 cr = (struct sccp_connection_request *) inpt->l2h;
147 msg = create_sccp_refuse(&cr->source_local_reference);
148 if (msg) {
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100149 mtp_link_set_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800150 msgb_free(msg);
151 }
152 return;
153 } else if (inpt->l2h[0] == SCCP_MSG_TYPE_DT1 && result->data_len >= 3) {
154 struct active_sccp_con *con;
155 struct sccp_data_form1 *form1;
156 struct msgb *msg;
157
158 if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_CLEAR_COMPLETE) {
159 LOGP(DINP, LOGL_DEBUG, "Received Clear Complete. Sending Release.\n");
160
161 form1 = (struct sccp_data_form1 *) inpt->l2h;
162
163 llist_for_each_entry(con, &bsc.sccp_connections, entry) {
164 if (memcmp(&form1->destination_local_reference,
165 &con->dst_ref, sizeof(con->dst_ref)) == 0) {
166 LOGP(DINP, LOGL_DEBUG, "Sending a release request now.\n");
167 msg = create_sccp_rlsd(&con->dst_ref, &con->src_ref);
168 if (msg) {
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100169 mtp_link_set_submit_sccp_data(link, con->sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800170 msgb_free(msg);
171 }
172 return;
173 }
174 }
175
176 LOGP(DINP, LOGL_ERROR, "Could not find connection for the Clear Command.\n");
177 }
178 } else if (inpt->l2h[0] == SCCP_MSG_TYPE_UDT && result->data_len >= 3) {
179 if (inpt->l3h[0] == 0 && inpt->l3h[2] == BSS_MAP_MSG_RESET_ACKNOWLEDGE) {
180 LOGP(DINP, LOGL_NOTICE, "Reset ACK. Connecting to the MSC again.\n");
181 bsc_resources_released(&bsc);
182 return;
183 }
184 }
185
186
187 /* Update the state, maybe the connection was released? */
Holger Hans Peter Freyther5d930f82010-10-08 23:18:15 +0800188 update_con_state(link, 0, result, inpt, 0, sls);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800189 if (llist_empty(&bsc.sccp_connections))
190 bsc_resources_released(&bsc);
191 return;
192}
193
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800194static void clear_connections(struct bsc_data *bsc)
195{
196 struct active_sccp_con *tmp, *con;
197
198 llist_for_each_entry_safe(con, tmp, &bsc->sccp_connections, entry) {
199 free_con(con);
200 }
201
Holger Hans Peter Freytherfe72c162011-01-04 13:21:52 +0100202 link_clear_all(bsc->link_set);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800203}
204
205void bsc_resources_released(struct bsc_data *bsc)
206{
207 bsc_del_timer(&bsc->reset_timeout);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800208}
209
210static void bsc_reset_timeout(void *_data)
211{
212 struct msgb *msg;
213 struct bsc_data *bsc = (struct bsc_data *) _data;
214
215 /* no reset */
216 if (bsc->reset_count > 0) {
217 LOGP(DINP, LOGL_ERROR, "The BSC did not answer the GSM08.08 reset. Restart MTP\n");
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100218 mtp_link_set_stop(bsc->link_set);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800219 clear_connections(bsc);
Holger Hans Peter Freytherfe72c162011-01-04 13:21:52 +0100220 link_reset_all(bsc->link_set);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800221 bsc_resources_released(bsc);
222 return;
223 }
224
225 msg = create_reset();
226 if (!msg) {
227 bsc_schedule_timer(&bsc->reset_timeout, 10, 0);
228 return;
229 }
230
231 ++bsc->reset_count;
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100232 mtp_link_set_submit_sccp_data(bsc->link_set, -1, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800233 msgb_free(msg);
234 bsc_schedule_timer(&bsc->reset_timeout, 20, 0);
235}
236
237/*
238 * We have lost the connection to the MSC. This is tough. We
239 * can not just bring down the MTP link as this will disable
240 * the BTS radio. We will have to do the following:
241 *
242 * 1.) Bring down all open SCCP connections. As this will close
243 * all radio resources
244 * 2.) Bring down all MGCP endpoints
245 * 3.) Clear the connection data.
246 *
247 * To make things worse we need to buffer the BSC messages... atfer
248 * everything has been sent we will try to connect to the MSC again.
249 *
250 * We will have to veriy that all connections are closed properly..
251 * this means we need to parse response message. In the case the
252 * MTP link is going down while we are sending. We will simply
253 * reconnect to the MSC.
254 */
255void release_bsc_resources(struct bsc_data *bsc)
256{
257 struct active_sccp_con *tmp;
258 struct active_sccp_con *con;
259
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800260 bsc_del_timer(&bsc->reset_timeout);
261
262 /* 2. clear the MGCP endpoints */
263 mgcp_reset(bsc);
264
265 /* 1. send BSSMAP Cleanup.. if we have any connection */
266 llist_for_each_entry_safe(con, tmp, &bsc->sccp_connections, entry) {
267 if (!con->has_dst_ref) {
268 free_con(con);
269 continue;
270 }
271
272 struct msgb *msg = create_clear_command(&con->src_ref);
273 if (!msg)
274 continue;
275
276 /* wait for the clear commands */
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100277 mtp_link_set_submit_sccp_data(bsc->link_set, con->sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800278 msgb_free(msg);
279 }
280
281 if (llist_empty(&bsc->sccp_connections)) {
282 bsc_resources_released(bsc);
283 } else {
284 /* Send a reset in 20 seconds if we fail to bring everything down */
285 bsc->reset_timeout.cb = bsc_reset_timeout;
286 bsc->reset_timeout.data = bsc;
287 bsc->reset_count = 0;
288 bsc_schedule_timer(&bsc->reset_timeout, 10, 0);
289 }
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800290}
291
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100292void mtp_linkset_down(struct mtp_link_set *set)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800293{
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100294 set->available = 0;
295 mtp_link_set_stop(set);
296 clear_connections(set->bsc);
297 mgcp_reset(set->bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800298
Holger Hans Peter Freyther7b7c2972010-08-07 05:41:06 +0800299 /* If we have an A link send a reset to the MSC */
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100300 msc_send_reset(set->bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800301}
302
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100303void mtp_linkset_up(struct mtp_link_set *set)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800304{
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100305 set->available = 1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800306
307 /* we have not gone through link down */
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100308 if (set->bsc->msc_link_down) {
309 clear_connections(set->bsc);
310 bsc_resources_released(set->bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800311 }
312
Holger Hans Peter Freytherc8405692011-01-02 20:24:08 +0100313 mtp_link_set_reset(set);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800314}
315
316/**
317 * update the connection state and helpers below
318 */
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800319static void send_rlc_to_bsc(unsigned int sls, struct sccp_source_reference *src, struct sccp_source_reference *dst)
320{
321 struct msgb *msg;
322
323 msg = create_sccp_rlc(src, dst);
324 if (!msg)
325 return;
326
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100327 mtp_link_set_submit_sccp_data(bsc.link_set, sls, msg->l2h, msgb_l2len(msg));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800328 msgb_free(msg);
329}
330
331static void handle_rlsd(struct sccp_connection_released *rlsd, int from_msc)
332{
333 struct active_sccp_con *con;
334
335 if (from_msc) {
336 /* search for a connection, reverse src/dest for MSC */
337 con = find_con_by_src_dest_ref(&rlsd->destination_local_reference,
338 &rlsd->source_local_reference);
339 if (con) {
340 LOGP(DINP, LOGL_DEBUG, "RLSD conn still alive: local: 0x%x remote: 0x%x\n",
341 sccp_src_ref_to_int(&con->src_ref),
342 sccp_src_ref_to_int(&con->dst_ref));
343 con->released_from_msc = 1;
344 } else {
345 /* send RLC */
346 LOGP(DINP, LOGL_DEBUG, "Sending RLC for MSC: src: 0x%x dst: 0x%x\n",
347 sccp_src_ref_to_int(&rlsd->destination_local_reference),
348 sccp_src_ref_to_int(&rlsd->source_local_reference));
349 msc_send_rlc(&bsc, &rlsd->destination_local_reference,
350 &rlsd->source_local_reference);
351 }
352 } else {
Holger Hans Peter Freyther346e1c42011-01-02 18:11:37 +0100353 unsigned int sls = -1;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800354 con = find_con_by_src_dest_ref(&rlsd->source_local_reference,
355 &rlsd->destination_local_reference);
356 if (con) {
357 LOGP(DINP, LOGL_DEBUG, "Timeout on BSC. Sending RLC. src: 0x%x\n",
358 sccp_src_ref_to_int(&rlsd->source_local_reference));
359
360 if (con->released_from_msc)
361 msc_send_rlc(&bsc, &con->src_ref, &con->dst_ref);
362 sls = con->sls;
363 free_con(con);
364 } else {
365 LOGP(DINP, LOGL_ERROR, "Timeout on BSC for unknown connection. src: 0x%x\n",
366 sccp_src_ref_to_int(&rlsd->source_local_reference));
367 }
368
369 /* now send a rlc back to the BSC */
370 send_rlc_to_bsc(sls, &rlsd->destination_local_reference, &rlsd->source_local_reference);
371 }
372}
373
374/**
375 * Update connection state and also send message.....
376 *
377 * RLSD from MSC:
378 * 1.) We don't find the entry in this case we will send a
379 * forged RLC to the MSC and we are done.
380 * 2.) We find an entry in this we will need to register that
381 * we need to send a RLC and we are done for now.
382 * RLSD from BSC:
383 * 1.) This is an error we are ignoring for now.
384 * RLC from BSC:
385 * 1.) We are destroying the connection, we might send a RLC to
386 * the MSC if we are waiting for one.
387 */
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100388void 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 +0800389{
390 struct active_sccp_con *con;
391 struct sccp_connection_request *cr;
392 struct sccp_connection_confirm *cc;
393 struct sccp_connection_release_complete *rlc;
394 struct sccp_connection_refused *cref;
395
396 /* was the header okay? */
397 if (rc < 0)
398 return;
399
400 /* the header was size checked */
401 switch (msg->l2h[0]) {
402 case SCCP_MSG_TYPE_CR:
403 if (from_msc) {
404 LOGP(DMSC, LOGL_ERROR, "CR from MSC is not handled.\n");
405 return;
406 }
407
408 cr = (struct sccp_connection_request *) msg->l2h;
409 con = find_con_by_src_ref(&cr->source_local_reference);
410 if (con) {
411 LOGP(DINP, LOGL_ERROR, "Duplicate SRC reference for: 0x%x. Reusing\n",
412 sccp_src_ref_to_int(&con->src_ref));
413 free_con(con);
414 }
415
416 con = talloc_zero(NULL, struct active_sccp_con);
417 if (!con) {
418 LOGP(DINP, LOGL_ERROR, "Failed to allocate\n");
419 return;
420 }
421
422 con->src_ref = cr->source_local_reference;
423 con->sls = sls;
Holger Hans Peter Freyther5d930f82010-10-08 23:18:15 +0800424 con->link = link;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800425 llist_add_tail(&con->entry, &bsc.sccp_connections);
426 LOGP(DINP, LOGL_DEBUG, "Adding CR: local ref: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
427 break;
428 case SCCP_MSG_TYPE_CC:
429 if (!from_msc) {
430 LOGP(DINP, LOGL_ERROR, "CC from BSC is not handled.\n");
431 return;
432 }
433
434 cc = (struct sccp_connection_confirm *) msg->l2h;
435 con = find_con_by_src_ref(&cc->destination_local_reference);
436 if (con) {
437 con->dst_ref = cc->source_local_reference;
438 con->has_dst_ref = 1;
439 LOGP(DINP, LOGL_DEBUG, "Updating CC: local: 0x%x remote: 0x%x\n",
440 sccp_src_ref_to_int(&con->src_ref), sccp_src_ref_to_int(&con->dst_ref));
441 return;
442 }
443
444 LOGP(DINP, LOGL_ERROR, "CCed connection can not be found: 0x%x\n",
445 sccp_src_ref_to_int(&cc->destination_local_reference));
446 break;
447 case SCCP_MSG_TYPE_CREF:
448 if (!from_msc) {
449 LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n");
450 return;
451 }
452
453 cref = (struct sccp_connection_refused *) msg->l2h;
454 con = find_con_by_src_ref(&cref->destination_local_reference);
455 if (con) {
456 LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
457 free_con(con);
458 return;
459 }
460
461 LOGP(DINP, LOGL_ERROR, "CREF from BSC is not handled.\n");
462 break;
463 case SCCP_MSG_TYPE_RLSD:
464 handle_rlsd((struct sccp_connection_released *) msg->l2h, from_msc);
465 break;
466 case SCCP_MSG_TYPE_RLC:
467 if (from_msc) {
468 LOGP(DINP, LOGL_ERROR, "RLC from MSC is wrong.\n");
469 return;
470 }
471
472 rlc = (struct sccp_connection_release_complete *) msg->l2h;
473 con = find_con_by_src_dest_ref(&rlc->source_local_reference,
474 &rlc->destination_local_reference);
475 if (con) {
476 LOGP(DINP, LOGL_DEBUG, "Releasing local: 0x%x\n", sccp_src_ref_to_int(&con->src_ref));
477 if (con->released_from_msc)
478 msc_send_rlc(&bsc, &con->src_ref, &con->dst_ref);
479 free_con(con);
480 return;
481 }
482
483 LOGP(DINP, LOGL_ERROR, "RLC can not be found. 0x%x 0x%x\n",
484 sccp_src_ref_to_int(&rlc->source_local_reference),
485 sccp_src_ref_to_int(&rlc->destination_local_reference));
486 break;
487 }
488}
489
490static void send_local_rlsd_for_con(void *data)
491{
492 struct msgb *rlsd;
493 struct active_sccp_con *con = (struct active_sccp_con *) data;
494
495 /* try again in three seconds */
496 con->rlc_timeout.data = con;
497 con->rlc_timeout.cb = send_local_rlsd_for_con;
498 bsc_schedule_timer(&con->rlc_timeout, 3, 0);
499
500 /* we send this to the BSC so we need to switch src and dest */
501 rlsd = create_sccp_rlsd(&con->dst_ref, &con->src_ref);
502 if (!rlsd)
503 return;
504
505 ++con->rls_tries;
506 LOGP(DINP, LOGL_DEBUG, "Sending RLSD for 0x%x the %d time.\n",
507 sccp_src_ref_to_int(&con->src_ref), con->rls_tries);
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100508 mtp_link_set_submit_sccp_data(bsc.link_set, con->sls, rlsd->l2h, msgb_l2len(rlsd));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800509 msgb_free(rlsd);
510}
511
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100512static void send_local_rlsd(struct mtp_link_set *link, struct sccp_parse_result *res)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800513{
514 struct active_sccp_con *con;
515
516 LOGP(DINP, LOGL_DEBUG, "Received GSM Clear Complete. Sending RLSD locally.\n");
517
518 con = find_con_by_dest_ref(res->destination_local_reference);
519 if (!con)
520 return;
521 con->rls_tries = 0;
522 send_local_rlsd_for_con(con);
523}
524
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100525static void send_reset_ack(struct mtp_link_set *link, int sls)
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800526{
Holger Hans Peter Freyther5aa17012010-07-31 04:37:26 +0800527 static const uint8_t reset_ack[] = {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800528 0x09, 0x00, 0x03, 0x05, 0x7, 0x02, 0x42, 0xfe,
529 0x02, 0x42, 0xfe, 0x03,
530 0x00, 0x01, 0x31
531 };
532
Holger Hans Peter Freyther569f1e12011-01-02 18:47:49 +0100533 mtp_link_set_submit_sccp_data(link, sls, reset_ack, sizeof(reset_ack));
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800534}
535
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800536static void print_usage()
537{
538 printf("Usage: cellmgr_ng\n");
539}
540
541static void sigint()
542{
543 static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
544 static int handled = 0;
545
546 /* failed to lock */
547 if (pthread_mutex_trylock(&exit_mutex) != 0)
548 return;
549 if (handled)
550 goto out;
551
552 printf("Terminating.\n");
553 handled = 1;
554 if (bsc.setup)
Holger Hans Peter Freytherfe72c162011-01-04 13:21:52 +0100555 link_shutdown_all(bsc.link_set);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800556 exit(0);
557
558out:
559 pthread_mutex_unlock(&exit_mutex);
560}
561
562static void sigusr2()
563{
564 printf("Closing the MSC connection on demand.\n");
Holger Hans Peter Freyther43d9eec2010-08-07 01:54:19 +0800565 msc_close_connection(&bsc);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800566}
567
568static void print_help()
569{
570 printf(" Some useful help...\n");
571 printf(" -h --help this text\n");
572 printf(" -c --config=CFG The config file to use.\n");
573 printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n");
574 printf(" -c --once. Send the SLTM msg only once.\n");
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +0800575 printf(" -v --version. Print the version number\n");
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800576}
577
578static void handle_options(int argc, char **argv)
579{
580 while (1) {
581 int option_index = 0, c;
582 static struct option long_options[] = {
583 {"help", 0, 0, 'h'},
584 {"config", 1, 0, 'c'},
585 {"pcap", 1, 0, 'p'},
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +0800586 {"version", 0, 0, 0},
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800587 {0, 0, 0, 0},
588 };
589
Holger Hans Peter Freythereefd72c2010-12-17 16:22:31 +0100590 c = getopt_long(argc, argv, "hc:p:v",
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800591 long_options, &option_index);
592 if (c == -1)
593 break;
594
595 switch (c) {
596 case 'h':
597 print_usage();
598 print_help();
599 exit(0);
600 case 'p':
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100601 if (bsc.pcap_fd >= 0)
602 close(bsc.pcap_fd);
603 bsc.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
604 if (bsc.pcap_fd < 0) {
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800605 fprintf(stderr, "Failed to open PCAP file.\n");
606 exit(0);
607 }
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100608 mtp_pcap_write_header(bsc.pcap_fd);
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800609 break;
610 case 'c':
611 config = optarg;
612 break;
Holger Hans Peter Freyther4bbfa272010-09-15 20:53:56 +0800613 case 'v':
614 printf("This is %s version %s.\n", PACKAGE, VERSION);
615 exit(0);
616 break;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800617 default:
618 fprintf(stderr, "Unknown option.\n");
619 break;
620 }
621 }
622}
623
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800624int main(int argc, char **argv)
625{
Holger Hans Peter Freytherfdae5c92010-09-30 01:01:03 +0800626 int rc;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800627 INIT_LLIST_HEAD(&bsc.sccp_connections);
628
Holger Hans Peter Freyther7942abc2010-09-30 00:34:46 +0800629 bsc.dpc = 1;
630 bsc.opc = 0;
Holger Hans Peter Freyther7a725562011-01-01 13:34:58 +0100631 bsc.sccp_opc = -1;
Holger Hans Peter Freytherd8a73e22011-01-17 22:37:11 +0100632 bsc.isup_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";
Holger Hans Peter Freyther644aafb2011-01-03 23:51:07 +0100659 bsc.pcap_fd = -1;
660 bsc.udp_reset_timeout = 180;
Holger Hans Peter Freyther97f66e22010-07-28 03:32:52 +0800661 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