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