blob: 6a17f2394e08c8628e8463eb599cbfaab5084aca [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;
Holger Hans Peter Freyther7a725562011-01-01 13:34:58 +0100229 bsc.sccp_opc = -1;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100230 bsc.udp_port = 3456;
231 bsc.udp_ip = NULL;
232 bsc.src_port = 1313;
Holger Hans Peter Freyther396282e2010-12-08 10:08:14 +0100233 bsc.ni_ni = MTP_NI_NATION_NET;
234 bsc.ni_spare = 0;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100235
236 mtp_link_init();
237 thread_init();
238
239 log_init(&log_info);
240 stderr_target = log_target_create_stderr();
241 log_add_target(stderr_target);
242
243 /* enable filters */
244 log_set_all_filter(stderr_target, 1);
245 log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO);
246 log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO);
247 log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO);
248 log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO);
249 log_set_print_timestamp(stderr_target, 1);
250 log_set_use_color(stderr_target, 0);
251
252 sccp_set_log_area(DSCCP);
253
254 bsc.setup = 0;
255 bsc.msc_address = "127.0.0.1";
256 bsc.link.pcap_fd = -1;
257 bsc.link.udp.reset_timeout = 180;
258 bsc.ping_time = 20;
259 bsc.pong_time = 5;
260 bsc.msc_time = 20;
Holger Hans Peter Freyther76943812010-11-16 11:14:34 +0100261 bsc.forward_only = 1;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100262
263 handle_options(argc, argv);
264
265 signal(SIGPIPE, SIG_IGN);
266 signal(SIGINT, sigint);
267 signal(SIGUSR2, sigusr2);
268 srand(time(NULL));
269
270 cell_vty_init();
271 if (vty_read_config_file(config, NULL) < 0) {
272 fprintf(stderr, "Failed to read the VTY config.\n");
273 return -1;
274 }
275
276 rc = telnet_init(NULL, NULL, 4242);
277 if (rc < 0)
278 return rc;
279
280 bsc.link.the_link = mtp_link_alloc();
281 bsc.link.the_link->dpc = bsc.dpc;
282 bsc.link.the_link->opc = bsc.opc;
Holger Hans Peter Freyther7a725562011-01-01 13:34:58 +0100283 bsc.link.the_link->sccp_opc = bsc.sccp_opc > -1 ? bsc.sccp_opc : bsc.opc;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100284 bsc.link.the_link->link = 0;
285 bsc.link.the_link->sltm_once = bsc.once;
Holger Hans Peter Freyther396282e2010-12-08 10:08:14 +0100286 bsc.link.the_link->ni = bsc.ni_ni;
287 bsc.link.the_link->spare = bsc.ni_spare;
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100288 bsc.link.bsc = &bsc;
289
290 if (bsc.udp_ip) {
291 LOGP(DINP, LOGL_NOTICE, "Using UDP MTP mode.\n");
292
293 /* setup SNMP first, it is blocking */
294 bsc.link.udp.session = snmp_mtp_session_create(bsc.udp_ip);
295 if (!bsc.link.udp.session)
296 return -1;
297
298 /* now connect to the transport */
299 if (link_udp_init(&bsc.link, bsc.src_port, bsc.udp_ip, bsc.udp_port) != 0)
300 return -1;
301
302 /*
303 * We will ask the MTP link to be taken down for two
304 * timeouts of the BSC to make sure we are missing the
305 * SLTM and it begins a reset. Then we will take it up
306 * again and do the usual business.
307 */
308 snmp_mtp_deactivate(bsc.link.udp.session);
309 bsc.start_timer.cb = start_rest;
310 bsc.start_timer.data = &bsc;
311 bsc_schedule_timer(&bsc.start_timer, bsc.link.udp.reset_timeout, 0);
312 LOGP(DMSC, LOGL_NOTICE, "Making sure SLTM will timeout.\n");
313 } else {
314 LOGP(DINP, LOGL_NOTICE, "Using NexusWare C7 input.\n");
315 if (link_c7_init(&bsc.link) != 0)
316 return -1;
317
318 /* give time to things to start*/
319 bsc.start_timer.cb = start_rest;
320 bsc.start_timer.data = &bsc;
321 bsc_schedule_timer(&bsc.start_timer, 30, 0);
322 LOGP(DMSC, LOGL_NOTICE, "Waiting to continue to startup.\n");
323 }
324
325
326 while (1) {
327 bsc_select_main(0);
328 }
329
330 return 0;
331}
332
333void release_bsc_resources(struct bsc_data *bsc)
334{
335 /* clear pending messages from the MSC */
336 msc_clear_queue(bsc);
337}
338
339struct msgb *create_sccp_rlc(struct sccp_source_reference *src_ref,
340 struct sccp_source_reference *dst)
341{
Holger Hans Peter Freyther8220b942010-12-08 10:08:40 +0100342 LOGP(DMSC, LOGL_NOTICE, "Refusing to create connection handling.\n");
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100343 return NULL;
344}
345
346struct msgb *create_reset()
347{
Holger Hans Peter Freyther8220b942010-12-08 10:08:40 +0100348 LOGP(DMSC, LOGL_NOTICE, "Refusing to create a GSM0808 reset message.\n");
Holger Hans Peter Freyther594ee9a2010-11-16 11:03:19 +0100349 return NULL;
350}
351
352void update_con_state(struct mtp_link *link, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls)
353{
354 LOGP(DMSC, LOGL_ERROR, "Should not be called.\n");
355 return;
356}
357
358unsigned int sls_for_src_ref(struct sccp_source_reference *ref)
359{
360 return 13;
361}
362
363int bsc_ussd_handle_in_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg)
364{
365 return 0;
366}
367
368int bsc_ussd_handle_out_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg)
369{
370 return 0;
371}