blob: 0b07da7dd8b57c8782df725f7e5027ceb7e45f41 [file] [log] [blame]
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +01001/* Relay UDT/all SCCP messages */
2/*
3 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2010 by On-Waves
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <mtp_data.h>
24#include <mtp_pcap.h>
25#include <thread.h>
26#include <bsc_data.h>
27#include <snmp_mtp.h>
28#include <cellmgr_debug.h>
29
30#include <osmocore/talloc.h>
31
32#include <osmocom/vty/vty.h>
33#include <osmocom/vty/telnet_interface.h>
34
35#include <sys/stat.h>
36#include <sys/types.h>
37
38#include <fcntl.h>
39#include <signal.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <assert.h>
44#include <unistd.h>
45
46#ifndef _GNU_SOURCE
47#define _GNU_SOURCE
48#endif
49#include <getopt.h>
50
51#undef PACKAGE_NAME
52#undef PACKAGE_VERSION
53#undef PACKAGE_BUGREPORT
54#undef PACKAGE_TARNAME
55#undef PACKAGE_STRING
56#include <cellmgr_config.h>
57
58static struct log_target *stderr_target;
59
60static char *config = "udt_relay.cfg";
61
62struct bsc_data bsc;
63extern void cell_vty_init(void);
64
65int link_c7_init(struct link_data *data) __attribute__((__weak__));
66
67int link_c7_init(struct link_data *data)
68{
69 return -1;
70}
71
72/*
73 * methods called from the MTP Level3 part
74 */
75void mtp_link_submit(struct mtp_link *link, struct msgb *msg)
76{
77 bsc.link.write(&bsc.link, msg);
78}
79
80void mtp_link_restart(struct mtp_link *link)
81{
82 LOGP(DINP, LOGL_ERROR, "Need to restart the SS7 link.\n");
83 bsc.link.reset(&bsc.link);
84}
85
86void mtp_link_sccp_down(struct mtp_link *link)
87{
88 msc_clear_queue(&bsc);
89}
90
91void mtp_link_forward_sccp(struct mtp_link *link, struct msgb *_msg, int sls)
92{
93 msc_send_direct(&bsc, _msg);
94}
95
96void bsc_link_down(struct link_data *data)
97{
98 int was_up;
99 struct mtp_link *link = data->the_link;
100
101 link->available = 0;
102 was_up = link->sccp_up;
103 mtp_link_stop(link);
104
105 data->clear_queue(data);
106
107 /* clear pending messages from the MSC */
108 msc_clear_queue(data->bsc);
109
110 /* If we have an A link send a reset to the MSC */
111 msc_send_reset(data->bsc);
112}
113
114void bsc_link_up(struct link_data *data)
115{
116 data->the_link->available = 1;
117 mtp_link_reset(data->the_link);
118}
119
120static void print_usage()
121{
122 printf("Usage: cellmgr_ng\n");
123}
124
125static void sigint()
126{
127 static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
128 static int handled = 0;
129
130 /* failed to lock */
131 if (pthread_mutex_trylock(&exit_mutex) != 0)
132 return;
133 if (handled)
134 goto out;
135
136 printf("Terminating.\n");
137 handled = 1;
138 if (bsc.setup)
139 bsc.link.shutdown(&bsc.link);
140 exit(0);
141
142out:
143 pthread_mutex_unlock(&exit_mutex);
144}
145
146static void sigusr2()
147{
148 printf("Closing the MSC connection on demand.\n");
149 msc_close_connection(&bsc);
150}
151
152static void print_help()
153{
154 printf(" Some useful help...\n");
155 printf(" -h --help this text\n");
156 printf(" -c --config=CFG The config file to use.\n");
157 printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n");
158 printf(" -c --once. Send the SLTM msg only once.\n");
159 printf(" -v --version. Print the version number\n");
160}
161
162static void handle_options(int argc, char **argv)
163{
164 while (1) {
165 int option_index = 0, c;
166 static struct option long_options[] = {
167 {"help", 0, 0, 'h'},
168 {"config", 1, 0, 'c'},
169 {"pcap", 1, 0, 'p'},
170 {"version", 0, 0, 0},
171 {0, 0, 0, 0},
172 };
173
174 c = getopt_long(argc, argv, "hc:p:v",
175 long_options, &option_index);
176 if (c == -1)
177 break;
178
179 switch (c) {
180 case 'h':
181 print_usage();
182 print_help();
183 exit(0);
184 case 'p':
185 if (bsc.link.pcap_fd >= 0)
186 close(bsc.link.pcap_fd);
187 bsc.link.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH);
188 if (bsc.link.pcap_fd < 0) {
189 fprintf(stderr, "Failed to open PCAP file.\n");
190 exit(0);
191 }
192 mtp_pcap_write_header(bsc.link.pcap_fd);
193 break;
194 case 'c':
195 config = optarg;
196 break;
197 case 'v':
198 printf("This is %s version %s.\n", PACKAGE, VERSION);
199 exit(0);
200 break;
201 default:
202 fprintf(stderr, "Unknown option.\n");
203 break;
204 }
205 }
206}
207
208static void start_rest(void *start)
209{
210 bsc.setup = 1;
211
212 if (msc_init(&bsc, 0) != 0) {
213 fprintf(stderr, "Failed to init MSC part.\n");
214 exit(3);
215 }
216
217 bsc.link.start(&bsc.link);
218}
219
220
221int main(int argc, char **argv)
222{
223 int rc;
224 INIT_LLIST_HEAD(&bsc.sccp_connections);
225
226 bsc.dpc = 1;
227 bsc.opc = 0;
228 bsc.udp_port = 3456;
229 bsc.udp_ip = NULL;
230 bsc.src_port = 1313;
231
232 mtp_link_init();
233 thread_init();
234
235 log_init(&log_info);
236 stderr_target = log_target_create_stderr();
237 log_add_target(stderr_target);
238
239 /* enable filters */
240 log_set_all_filter(stderr_target, 1);
241 log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO);
242 log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO);
243 log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO);
244 log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO);
245 log_set_print_timestamp(stderr_target, 1);
246 log_set_use_color(stderr_target, 0);
247
248 sccp_set_log_area(DSCCP);
249
250 bsc.setup = 0;
251 bsc.msc_address = "127.0.0.1";
252 bsc.link.pcap_fd = -1;
253 bsc.link.udp.reset_timeout = 180;
254 bsc.ping_time = 20;
255 bsc.pong_time = 5;
256 bsc.msc_time = 20;
257
258 handle_options(argc, argv);
259
260 signal(SIGPIPE, SIG_IGN);
261 signal(SIGINT, sigint);
262 signal(SIGUSR2, sigusr2);
263 srand(time(NULL));
264
265 cell_vty_init();
266 if (vty_read_config_file(config, NULL) < 0) {
267 fprintf(stderr, "Failed to read the VTY config.\n");
268 return -1;
269 }
270
271 rc = telnet_init(NULL, NULL, 4242);
272 if (rc < 0)
273 return rc;
274
275 bsc.link.the_link = mtp_link_alloc();
276 bsc.link.the_link->dpc = bsc.dpc;
277 bsc.link.the_link->opc = bsc.opc;
278 bsc.link.the_link->link = 0;
279 bsc.link.the_link->sltm_once = bsc.once;
280 bsc.link.bsc = &bsc;
281
282 if (bsc.udp_ip) {
283 LOGP(DINP, LOGL_NOTICE, "Using UDP MTP mode.\n");
284
285 /* setup SNMP first, it is blocking */
286 bsc.link.udp.session = snmp_mtp_session_create(bsc.udp_ip);
287 if (!bsc.link.udp.session)
288 return -1;
289
290 /* now connect to the transport */
291 if (link_udp_init(&bsc.link, bsc.src_port, bsc.udp_ip, bsc.udp_port) != 0)
292 return -1;
293
294 /*
295 * We will ask the MTP link to be taken down for two
296 * timeouts of the BSC to make sure we are missing the
297 * SLTM and it begins a reset. Then we will take it up
298 * again and do the usual business.
299 */
300 snmp_mtp_deactivate(bsc.link.udp.session);
301 bsc.start_timer.cb = start_rest;
302 bsc.start_timer.data = &bsc;
303 bsc_schedule_timer(&bsc.start_timer, bsc.link.udp.reset_timeout, 0);
304 LOGP(DMSC, LOGL_NOTICE, "Making sure SLTM will timeout.\n");
305 } else {
306 LOGP(DINP, LOGL_NOTICE, "Using NexusWare C7 input.\n");
307 if (link_c7_init(&bsc.link) != 0)
308 return -1;
309
310 /* give time to things to start*/
311 bsc.start_timer.cb = start_rest;
312 bsc.start_timer.data = &bsc;
313 bsc_schedule_timer(&bsc.start_timer, 30, 0);
314 LOGP(DMSC, LOGL_NOTICE, "Waiting to continue to startup.\n");
315 }
316
317
318 while (1) {
319 bsc_select_main(0);
320 }
321
322 return 0;
323}
324
325void release_bsc_resources(struct bsc_data *bsc)
326{
327 /* clear pending messages from the MSC */
328 msc_clear_queue(bsc);
329}
330
331struct msgb *create_sccp_rlc(struct sccp_source_reference *src_ref,
332 struct sccp_source_reference *dst)
333{
334 abort();
335 return NULL;
336}
337
338struct msgb *create_reset()
339{
340 abort();
341 return NULL;
342}
343
344void update_con_state(struct mtp_link *link, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls)
345{
346 LOGP(DMSC, LOGL_ERROR, "Should not be called.\n");
347 return;
348}
349
350unsigned int sls_for_src_ref(struct sccp_source_reference *ref)
351{
352 return 13;
353}
354
355int bsc_ussd_handle_in_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg)
356{
357 return 0;
358}
359
360int bsc_ussd_handle_out_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg)
361{
362 return 0;
363}