Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
| 3 | * (C) 2010 by On-Waves |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/socket.h> |
| 24 | #include <netinet/in.h> |
| 25 | #include <arpa/inet.h> |
| 26 | |
| 27 | #include <osmocore/talloc.h> |
| 28 | |
| 29 | #include <openbsc/debug.h> |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 30 | #include <openbsc/gb_proxy.h> |
| 31 | #include <openbsc/gprs_ns.h> |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 32 | |
| 33 | #include <vty/command.h> |
| 34 | #include <vty/vty.h> |
| 35 | |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 36 | static struct gbproxy_config *g_cfg = NULL; |
| 37 | |
| 38 | /* |
| 39 | * vty code for mgcp below |
| 40 | */ |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 41 | static struct cmd_node gbproxy_node = { |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 42 | GBPROXY_NODE, |
| 43 | "%s(gbproxy)#", |
| 44 | 1, |
| 45 | }; |
| 46 | |
| 47 | static int config_write_gbproxy(struct vty *vty) |
| 48 | { |
| 49 | struct in_addr ia; |
| 50 | |
| 51 | vty_out(vty, "gbproxy%s", VTY_NEWLINE); |
| 52 | |
| 53 | if (g_cfg->nsip_listen_ip) { |
| 54 | ia.s_addr = htonl(g_cfg->nsip_listen_ip); |
| 55 | vty_out(vty, " nsip bss local ip %s%s", inet_ntoa(ia), |
| 56 | VTY_NEWLINE); |
| 57 | } |
| 58 | vty_out(vty, " nsip bss local port %u%s", g_cfg->nsip_listen_port, |
| 59 | VTY_NEWLINE); |
| 60 | ia.s_addr = htonl(g_cfg->nsip_sgsn_ip); |
| 61 | vty_out(vty, " nsip sgsn remote ip %s%s", inet_ntoa(ia), |
| 62 | VTY_NEWLINE); |
| 63 | vty_out(vty, " nsip sgsn remote port %u%s", g_cfg->nsip_sgsn_port, |
| 64 | VTY_NEWLINE); |
| 65 | vty_out(vty, " nsip sgsn nsei %u%s", g_cfg->nsip_sgsn_nsei, |
| 66 | VTY_NEWLINE); |
| 67 | vty_out(vty, " nsip sgsn nsvci %u%s", g_cfg->nsip_sgsn_nsvci, |
| 68 | VTY_NEWLINE); |
| 69 | |
| 70 | return CMD_SUCCESS; |
| 71 | } |
| 72 | |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 73 | DEFUN(show_ns, show_ns_cmd, "show ns", |
| 74 | SHOW_STR "Display information about the NS protocol") |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 75 | { |
| 76 | /* FIXME: iterate over list of NS-VC's and display their state */ |
Harald Welte | 323e4f6 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 77 | struct gprs_ns_inst *nsi = g_cfg->nsi; |
| 78 | struct gprs_nsvc *nsvc; |
| 79 | |
| 80 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 81 | vty_out(vty, "NSEI %5u, NS-VC %5u, %s-mode, %s %s%s", |
| 82 | nsvc->nsei, nsvc->nsvci, |
| 83 | nsvc->remote_end_is_sgsn ? "BSS" : "SGSN", |
| 84 | nsvc->state & NSE_S_ALIVE ? "ALIVE" : "DEAD", |
| 85 | nsvc->state & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED", |
| 86 | VTY_NEWLINE); |
| 87 | if (nsvc->nsi->ll == GPRS_NS_LL_UDP) |
| 88 | vty_out(vty, " remote peer %s:%u%s", |
| 89 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), |
| 90 | ntohs(nsvc->ip.bts_addr.sin_port), VTY_NEWLINE); |
| 91 | } |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 92 | |
| 93 | return CMD_SUCCESS; |
| 94 | } |
| 95 | |
| 96 | DEFUN(cfg_gbproxy, |
| 97 | cfg_gbproxy_cmd, |
| 98 | "gbproxy", |
| 99 | "Configure the Gb proxy") |
| 100 | { |
| 101 | vty->node = GBPROXY_NODE; |
| 102 | return CMD_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | DEFUN(cfg_nsip_bss_local_ip, |
| 106 | cfg_nsip_bss_local_ip_cmd, |
| 107 | "nsip bss local ip A.B.C.D", |
| 108 | "Set the IP address on which we listen for BSS connects") |
| 109 | { |
| 110 | struct in_addr ia; |
| 111 | |
| 112 | inet_aton(argv[0], &ia); |
| 113 | g_cfg->nsip_listen_ip = ntohl(ia.s_addr); |
| 114 | |
| 115 | return CMD_SUCCESS; |
| 116 | } |
| 117 | |
| 118 | DEFUN(cfg_nsip_bss_local_port, |
| 119 | cfg_nsip_bss_local_port_cmd, |
| 120 | "nsip bss local port <0-65534>", |
| 121 | "Set the UDP port on which we listen for BSS connects") |
| 122 | { |
| 123 | unsigned int port = atoi(argv[0]); |
| 124 | |
| 125 | g_cfg->nsip_listen_port = port; |
| 126 | return CMD_SUCCESS; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | DEFUN(cfg_nsip_sgsn_ip, |
| 131 | cfg_nsip_sgsn_ip_cmd, |
| 132 | "nsip sgsn remote ip A.B.C.D", |
| 133 | "Set the IP of the SGSN to which the proxy shall connect") |
| 134 | { |
| 135 | struct in_addr ia; |
| 136 | |
| 137 | inet_aton(argv[0], &ia); |
| 138 | g_cfg->nsip_sgsn_ip = ntohl(ia.s_addr); |
| 139 | |
| 140 | return CMD_SUCCESS; |
| 141 | } |
| 142 | |
| 143 | DEFUN(cfg_nsip_sgsn_port, |
| 144 | cfg_nsip_sgsn_port_cmd, |
| 145 | "nsip sgsn remote port <0-65534>", |
| 146 | "Set the UDP port of the SGSN to which the proxy shall connect") |
| 147 | { |
| 148 | unsigned int port = atoi(argv[0]); |
| 149 | |
| 150 | g_cfg->nsip_sgsn_port = port; |
| 151 | return CMD_SUCCESS; |
| 152 | } |
| 153 | |
| 154 | DEFUN(cfg_nsip_sgsn_nsei, |
| 155 | cfg_nsip_sgsn_nsei_cmd, |
| 156 | "nsip sgsn nsei <0-65534>", |
| 157 | "Set the NSEI to be used in the connection with the SGSN") |
| 158 | { |
| 159 | unsigned int port = atoi(argv[0]); |
| 160 | |
| 161 | g_cfg->nsip_sgsn_nsei = port; |
| 162 | return CMD_SUCCESS; |
| 163 | } |
| 164 | |
| 165 | DEFUN(cfg_nsip_sgsn_nsvci, |
| 166 | cfg_nsip_sgsn_nsvci_cmd, |
| 167 | "nsip sgsn nsvci <0-65534>", |
| 168 | "Set the NSVCI to be used in the connection with the SGSN") |
| 169 | { |
| 170 | unsigned int port = atoi(argv[0]); |
| 171 | |
| 172 | g_cfg->nsip_sgsn_nsvci = port; |
| 173 | return CMD_SUCCESS; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | int gbproxy_vty_init(void) |
| 178 | { |
Harald Welte | b34e2c1 | 2010-05-11 05:49:43 +0200 | [diff] [blame] | 179 | install_element(VIEW_NODE, &show_ns_cmd); |
Harald Welte | a167efb | 2010-04-30 21:49:24 +0200 | [diff] [blame] | 180 | install_element(VIEW_NODE, &show_gbproxy_cmd); |
| 181 | |
| 182 | install_element(CONFIG_NODE, &cfg_gbproxy_cmd); |
| 183 | install_node(&gbproxy_node, config_write_gbproxy); |
| 184 | install_default(GBPROXY_NODE); |
| 185 | install_element(GBPROXY_NODE, &cfg_nsip_bss_local_ip_cmd); |
| 186 | install_element(GBPROXY_NODE, &cfg_nsip_bss_local_port_cmd); |
| 187 | install_element(GBPROXY_NODE, &cfg_nsip_sgsn_ip_cmd); |
| 188 | install_element(GBPROXY_NODE, &cfg_nsip_sgsn_port_cmd); |
| 189 | install_element(GBPROXY_NODE, &cfg_nsip_sgsn_nsei_cmd); |
| 190 | install_element(GBPROXY_NODE, &cfg_nsip_sgsn_nsvci_cmd); |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg) |
| 196 | { |
| 197 | int rc; |
| 198 | |
| 199 | g_cfg = cfg; |
| 200 | rc = vty_read_config_file(config_file); |
| 201 | if (rc < 0) { |
| 202 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 203 | return rc; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |