Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 1 | /* OpenBSC NAT interface to quagga VTY */ |
| 2 | /* (C) 2010 by Holger Hans Peter Freyther |
| 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 <vty/command.h> |
| 23 | #include <vty/buffer.h> |
| 24 | #include <vty/vty.h> |
| 25 | |
| 26 | #include <openbsc/bsc_nat.h> |
| 27 | #include <openbsc/gsm_04_08.h> |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 28 | #include <openbsc/mgcp.h> |
Holger Hans Peter Freyther | b372fad | 2010-04-06 12:01:15 +0200 | [diff] [blame] | 29 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 3b96089 | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 30 | |
| 31 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 32 | |
| 33 | #include <sccp/sccp.h> |
| 34 | |
| 35 | #include <stdlib.h> |
| 36 | |
| 37 | static struct bsc_nat *_nat; |
| 38 | |
| 39 | static struct cmd_node nat_node = { |
| 40 | NAT_NODE, |
| 41 | "%s(nat)#", |
| 42 | 1, |
| 43 | }; |
| 44 | |
| 45 | static struct cmd_node bsc_node = { |
| 46 | BSC_NODE, |
| 47 | "%s(bsc)#", |
| 48 | 1, |
| 49 | }; |
| 50 | |
| 51 | static int config_write_nat(struct vty *vty) |
| 52 | { |
| 53 | vty_out(vty, "nat%s", VTY_NEWLINE); |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 54 | if (_nat->imsi_allow) |
| 55 | vty_out(vty, " imsi allow %s%s", _nat->imsi_allow, VTY_NEWLINE); |
| 56 | if (_nat->imsi_deny) |
| 57 | vty_out(vty, " insi deny %s%s", _nat->imsi_deny, VTY_NEWLINE); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 58 | vty_out(vty, " msc ip %s%s", _nat->msc_ip, VTY_NEWLINE); |
Holger Hans Peter Freyther | bace202 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 59 | vty_out(vty, " msc port %d%s", _nat->msc_port, VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 60 | return CMD_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | static void config_write_bsc_single(struct vty *vty, struct bsc_config *bsc) |
| 64 | { |
| 65 | vty_out(vty, " bsc %u%s", bsc->nr, VTY_NEWLINE); |
| 66 | vty_out(vty, " token %s%s", bsc->token, VTY_NEWLINE); |
Holger Hans Peter Freyther | 085ea30 | 2010-04-16 16:52:20 +0200 | [diff] [blame] | 67 | vty_out(vty, " location_area_code %u%s", bsc->lac, VTY_NEWLINE); |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 68 | if (bsc->imsi_allow) |
| 69 | vty_out(vty, " imsi allow %s%s", bsc->imsi_allow, VTY_NEWLINE); |
| 70 | if (bsc->imsi_deny) |
| 71 | vty_out(vty, " imsi deny %s%s", bsc->imsi_deny, VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static int config_write_bsc(struct vty *vty) |
| 75 | { |
| 76 | struct bsc_config *bsc; |
| 77 | |
| 78 | llist_for_each_entry(bsc, &_nat->bsc_configs, entry) |
| 79 | config_write_bsc_single(vty, bsc); |
| 80 | return CMD_SUCCESS; |
| 81 | } |
| 82 | |
| 83 | |
Holger Hans Peter Freyther | 17f8c95 | 2010-04-19 16:06:43 +0800 | [diff] [blame] | 84 | DEFUN(show_sccp, show_sccp_cmd, "show sccp connections", |
| 85 | SHOW_STR "Display information about current SCCP connections") |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 86 | { |
| 87 | struct sccp_connections *con; |
| 88 | llist_for_each_entry(con, &_nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 89 | vty_out(vty, "SCCP for BSC: Nr: %d lac: %d BSC ref: 0x%x Local ref: 0x%x MSC/BSC mux: 0x%x/0x%x%s", |
| 90 | con->bsc->cfg ? con->bsc->cfg->nr : -1, |
| 91 | con->bsc->cfg ? con->bsc->cfg->lac : -1, |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 92 | sccp_src_ref_to_int(&con->real_ref), |
Holger Hans Peter Freyther | 32aba23 | 2010-04-05 10:10:33 +0200 | [diff] [blame] | 93 | sccp_src_ref_to_int(&con->patched_ref), |
| 94 | con->msc_timeslot, con->bsc_timeslot, |
| 95 | VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return CMD_SUCCESS; |
| 99 | } |
| 100 | |
Holger Hans Peter Freyther | 17f8c95 | 2010-04-19 16:06:43 +0800 | [diff] [blame] | 101 | DEFUN(show_bsc, show_bsc_cmd, "show bsc connections", |
| 102 | SHOW_STR "Display information about current BSCs") |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 103 | { |
| 104 | struct bsc_connection *con; |
Holger Hans Peter Freyther | 9e53e97 | 2010-04-08 10:35:20 +0200 | [diff] [blame] | 105 | struct sockaddr_in sock; |
| 106 | socklen_t len = sizeof(sock); |
| 107 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 108 | llist_for_each_entry(con, &_nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | 9e53e97 | 2010-04-08 10:35:20 +0200 | [diff] [blame] | 109 | getpeername(con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len); |
| 110 | vty_out(vty, "BSC lac: %d, %d auth: %d fd: %d peername: %s%s", |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 111 | con->cfg ? con->cfg->nr : -1, |
| 112 | con->cfg ? con->cfg->lac : -1, |
Holger Hans Peter Freyther | 9e53e97 | 2010-04-08 10:35:20 +0200 | [diff] [blame] | 113 | con->authenticated, con->write_queue.bfd.fd, |
| 114 | inet_ntoa(sock.sin_addr), VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return CMD_SUCCESS; |
| 118 | } |
| 119 | |
Holger Hans Peter Freyther | 73e5b78 | 2010-04-12 07:29:22 +0200 | [diff] [blame] | 120 | DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "bsc config show", |
Holger Freyther | f514812 | 2010-04-17 23:10:34 +0800 | [diff] [blame] | 121 | "Display information about known BSC configs") |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 122 | { |
| 123 | struct bsc_config *conf; |
| 124 | llist_for_each_entry(conf, &_nat->bsc_configs, entry) { |
| 125 | vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s", |
| 126 | conf->token, conf->lac, conf->nr, VTY_NEWLINE); |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 127 | vty_out(vty, " imsi_allow: '%s' imsi_deny: '%s'%s", |
| 128 | conf->imsi_allow ? conf->imsi_allow: "any", |
| 129 | conf->imsi_deny ? conf->imsi_deny : "none", |
| 130 | VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | return CMD_SUCCESS; |
| 134 | } |
| 135 | |
Holger Hans Peter Freyther | 02e1c7d | 2010-04-12 12:33:27 +0200 | [diff] [blame] | 136 | DEFUN(show_stats, |
| 137 | show_stats_cmd, |
| 138 | "show statistics", |
| 139 | SHOW_STR "Display network statistics\n") |
| 140 | { |
| 141 | struct bsc_config *conf; |
| 142 | |
| 143 | vty_out(vty, "NAT statistics%s", VTY_NEWLINE); |
| 144 | vty_out(vty, " SCCP Connections %lu total, %lu calls%s", |
| 145 | counter_get(_nat->stats.sccp.conn), |
| 146 | counter_get(_nat->stats.sccp.calls), VTY_NEWLINE); |
| 147 | vty_out(vty, " MSC Connections %lu%s", |
| 148 | counter_get(_nat->stats.msc.reconn), VTY_NEWLINE); |
| 149 | vty_out(vty, " BSC Connections %lu total, %lu auth failed.%s", |
| 150 | counter_get(_nat->stats.bsc.reconn), |
| 151 | counter_get(_nat->stats.bsc.auth_fail), VTY_NEWLINE); |
| 152 | |
| 153 | llist_for_each_entry(conf, &_nat->bsc_configs, entry) { |
| 154 | vty_out(vty, " BSC lac: %d nr: %d%s", |
| 155 | conf->lac, conf->nr, VTY_NEWLINE); |
| 156 | vty_out(vty, " SCCP Connnections %lu total, %lu calls%s", |
| 157 | counter_get(conf->stats.sccp.conn), |
| 158 | counter_get(conf->stats.sccp.calls), VTY_NEWLINE); |
| 159 | vty_out(vty, " BSC Connections %lu total%s", |
| 160 | counter_get(conf->stats.net.reconn), VTY_NEWLINE); |
| 161 | } |
| 162 | |
| 163 | return CMD_SUCCESS; |
| 164 | } |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 165 | |
| 166 | DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT") |
| 167 | { |
| 168 | vty->index = _nat; |
| 169 | vty->node = NAT_NODE; |
| 170 | |
| 171 | return CMD_SUCCESS; |
| 172 | } |
| 173 | |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 174 | static void parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) |
| 175 | { |
| 176 | if (*imsi) { |
| 177 | talloc_free(*imsi); |
| 178 | *imsi = NULL; |
| 179 | } |
| 180 | regfree(reg); |
| 181 | |
| 182 | if (argc > 0) { |
| 183 | *imsi = talloc_strdup(ctx, argv[0]); |
| 184 | regcomp(reg, argv[0], 0); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | DEFUN(cfg_nat_imsi_allow, |
| 189 | cfg_nat_imsi_allow_cmd, |
| 190 | "imsi allow [REGEXP]", |
| 191 | "Allow matching IMSIs to talk to the MSC. " |
| 192 | "The defualt is to allow everyone.") |
| 193 | { |
| 194 | parse_reg(_nat, &_nat->imsi_allow_re, &_nat->imsi_allow, argc, argv); |
| 195 | return CMD_SUCCESS; |
| 196 | } |
| 197 | |
| 198 | DEFUN(cfg_nat_imsi_deny, |
| 199 | cfg_nat_imsi_deny_cmd, |
| 200 | "imsi deny [REGEXP]", |
| 201 | "Deny matching IMSIs to talk to the MSC. " |
| 202 | "The defualt is to not deny.") |
| 203 | { |
| 204 | parse_reg(_nat, &_nat->imsi_deny_re, &_nat->imsi_deny, argc, argv); |
| 205 | return CMD_SUCCESS; |
| 206 | } |
| 207 | |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 208 | DEFUN(cfg_nat_msc_ip, |
| 209 | cfg_nat_msc_ip_cmd, |
| 210 | "msc ip IP", |
| 211 | "Set the IP address of the MSC.") |
| 212 | { |
| 213 | bsc_nat_set_msc_ip(_nat, argv[0]); |
| 214 | return CMD_SUCCESS; |
| 215 | } |
| 216 | |
Holger Hans Peter Freyther | bace202 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 217 | DEFUN(cfg_nat_msc_port, |
| 218 | cfg_nat_msc_port_cmd, |
| 219 | "msc port <1-65500>", |
| 220 | "Set the port of the MSC.") |
| 221 | { |
| 222 | _nat->msc_port = atoi(argv[0]); |
| 223 | return CMD_SUCCESS; |
| 224 | } |
| 225 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 226 | /* per BSC configuration */ |
| 227 | DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n") |
| 228 | { |
| 229 | int bsc_nr = atoi(argv[0]); |
| 230 | struct bsc_config *bsc; |
| 231 | |
| 232 | if (bsc_nr > _nat->num_bsc) { |
| 233 | vty_out(vty, "%% The next unused BSC number is %u%s", |
| 234 | _nat->num_bsc, VTY_NEWLINE); |
| 235 | return CMD_WARNING; |
| 236 | } else if (bsc_nr == _nat->num_bsc) { |
| 237 | /* allocate a new one */ |
| 238 | bsc = bsc_config_alloc(_nat, "unknown", 0); |
| 239 | } else |
| 240 | bsc = bsc_config_num(_nat, bsc_nr); |
| 241 | |
| 242 | if (!bsc) |
| 243 | return CMD_WARNING; |
| 244 | |
| 245 | vty->index = bsc; |
| 246 | vty->node = BSC_NODE; |
| 247 | |
| 248 | return CMD_SUCCESS; |
| 249 | } |
| 250 | |
| 251 | DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token") |
| 252 | { |
| 253 | struct bsc_config *conf = vty->index; |
| 254 | |
| 255 | if (conf->token) |
| 256 | talloc_free(conf->token); |
| 257 | conf->token = talloc_strdup(conf, argv[0]); |
| 258 | return CMD_SUCCESS; |
| 259 | } |
| 260 | |
| 261 | DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>", |
| 262 | "Set the Location Area Code (LAC) of this BSC\n") |
| 263 | { |
Holger Hans Peter Freyther | 435a5c1 | 2010-03-30 06:08:56 +0200 | [diff] [blame] | 264 | struct bsc_config *tmp; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 265 | struct bsc_config *conf = vty->index; |
| 266 | |
| 267 | int lac = atoi(argv[0]); |
| 268 | |
| 269 | if (lac < 0 || lac > 0xffff) { |
| 270 | vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s", |
| 271 | lac, VTY_NEWLINE); |
| 272 | return CMD_WARNING; |
| 273 | } |
| 274 | |
| 275 | if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) { |
| 276 | vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s", |
| 277 | lac, VTY_NEWLINE); |
| 278 | return CMD_WARNING; |
| 279 | } |
| 280 | |
Holger Hans Peter Freyther | 435a5c1 | 2010-03-30 06:08:56 +0200 | [diff] [blame] | 281 | /* verify that the LACs are unique */ |
| 282 | llist_for_each_entry(tmp, &_nat->bsc_configs, entry) { |
| 283 | if (tmp->lac == lac) { |
| 284 | vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE); |
| 285 | return CMD_ERR_INCOMPLETE; |
| 286 | } |
| 287 | } |
| 288 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 289 | conf->lac = lac; |
| 290 | |
| 291 | return CMD_SUCCESS; |
| 292 | } |
| 293 | |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 294 | DEFUN(cfg_bsc_imsi_allow, |
| 295 | cfg_bsc_imsi_allow_cmd, |
| 296 | "imsi allow [REGEXP]", |
| 297 | "Allow IMSIs with the following network to talk to the MSC." |
| 298 | "The default is to allow everyone)") |
| 299 | { |
| 300 | struct bsc_config *conf = vty->index; |
| 301 | |
| 302 | parse_reg(conf, &conf->imsi_allow_re, &conf->imsi_allow, argc, argv); |
| 303 | return CMD_SUCCESS; |
| 304 | } |
| 305 | |
| 306 | DEFUN(cfg_bsc_imsi_deny, |
| 307 | cfg_bsc_imsi_deny_cmd, |
| 308 | "imsi deny [REGEXP]", |
| 309 | "Deny IMSIs with the following network to talk to the MSC." |
| 310 | "The default is to not deny anyone.)") |
| 311 | { |
| 312 | struct bsc_config *conf = vty->index; |
| 313 | |
| 314 | parse_reg(conf, &conf->imsi_deny_re, &conf->imsi_deny, argc, argv); |
| 315 | return CMD_SUCCESS; |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 318 | int bsc_nat_vty_init(struct bsc_nat *nat) |
| 319 | { |
| 320 | _nat = nat; |
| 321 | |
| 322 | cmd_init(1); |
| 323 | vty_init(); |
| 324 | |
| 325 | /* show commands */ |
| 326 | install_element(VIEW_NODE, &show_sccp_cmd); |
| 327 | install_element(VIEW_NODE, &show_bsc_cmd); |
| 328 | install_element(VIEW_NODE, &show_bsc_cfg_cmd); |
Holger Hans Peter Freyther | 02e1c7d | 2010-04-12 12:33:27 +0200 | [diff] [blame] | 329 | install_element(VIEW_NODE, &show_stats_cmd); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 330 | |
Holger Hans Peter Freyther | b372fad | 2010-04-06 12:01:15 +0200 | [diff] [blame] | 331 | openbsc_vty_add_cmds(); |
| 332 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 333 | /* nat group */ |
| 334 | install_element(CONFIG_NODE, &cfg_nat_cmd); |
| 335 | install_node(&nat_node, config_write_nat); |
| 336 | install_default(NAT_NODE); |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 337 | install_element(NAT_NODE, &cfg_nat_imsi_allow_cmd); |
| 338 | install_element(NAT_NODE, &cfg_nat_imsi_deny_cmd); |
Holger Hans Peter Freyther | 9226d70 | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 339 | install_element(NAT_NODE, &cfg_nat_msc_ip_cmd); |
Holger Hans Peter Freyther | bace202 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 340 | install_element(NAT_NODE, &cfg_nat_msc_port_cmd); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 341 | |
| 342 | /* BSC subgroups */ |
| 343 | install_element(NAT_NODE, &cfg_bsc_cmd); |
| 344 | install_node(&bsc_node, config_write_bsc); |
| 345 | install_default(BSC_NODE); |
| 346 | install_element(BSC_NODE, &cfg_bsc_token_cmd); |
| 347 | install_element(BSC_NODE, &cfg_bsc_lac_cmd); |
Holger Hans Peter Freyther | 4b6da56 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 348 | install_element(BSC_NODE, &cfg_bsc_imsi_allow_cmd); |
| 349 | install_element(BSC_NODE, &cfg_bsc_imsi_deny_cmd); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 350 | |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 351 | mgcp_vty_init(); |
| 352 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /* called by the telnet interface... we have our own init above */ |
| 358 | void bsc_vty_init() |
| 359 | {} |