/* * (C) 2022 by Harald Welte * All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "logging.h" #include "v5x_internal.h" #include "v5x_protocol.h" #include "v51_le_ctrl.h" #include "v52_le_user_port_fsm.h" #include "v5x_le_pstn_fsm.h" #include "v5x_l2_mgmt.h" #include "v51_le_provisioning.h" #include "lapv5.h" #define _GNU_SOURCE #include /* can be changed once libosmocore 1.4.0 is released */ #ifndef OSMO_CTRL_PORT_MGW #define OSMO_CTRL_PORT_MGW 4267 #endif /* only temporarily until this is in libosmocore gsmtap.h */ #ifndef GSMTAP_E1T1_V5EF #define GSMTAP_E1T1_V5EF 0x06 #endif void *tall_v5le_ctx = NULL; struct v5x_instance *v5i = NULL; static struct gsmtap_inst *g_gti; struct v5le_config { }; struct v5le_config *v5le_config_alloc(void *ctx) { struct v5le_config *cfg = talloc_zero(ctx, struct v5le_config); OSMO_ASSERT(cfg); return cfg; } static int v5le_rx_sign(struct msgb *msg) { LOGP(DLMI, LOGL_NOTICE, "Rx: %s\n", msgb_hexdump(msg)); msgb_free(msg); return 0; } static const struct e1inp_line_ops v5le_e1_line_ops = { .sign_link_up = NULL, .sign_link_down = NULL, .sign_link = v5le_rx_sign, }; #include "v5x_protocol.h" /* data L1 -> L2 */ static void hdlc_rx_cb(struct e1inp_ts *ts, struct msgb *msg) { #warning HACKING struct v5x_interface *v5if = (struct v5x_interface *)v5i->interfaces.next; LOGP(DLINP, LOGL_DEBUG, "L1->L2: %s\n", msgb_hexdump(msg)); /* send V5 data via gsmtap so wireshark can receive + decode it */ gsmtap_send_ex(g_gti, GSMTAP_TYPE_E1T1, 0, ts->num, GSMTAP_E1T1_V5EF, 0, 0, 0, 0, msgb_data(msg), msgb_length(msg)); lapv5ef_rx(v5if->primary_link, msg); } static void raw_rx_cb(struct e1inp_ts *ts, struct msgb *msg) { #warning HACKING struct v5x_interface *v5if = (struct v5x_interface *)v5i->interfaces.next; struct v5x_user_port *v5up = v5if->links[0].ts[ts->num].v5up; /* not used by any user port */ if (!v5up) { msgb_free(msg); return; } if (v5up->ts_nr[0] == ts->num && v5up->ts_activated[0]) ph_socket_tx_msg(&v5up->ph_socket, 1, PH_PRIM_DATA_IND, msg->data, msg->len); if (v5up->ts_nr[1] == ts->num && v5up->ts_activated[1]) ph_socket_tx_msg(&v5up->ph_socket, 2, PH_PRIM_DATA_IND, msg->data, msg->len); msgb_free(msg); } /* l2 -> L1 */ int ph_data_req(struct msgb *msg, void *cbdata) { msg->l2h = msgb_push(msg, 2); msg->l2h[0] = msg->l2h[2] & 0xfd; msg->l2h[1] = msg->l2h[3]; LOGP(DLINP, LOGL_DEBUG, "L2->L1: %s\n", msgb_hexdump(msg)); #warning hacking struct e1inp_line *e1_line = e1inp_line_find(0); struct e1inp_ts *ts = &e1_line->ts[16-1]; /* send V5 data via gsmtap so wireshark can receive + decode it */ gsmtap_send_ex(g_gti, GSMTAP_TYPE_E1T1, GSMTAP_ARFCN_F_UPLINK, ts->num, GSMTAP_E1T1_V5EF, 0, 0, 0, 0, msgb_data(msg), msgb_length(msg)); return e1inp_ts_send_hdlc(ts, msg); } #warning hacking int ph_data_req_hack(struct msgb *msg) { LOGP(DLINP, LOGL_DEBUG, "L2->L1: %s\n", msgb_hexdump(msg)); #warning hacking struct e1inp_line *e1_line = e1inp_line_find(0); struct e1inp_ts *ts = &e1_line->ts[16-1]; /* send V5 data via gsmtap so wireshark can receive + decode it */ gsmtap_send_ex(g_gti, GSMTAP_TYPE_E1T1, GSMTAP_ARFCN_F_UPLINK, ts->num, GSMTAP_E1T1_V5EF, 0, 0, 0, 0, msgb_data(msg), msgb_length(msg)); return e1inp_ts_send_hdlc(ts, msg); } #warning hacking int ph_data_req_raw(struct msgb *msg, int ts_nr) { #warning hacking struct e1inp_line *e1_line = e1inp_line_find(0); struct e1inp_ts *ts = &e1_line->ts[ts_nr-1]; return e1inp_ts_send_raw(ts, msg); } static int e1_init(void) { int line_nr = 1, ts; struct e1inp_line *e1_line; e1_line = e1inp_line_find(0); OSMO_ASSERT(e1_line); e1inp_line_bind_ops(e1_line, &v5le_e1_line_ops); for (ts = 1; ts <= 31; ts++) { struct e1inp_ts *e1_ts = &e1_line->ts[ts-1]; if (ts == 16) { // FIXME: make this depending on c_channel //e1inp_ts_config_sign(e1_ts, e1_line); //e1inp_sign_link_create(e1_ts, E1INP_SIGN_NONE, NULL, 115/*TEI*/, 0/*SAPI*/); e1inp_ts_config_hdlc(e1_ts, e1_line, hdlc_rx_cb); } else e1inp_ts_config_raw(e1_ts, e1_line, raw_rx_cb); } return e1inp_line_update(e1_line); } static struct v5le_config *g_cfg; static int daemonize = 0; const char *v5le_copyright = "Copyright (C) 2022 by Harald Welte\r\n" "License AGPLv3+: GNU AGPL version 3 or later \r\n" "This is free software: you are free to change and redistribute it.\r\n" "There is NO WARRANTY, to the extent permitted by law.\r\n"; static char *config_file = "osmo-v5le.cfg"; static void print_help() { printf("Some useful options:\n"); printf(" -h --help is printing this text.\n"); printf(" -c --config-file filename The config file to use.\n"); printf(" -s --disable-color\n"); printf(" -D --daemonize Fork the process into a background daemon\n"); printf(" -V --version Print the version number\n"); printf("\nVTY reference generation:\n"); printf(" --vty-ref-mode MODE VTY reference generation mode (e.g. 'expert').\n"); printf(" --vty-ref-xml Generate the VTY reference XML output and exit.\n"); } static void handle_long_options(const char *prog_name, const int long_option) { static int vty_ref_mode = VTY_REF_GEN_MODE_DEFAULT; switch (long_option) { case 1: vty_ref_mode = get_string_value(vty_ref_gen_mode_names, optarg); if (vty_ref_mode < 0) { fprintf(stderr, "%s: Unknown VTY reference generation " "mode '%s'\n", prog_name, optarg); exit(2); } break; case 2: fprintf(stderr, "Generating the VTY reference in mode '%s' (%s)\n", get_value_string(vty_ref_gen_mode_names, vty_ref_mode), get_value_string(vty_ref_gen_mode_desc, vty_ref_mode)); vty_dump_xml_ref_mode(stdout, (enum vty_ref_gen_mode) vty_ref_mode); exit(0); default: fprintf(stderr, "%s: error parsing cmdline options\n", prog_name); exit(2); } } static void handle_options(int argc, char **argv) { while (1) { int option_index = 0, c; static int long_option = 0; static struct option long_options[] = { {"help", 0, 0, 'h'}, {"config-file", 1, 0, 'c'}, {"daemonize", 0, 0, 'D'}, {"version", 0, 0, 'V'}, {"disable-color", 0, 0, 's'}, {"vty-ref-mode", 1, &long_option, 1}, {"vty-ref-xml", 0, &long_option, 2}, {0, 0, 0, 0}, }; c = getopt_long(argc, argv, "hc:sVD", long_options, &option_index); if (c == -1) break; switch(c) { case 'h': print_help(); exit(0); break; case 0: handle_long_options(argv[0], long_option); break; case 'c': config_file = talloc_strdup(tall_v5le_ctx, optarg); break; case 's': log_set_use_color(osmo_stderr_target, 0); break; case 'V': print_version(1); exit(0); break; case 'D': daemonize = 1; break; default: /* ignore */ break; }; } if (argc > optind) { fprintf(stderr, "Unsupported positional arguments on command line\n"); exit(2); } } int v5le_vty_is_config_node(struct vty *vty, int node) { switch (node) { case CONFIG_NODE: return 0; default: return 1; } } int v5le_vty_go_parent(struct vty *vty) { switch (vty->node) { default: if (v5le_vty_is_config_node(vty, vty->node)) vty->node = CONFIG_NODE; else vty->node = ENABLE_NODE; vty->index = NULL; } return vty->node; } static struct vty_app_info vty_info = { .name = "OsmoV5LE", .version = PACKAGE_VERSION, .go_parent_cb = v5le_vty_go_parent, .is_config_node = v5le_vty_is_config_node, }; int main(int argc, char **argv) { unsigned int flags; int rc; tall_v5le_ctx = talloc_named_const(NULL, 1, "v5le"); vty_info.tall_ctx = tall_v5le_ctx; msgb_talloc_ctx_init(tall_v5le_ctx, 0); osmo_init_ignore_signals(); osmo_init_logging2(tall_v5le_ctx, &log_info); libosmo_abis_init(tall_v5le_ctx); g_cfg = v5le_config_alloc(tall_v5le_ctx); if (!g_cfg) return -1; vty_info.copyright = v5le_copyright; vty_init(&vty_info); logging_vty_add_cmds(); osmo_talloc_vty_add_cmds(); osmo_stats_vty_add_cmds(); v5x_vty_init(); ctrl_vty_init(g_cfg); e1inp_vty_init(); osmo_cpu_sched_vty_init(tall_v5le_ctx); handle_options(argc, argv); rate_ctr_init(tall_v5le_ctx); osmo_stats_init(tall_v5le_ctx); /* global inits of protocols */ v51_ctrl_init(); v51_port_ctrl_init(); v51_pstn_init(); v51_provisioning_init(); v51_mgmt_init(); /* create v5x instance */ v5i = v5x_instance_alloc(tall_v5le_ctx); if (!v5i) return -ENOMEM; // FIXME: move this to VTY code /* create v5x interface */ struct v5x_interface *v5if; // struct v5x_user_port *v5up; uint32_t interface_id = 1; uint8_t interface_variant = 1; v5if = v5x_interface_alloc(v5i, V5X_DIALECT_V51, interface_id, interface_variant, ph_data_req); if (!v5if) return -ENOMEM; // v5up = v5x_user_port_create(v5if, 1001, V5X_USER_TYPE_ISDN, 1, 2); // v5up = v5x_user_port_create(v5if, 1002, V5X_USER_TYPE_PSTN, 3, 0); // if (!v5up) // return -ENOMEM; rc = vty_read_config_file(config_file, NULL); if (rc < 0) return rc; g_gti = gsmtap_source_init("224.0.0.1", GSMTAP_UDP_PORT, 0); OSMO_ASSERT(g_gti); gsmtap_source_add_sink(g_gti); rc = e1_init(); OSMO_ASSERT(rc == 0); /* start telnet after reading config for vty_get_bind_addr() */ rc = telnet_init_dynif(tall_v5le_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_MGW); if (rc < 0) return rc; #if 0 cfg->ctrl = v5le_ctrl_interface_setup(cfg, ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_MGW); if (!cfg->ctrl) { fprintf(stderr, "Failed to init the control interface on %s:%u. Exiting\n", ctrl_vty_get_bind_addr(), OSMO_CTRL_PORT_MGW); } /* Set the reset callback function. This functions is called when the * mgcp-command "RSIP" (Reset in Progress) is received */ cfg->reset_cb = mgcp_rsip_cb; /* we need to bind a socket */ flags = OSMO_SOCK_F_BIND; if (strlen(cfg->call_agent_addr)) flags |= OSMO_SOCK_F_CONNECT; rc = osmo_sock_init2_ofd(&cfg->gw_fd.bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, cfg->source_addr, cfg->source_port, cfg->call_agent_addr, strlen(cfg->call_agent_addr) ? 2727 : 0, flags); if (rc < 0) { perror("Gateway failed to bind"); return -1; } cfg->gw_fd.bfd.cb = read_call_agent; cfg->gw_fd.bfd.data = msgb_alloc(4096, "mgcp-msg"); if (!cfg->gw_fd.bfd.data) { fprintf(stderr, "Gateway memory error.\n"); return -1; } LOGP(DLMGCP, LOGL_NOTICE, "Configured for MGCP, listen on %s:%u\n", cfg->source_addr, cfg->source_port); #endif /* initialisation */ srand(time(NULL)); if (daemonize) { rc = osmo_daemonize(); if (rc < 0) { perror("Error during daemonize"); exit(1); } } /* main loop */ while (1) { osmo_select_main(0); } return 0; }