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); |
| 54 | return CMD_SUCCESS; |
| 55 | } |
| 56 | |
| 57 | static void config_write_bsc_single(struct vty *vty, struct bsc_config *bsc) |
| 58 | { |
| 59 | vty_out(vty, " bsc %u%s", bsc->nr, VTY_NEWLINE); |
| 60 | vty_out(vty, " token %s%s", bsc->token, VTY_NEWLINE); |
| 61 | vty_out(vty, " lac %u%s", bsc->lac, VTY_NEWLINE); |
| 62 | } |
| 63 | |
| 64 | static int config_write_bsc(struct vty *vty) |
| 65 | { |
| 66 | struct bsc_config *bsc; |
| 67 | |
| 68 | llist_for_each_entry(bsc, &_nat->bsc_configs, entry) |
| 69 | config_write_bsc_single(vty, bsc); |
| 70 | return CMD_SUCCESS; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | DEFUN(show_sccp, show_sccp_cmd, "show connections sccp", |
| 75 | SHOW_STR "Display information about current SCCP connections") |
| 76 | { |
| 77 | struct sccp_connections *con; |
| 78 | llist_for_each_entry(con, &_nat->sccp_connections, list_entry) { |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 79 | 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", |
| 80 | con->bsc->cfg ? con->bsc->cfg->nr : -1, |
| 81 | con->bsc->cfg ? con->bsc->cfg->lac : -1, |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 82 | sccp_src_ref_to_int(&con->real_ref), |
Holger Hans Peter Freyther | 32aba23 | 2010-04-05 10:10:33 +0200 | [diff] [blame] | 83 | sccp_src_ref_to_int(&con->patched_ref), |
| 84 | con->msc_timeslot, con->bsc_timeslot, |
| 85 | VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return CMD_SUCCESS; |
| 89 | } |
| 90 | |
| 91 | DEFUN(show_bsc, show_bsc_cmd, "show connections bsc", |
| 92 | SHOW_STR "Display information about current BSCs") |
| 93 | { |
| 94 | struct bsc_connection *con; |
| 95 | llist_for_each_entry(con, &_nat->bsc_connections, list_entry) { |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 96 | vty_out(vty, "BSC lac: %d, %d auth: %d fd: %d%s", |
| 97 | con->cfg ? con->cfg->nr : -1, |
| 98 | con->cfg ? con->cfg->lac : -1, |
| 99 | con->authenticated, con->write_queue.bfd.fd, VTY_NEWLINE); |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return CMD_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "show bsc config", |
| 106 | SHOW_STR "Display information about known BSC configs") |
| 107 | { |
| 108 | struct bsc_config *conf; |
| 109 | llist_for_each_entry(conf, &_nat->bsc_configs, entry) { |
| 110 | vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s", |
| 111 | conf->token, conf->lac, conf->nr, VTY_NEWLINE); |
| 112 | } |
| 113 | |
| 114 | return CMD_SUCCESS; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT") |
| 119 | { |
| 120 | vty->index = _nat; |
| 121 | vty->node = NAT_NODE; |
| 122 | |
| 123 | return CMD_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | /* per BSC configuration */ |
| 127 | DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n") |
| 128 | { |
| 129 | int bsc_nr = atoi(argv[0]); |
| 130 | struct bsc_config *bsc; |
| 131 | |
| 132 | if (bsc_nr > _nat->num_bsc) { |
| 133 | vty_out(vty, "%% The next unused BSC number is %u%s", |
| 134 | _nat->num_bsc, VTY_NEWLINE); |
| 135 | return CMD_WARNING; |
| 136 | } else if (bsc_nr == _nat->num_bsc) { |
| 137 | /* allocate a new one */ |
| 138 | bsc = bsc_config_alloc(_nat, "unknown", 0); |
| 139 | } else |
| 140 | bsc = bsc_config_num(_nat, bsc_nr); |
| 141 | |
| 142 | if (!bsc) |
| 143 | return CMD_WARNING; |
| 144 | |
| 145 | vty->index = bsc; |
| 146 | vty->node = BSC_NODE; |
| 147 | |
| 148 | return CMD_SUCCESS; |
| 149 | } |
| 150 | |
| 151 | DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token") |
| 152 | { |
| 153 | struct bsc_config *conf = vty->index; |
| 154 | |
| 155 | if (conf->token) |
| 156 | talloc_free(conf->token); |
| 157 | conf->token = talloc_strdup(conf, argv[0]); |
| 158 | return CMD_SUCCESS; |
| 159 | } |
| 160 | |
| 161 | DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>", |
| 162 | "Set the Location Area Code (LAC) of this BSC\n") |
| 163 | { |
Holger Hans Peter Freyther | 435a5c1 | 2010-03-30 06:08:56 +0200 | [diff] [blame] | 164 | struct bsc_config *tmp; |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 165 | struct bsc_config *conf = vty->index; |
| 166 | |
| 167 | int lac = atoi(argv[0]); |
| 168 | |
| 169 | if (lac < 0 || lac > 0xffff) { |
| 170 | vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s", |
| 171 | lac, VTY_NEWLINE); |
| 172 | return CMD_WARNING; |
| 173 | } |
| 174 | |
| 175 | if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) { |
| 176 | vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s", |
| 177 | lac, VTY_NEWLINE); |
| 178 | return CMD_WARNING; |
| 179 | } |
| 180 | |
Holger Hans Peter Freyther | 435a5c1 | 2010-03-30 06:08:56 +0200 | [diff] [blame] | 181 | /* verify that the LACs are unique */ |
| 182 | llist_for_each_entry(tmp, &_nat->bsc_configs, entry) { |
| 183 | if (tmp->lac == lac) { |
| 184 | vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE); |
| 185 | return CMD_ERR_INCOMPLETE; |
| 186 | } |
| 187 | } |
| 188 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 189 | conf->lac = lac; |
| 190 | |
| 191 | return CMD_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | int bsc_nat_vty_init(struct bsc_nat *nat) |
| 195 | { |
| 196 | _nat = nat; |
| 197 | |
| 198 | cmd_init(1); |
| 199 | vty_init(); |
| 200 | |
| 201 | /* show commands */ |
| 202 | install_element(VIEW_NODE, &show_sccp_cmd); |
| 203 | install_element(VIEW_NODE, &show_bsc_cmd); |
| 204 | install_element(VIEW_NODE, &show_bsc_cfg_cmd); |
| 205 | |
Holger Hans Peter Freyther | b372fad | 2010-04-06 12:01:15 +0200 | [diff] [blame] | 206 | openbsc_vty_add_cmds(); |
| 207 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 208 | /* nat group */ |
| 209 | install_element(CONFIG_NODE, &cfg_nat_cmd); |
| 210 | install_node(&nat_node, config_write_nat); |
| 211 | install_default(NAT_NODE); |
| 212 | |
| 213 | /* BSC subgroups */ |
| 214 | install_element(NAT_NODE, &cfg_bsc_cmd); |
| 215 | install_node(&bsc_node, config_write_bsc); |
| 216 | install_default(BSC_NODE); |
| 217 | install_element(BSC_NODE, &cfg_bsc_token_cmd); |
| 218 | install_element(BSC_NODE, &cfg_bsc_lac_cmd); |
| 219 | |
Holger Hans Peter Freyther | 3660e4d | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 220 | mgcp_vty_init(); |
| 221 | |
Holger Hans Peter Freyther | 5e54788 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* called by the telnet interface... we have our own init above */ |
| 227 | void bsc_vty_init() |
| 228 | {} |