Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 1 | /* 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 | |
| 58 | static struct log_target *stderr_target; |
| 59 | |
| 60 | static char *config = "udt_relay.cfg"; |
| 61 | |
| 62 | struct bsc_data bsc; |
| 63 | extern void cell_vty_init(void); |
| 64 | |
| 65 | int link_c7_init(struct link_data *data) __attribute__((__weak__)); |
| 66 | |
| 67 | int link_c7_init(struct link_data *data) |
| 68 | { |
| 69 | return -1; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * methods called from the MTP Level3 part |
| 74 | */ |
| 75 | void mtp_link_submit(struct mtp_link *link, struct msgb *msg) |
| 76 | { |
| 77 | bsc.link.write(&bsc.link, msg); |
| 78 | } |
| 79 | |
| 80 | void 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 | |
| 86 | void mtp_link_sccp_down(struct mtp_link *link) |
| 87 | { |
| 88 | msc_clear_queue(&bsc); |
| 89 | } |
| 90 | |
| 91 | void mtp_link_forward_sccp(struct mtp_link *link, struct msgb *_msg, int sls) |
| 92 | { |
| 93 | msc_send_direct(&bsc, _msg); |
| 94 | } |
| 95 | |
| 96 | void 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 | |
| 114 | void bsc_link_up(struct link_data *data) |
| 115 | { |
| 116 | data->the_link->available = 1; |
| 117 | mtp_link_reset(data->the_link); |
| 118 | } |
| 119 | |
| 120 | static void print_usage() |
| 121 | { |
| 122 | printf("Usage: cellmgr_ng\n"); |
| 123 | } |
| 124 | |
| 125 | static 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 | |
| 142 | out: |
| 143 | pthread_mutex_unlock(&exit_mutex); |
| 144 | } |
| 145 | |
| 146 | static void sigusr2() |
| 147 | { |
| 148 | printf("Closing the MSC connection on demand.\n"); |
| 149 | msc_close_connection(&bsc); |
| 150 | } |
| 151 | |
| 152 | static 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 | |
| 162 | static 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 | |
| 208 | static 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 | |
| 221 | int 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; |
Holger Hans Peter Freyther | 7694381 | 2010-11-16 11:14:34 +0100 | [diff] [blame^] | 257 | bsc.forward_only = 1; |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 258 | |
| 259 | handle_options(argc, argv); |
| 260 | |
| 261 | signal(SIGPIPE, SIG_IGN); |
| 262 | signal(SIGINT, sigint); |
| 263 | signal(SIGUSR2, sigusr2); |
| 264 | srand(time(NULL)); |
| 265 | |
| 266 | cell_vty_init(); |
| 267 | if (vty_read_config_file(config, NULL) < 0) { |
| 268 | fprintf(stderr, "Failed to read the VTY config.\n"); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | rc = telnet_init(NULL, NULL, 4242); |
| 273 | if (rc < 0) |
| 274 | return rc; |
| 275 | |
| 276 | bsc.link.the_link = mtp_link_alloc(); |
| 277 | bsc.link.the_link->dpc = bsc.dpc; |
| 278 | bsc.link.the_link->opc = bsc.opc; |
| 279 | bsc.link.the_link->link = 0; |
| 280 | bsc.link.the_link->sltm_once = bsc.once; |
| 281 | bsc.link.bsc = &bsc; |
| 282 | |
| 283 | if (bsc.udp_ip) { |
| 284 | LOGP(DINP, LOGL_NOTICE, "Using UDP MTP mode.\n"); |
| 285 | |
| 286 | /* setup SNMP first, it is blocking */ |
| 287 | bsc.link.udp.session = snmp_mtp_session_create(bsc.udp_ip); |
| 288 | if (!bsc.link.udp.session) |
| 289 | return -1; |
| 290 | |
| 291 | /* now connect to the transport */ |
| 292 | if (link_udp_init(&bsc.link, bsc.src_port, bsc.udp_ip, bsc.udp_port) != 0) |
| 293 | return -1; |
| 294 | |
| 295 | /* |
| 296 | * We will ask the MTP link to be taken down for two |
| 297 | * timeouts of the BSC to make sure we are missing the |
| 298 | * SLTM and it begins a reset. Then we will take it up |
| 299 | * again and do the usual business. |
| 300 | */ |
| 301 | snmp_mtp_deactivate(bsc.link.udp.session); |
| 302 | bsc.start_timer.cb = start_rest; |
| 303 | bsc.start_timer.data = &bsc; |
| 304 | bsc_schedule_timer(&bsc.start_timer, bsc.link.udp.reset_timeout, 0); |
| 305 | LOGP(DMSC, LOGL_NOTICE, "Making sure SLTM will timeout.\n"); |
| 306 | } else { |
| 307 | LOGP(DINP, LOGL_NOTICE, "Using NexusWare C7 input.\n"); |
| 308 | if (link_c7_init(&bsc.link) != 0) |
| 309 | return -1; |
| 310 | |
| 311 | /* give time to things to start*/ |
| 312 | bsc.start_timer.cb = start_rest; |
| 313 | bsc.start_timer.data = &bsc; |
| 314 | bsc_schedule_timer(&bsc.start_timer, 30, 0); |
| 315 | LOGP(DMSC, LOGL_NOTICE, "Waiting to continue to startup.\n"); |
| 316 | } |
| 317 | |
| 318 | |
| 319 | while (1) { |
| 320 | bsc_select_main(0); |
| 321 | } |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | void release_bsc_resources(struct bsc_data *bsc) |
| 327 | { |
| 328 | /* clear pending messages from the MSC */ |
| 329 | msc_clear_queue(bsc); |
| 330 | } |
| 331 | |
| 332 | struct msgb *create_sccp_rlc(struct sccp_source_reference *src_ref, |
| 333 | struct sccp_source_reference *dst) |
| 334 | { |
| 335 | abort(); |
| 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | struct msgb *create_reset() |
| 340 | { |
| 341 | abort(); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | void update_con_state(struct mtp_link *link, int rc, struct sccp_parse_result *res, struct msgb *msg, int from_msc, int sls) |
| 346 | { |
| 347 | LOGP(DMSC, LOGL_ERROR, "Should not be called.\n"); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | unsigned int sls_for_src_ref(struct sccp_source_reference *ref) |
| 352 | { |
| 353 | return 13; |
| 354 | } |
| 355 | |
| 356 | int bsc_ussd_handle_in_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg) |
| 357 | { |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | int bsc_ussd_handle_out_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg) |
| 362 | { |
| 363 | return 0; |
| 364 | } |