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