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