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