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