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 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 7 | * 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 Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 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 |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 15 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 16 | * |
Holger Hans Peter Freyther | de56c22 | 2011-01-16 17:45:14 +0100 | [diff] [blame] | 17 | * 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 Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <mtp_data.h> |
Holger Hans Peter Freyther | 396282e | 2010-12-08 10:08:14 +0100 | [diff] [blame] | 23 | #include <mtp_level3.h> |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 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 | |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 65 | /* |
| 66 | * methods called from the MTP Level3 part |
| 67 | */ |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame^] | 68 | void mtp_link_set_forward_sccp(struct mtp_link_set *link, struct msgb *_msg, int sls) |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 69 | { |
| 70 | msc_send_direct(&bsc, _msg); |
| 71 | } |
| 72 | |
| 73 | void bsc_link_down(struct link_data *data) |
| 74 | { |
| 75 | int was_up; |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame^] | 76 | struct mtp_link_set *link = data->the_link; |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 77 | |
| 78 | link->available = 0; |
| 79 | was_up = link->sccp_up; |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame^] | 80 | mtp_link_set_stop(link); |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 81 | |
| 82 | data->clear_queue(data); |
| 83 | |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 84 | /* If we have an A link send a reset to the MSC */ |
| 85 | msc_send_reset(data->bsc); |
| 86 | } |
| 87 | |
| 88 | void bsc_link_up(struct link_data *data) |
| 89 | { |
| 90 | data->the_link->available = 1; |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame^] | 91 | mtp_link_set_reset(data->the_link); |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void print_usage() |
| 95 | { |
| 96 | printf("Usage: cellmgr_ng\n"); |
| 97 | } |
| 98 | |
| 99 | static void sigint() |
| 100 | { |
| 101 | static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 102 | static int handled = 0; |
| 103 | |
| 104 | /* failed to lock */ |
| 105 | if (pthread_mutex_trylock(&exit_mutex) != 0) |
| 106 | return; |
| 107 | if (handled) |
| 108 | goto out; |
| 109 | |
| 110 | printf("Terminating.\n"); |
| 111 | handled = 1; |
| 112 | if (bsc.setup) |
| 113 | bsc.link.shutdown(&bsc.link); |
| 114 | exit(0); |
| 115 | |
| 116 | out: |
| 117 | pthread_mutex_unlock(&exit_mutex); |
| 118 | } |
| 119 | |
| 120 | static void sigusr2() |
| 121 | { |
| 122 | printf("Closing the MSC connection on demand.\n"); |
| 123 | msc_close_connection(&bsc); |
| 124 | } |
| 125 | |
| 126 | static void print_help() |
| 127 | { |
| 128 | printf(" Some useful help...\n"); |
| 129 | printf(" -h --help this text\n"); |
| 130 | printf(" -c --config=CFG The config file to use.\n"); |
| 131 | printf(" -p --pcap=FILE. Write MSUs to the PCAP file.\n"); |
| 132 | printf(" -c --once. Send the SLTM msg only once.\n"); |
| 133 | printf(" -v --version. Print the version number\n"); |
| 134 | } |
| 135 | |
| 136 | static void handle_options(int argc, char **argv) |
| 137 | { |
| 138 | while (1) { |
| 139 | int option_index = 0, c; |
| 140 | static struct option long_options[] = { |
| 141 | {"help", 0, 0, 'h'}, |
| 142 | {"config", 1, 0, 'c'}, |
| 143 | {"pcap", 1, 0, 'p'}, |
| 144 | {"version", 0, 0, 0}, |
| 145 | {0, 0, 0, 0}, |
| 146 | }; |
| 147 | |
| 148 | c = getopt_long(argc, argv, "hc:p:v", |
| 149 | long_options, &option_index); |
| 150 | if (c == -1) |
| 151 | break; |
| 152 | |
| 153 | switch (c) { |
| 154 | case 'h': |
| 155 | print_usage(); |
| 156 | print_help(); |
| 157 | exit(0); |
| 158 | case 'p': |
| 159 | if (bsc.link.pcap_fd >= 0) |
| 160 | close(bsc.link.pcap_fd); |
| 161 | bsc.link.pcap_fd = open(optarg, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP| S_IROTH); |
| 162 | if (bsc.link.pcap_fd < 0) { |
| 163 | fprintf(stderr, "Failed to open PCAP file.\n"); |
| 164 | exit(0); |
| 165 | } |
| 166 | mtp_pcap_write_header(bsc.link.pcap_fd); |
| 167 | break; |
| 168 | case 'c': |
| 169 | config = optarg; |
| 170 | break; |
| 171 | case 'v': |
| 172 | printf("This is %s version %s.\n", PACKAGE, VERSION); |
| 173 | exit(0); |
| 174 | break; |
| 175 | default: |
| 176 | fprintf(stderr, "Unknown option.\n"); |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 182 | int main(int argc, char **argv) |
| 183 | { |
| 184 | int rc; |
| 185 | INIT_LLIST_HEAD(&bsc.sccp_connections); |
| 186 | |
| 187 | bsc.dpc = 1; |
| 188 | bsc.opc = 0; |
Holger Hans Peter Freyther | 7a72556 | 2011-01-01 13:34:58 +0100 | [diff] [blame] | 189 | bsc.sccp_opc = -1; |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 190 | bsc.udp_port = 3456; |
| 191 | bsc.udp_ip = NULL; |
| 192 | bsc.src_port = 1313; |
Holger Hans Peter Freyther | 396282e | 2010-12-08 10:08:14 +0100 | [diff] [blame] | 193 | bsc.ni_ni = MTP_NI_NATION_NET; |
| 194 | bsc.ni_spare = 0; |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 195 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame^] | 196 | mtp_link_set_init(); |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 197 | thread_init(); |
| 198 | |
| 199 | log_init(&log_info); |
| 200 | stderr_target = log_target_create_stderr(); |
| 201 | log_add_target(stderr_target); |
| 202 | |
| 203 | /* enable filters */ |
| 204 | log_set_all_filter(stderr_target, 1); |
| 205 | log_set_category_filter(stderr_target, DINP, 1, LOGL_INFO); |
| 206 | log_set_category_filter(stderr_target, DSCCP, 1, LOGL_INFO); |
| 207 | log_set_category_filter(stderr_target, DMSC, 1, LOGL_INFO); |
| 208 | log_set_category_filter(stderr_target, DMGCP, 1, LOGL_INFO); |
| 209 | log_set_print_timestamp(stderr_target, 1); |
| 210 | log_set_use_color(stderr_target, 0); |
| 211 | |
| 212 | sccp_set_log_area(DSCCP); |
| 213 | |
| 214 | bsc.setup = 0; |
| 215 | bsc.msc_address = "127.0.0.1"; |
| 216 | bsc.link.pcap_fd = -1; |
| 217 | bsc.link.udp.reset_timeout = 180; |
| 218 | bsc.ping_time = 20; |
| 219 | bsc.pong_time = 5; |
| 220 | bsc.msc_time = 20; |
Holger Hans Peter Freyther | 7694381 | 2010-11-16 11:14:34 +0100 | [diff] [blame] | 221 | bsc.forward_only = 1; |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 222 | |
| 223 | handle_options(argc, argv); |
| 224 | |
| 225 | signal(SIGPIPE, SIG_IGN); |
| 226 | signal(SIGINT, sigint); |
| 227 | signal(SIGUSR2, sigusr2); |
| 228 | srand(time(NULL)); |
| 229 | |
| 230 | cell_vty_init(); |
| 231 | if (vty_read_config_file(config, NULL) < 0) { |
| 232 | fprintf(stderr, "Failed to read the VTY config.\n"); |
| 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | rc = telnet_init(NULL, NULL, 4242); |
| 237 | if (rc < 0) |
| 238 | return rc; |
| 239 | |
Holger Hans Peter Freyther | a99b04b | 2011-01-02 11:23:54 +0100 | [diff] [blame] | 240 | if (link_init(&bsc) != 0) |
| 241 | return -1; |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 242 | |
| 243 | while (1) { |
| 244 | bsc_select_main(0); |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | void release_bsc_resources(struct bsc_data *bsc) |
| 251 | { |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | struct msgb *create_sccp_rlc(struct sccp_source_reference *src_ref, |
| 255 | struct sccp_source_reference *dst) |
| 256 | { |
Holger Hans Peter Freyther | 8220b94 | 2010-12-08 10:08:40 +0100 | [diff] [blame] | 257 | LOGP(DMSC, LOGL_NOTICE, "Refusing to create connection handling.\n"); |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | struct msgb *create_reset() |
| 262 | { |
Holger Hans Peter Freyther | 8220b94 | 2010-12-08 10:08:40 +0100 | [diff] [blame] | 263 | LOGP(DMSC, LOGL_NOTICE, "Refusing to create a GSM0808 reset message.\n"); |
Holger Hans Peter Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 264 | return NULL; |
| 265 | } |
| 266 | |
Holger Hans Peter Freyther | 569f1e1 | 2011-01-02 18:47:49 +0100 | [diff] [blame^] | 267 | void 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 Freyther | 594ee9a | 2010-11-16 11:03:19 +0100 | [diff] [blame] | 268 | { |
| 269 | LOGP(DMSC, LOGL_ERROR, "Should not be called.\n"); |
| 270 | return; |
| 271 | } |
| 272 | |
| 273 | unsigned int sls_for_src_ref(struct sccp_source_reference *ref) |
| 274 | { |
| 275 | return 13; |
| 276 | } |
| 277 | |
| 278 | int bsc_ussd_handle_in_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg) |
| 279 | { |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | int bsc_ussd_handle_out_msg(struct bsc_data *bsc, struct sccp_parse_result *res, struct msgb *msg) |
| 284 | { |
| 285 | return 0; |
| 286 | } |