blob: 6ed1e724d1fda56617f1513df5725134dfbd8215 [file] [log] [blame]
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +08001/* 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 Freytherf7d33352010-06-15 18:50:26 +080028#include <openbsc/mgcp.h>
Holger Hans Peter Freyther5f291d52010-04-06 12:01:15 +020029#include <openbsc/vty.h>
Holger Hans Peter Freyther6c45f2e2010-06-15 19:06:18 +080030
31#include <osmocore/talloc.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080032
33#include <sccp/sccp.h>
34
35#include <stdlib.h>
36
37static struct bsc_nat *_nat;
38
39static struct cmd_node nat_node = {
40 NAT_NODE,
41 "%s(nat)#",
42 1,
43};
44
45static struct cmd_node bsc_node = {
46 BSC_NODE,
47 "%s(bsc)#",
48 1,
49};
50
51static int config_write_nat(struct vty *vty)
52{
53 vty_out(vty, "nat%s", VTY_NEWLINE);
54 return CMD_SUCCESS;
55}
56
57static 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
64static 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
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +020074DEFUN(show_sccp, show_sccp_cmd, "sccp connections show",
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080075 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 Freyther47dd4942010-04-06 15:11:34 +020079 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 Freyther9a85ef32010-06-15 18:46:11 +080082 sccp_src_ref_to_int(&con->real_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020083 sccp_src_ref_to_int(&con->patched_ref),
84 con->msc_timeslot, con->bsc_timeslot,
85 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080086 }
87
88 return CMD_SUCCESS;
89}
90
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +020091DEFUN(show_bsc, show_bsc_cmd, "bsc connections show",
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080092 SHOW_STR "Display information about current BSCs")
93{
94 struct bsc_connection *con;
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +020095 struct sockaddr_in sock;
96 socklen_t len = sizeof(sock);
97
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080098 llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +020099 getpeername(con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
100 vty_out(vty, "BSC lac: %d, %d auth: %d fd: %d peername: %s%s",
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200101 con->cfg ? con->cfg->nr : -1,
102 con->cfg ? con->cfg->lac : -1,
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200103 con->authenticated, con->write_queue.bfd.fd,
104 inet_ntoa(sock.sin_addr), VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800105 }
106
107 return CMD_SUCCESS;
108}
109
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +0200110DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "bsc config show",
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800111 SHOW_STR "Display information about known BSC configs")
112{
113 struct bsc_config *conf;
114 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
115 vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s",
116 conf->token, conf->lac, conf->nr, VTY_NEWLINE);
117 }
118
119 return CMD_SUCCESS;
120}
121
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200122DEFUN(show_stats,
123 show_stats_cmd,
124 "show statistics",
125 SHOW_STR "Display network statistics\n")
126{
127 struct bsc_config *conf;
128
129 vty_out(vty, "NAT statistics%s", VTY_NEWLINE);
130 vty_out(vty, " SCCP Connections %lu total, %lu calls%s",
131 counter_get(_nat->stats.sccp.conn),
132 counter_get(_nat->stats.sccp.calls), VTY_NEWLINE);
133 vty_out(vty, " MSC Connections %lu%s",
134 counter_get(_nat->stats.msc.reconn), VTY_NEWLINE);
135 vty_out(vty, " BSC Connections %lu total, %lu auth failed.%s",
136 counter_get(_nat->stats.bsc.reconn),
137 counter_get(_nat->stats.bsc.auth_fail), VTY_NEWLINE);
138
139 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
140 vty_out(vty, " BSC lac: %d nr: %d%s",
141 conf->lac, conf->nr, VTY_NEWLINE);
142 vty_out(vty, " SCCP Connnections %lu total, %lu calls%s",
143 counter_get(conf->stats.sccp.conn),
144 counter_get(conf->stats.sccp.calls), VTY_NEWLINE);
145 vty_out(vty, " BSC Connections %lu total%s",
146 counter_get(conf->stats.net.reconn), VTY_NEWLINE);
147 }
148
149 return CMD_SUCCESS;
150}
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800151
152DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT")
153{
154 vty->index = _nat;
155 vty->node = NAT_NODE;
156
157 return CMD_SUCCESS;
158}
159
160/* per BSC configuration */
161DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n")
162{
163 int bsc_nr = atoi(argv[0]);
164 struct bsc_config *bsc;
165
166 if (bsc_nr > _nat->num_bsc) {
167 vty_out(vty, "%% The next unused BSC number is %u%s",
168 _nat->num_bsc, VTY_NEWLINE);
169 return CMD_WARNING;
170 } else if (bsc_nr == _nat->num_bsc) {
171 /* allocate a new one */
172 bsc = bsc_config_alloc(_nat, "unknown", 0);
173 } else
174 bsc = bsc_config_num(_nat, bsc_nr);
175
176 if (!bsc)
177 return CMD_WARNING;
178
179 vty->index = bsc;
180 vty->node = BSC_NODE;
181
182 return CMD_SUCCESS;
183}
184
185DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
186{
187 struct bsc_config *conf = vty->index;
188
189 if (conf->token)
190 talloc_free(conf->token);
191 conf->token = talloc_strdup(conf, argv[0]);
192 return CMD_SUCCESS;
193}
194
195DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
196 "Set the Location Area Code (LAC) of this BSC\n")
197{
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200198 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800199 struct bsc_config *conf = vty->index;
200
201 int lac = atoi(argv[0]);
202
203 if (lac < 0 || lac > 0xffff) {
204 vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
205 lac, VTY_NEWLINE);
206 return CMD_WARNING;
207 }
208
209 if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
210 vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
211 lac, VTY_NEWLINE);
212 return CMD_WARNING;
213 }
214
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200215 /* verify that the LACs are unique */
216 llist_for_each_entry(tmp, &_nat->bsc_configs, entry) {
217 if (tmp->lac == lac) {
218 vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE);
219 return CMD_ERR_INCOMPLETE;
220 }
221 }
222
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800223 conf->lac = lac;
224
225 return CMD_SUCCESS;
226}
227
228int bsc_nat_vty_init(struct bsc_nat *nat)
229{
230 _nat = nat;
231
232 cmd_init(1);
233 vty_init();
234
235 /* show commands */
236 install_element(VIEW_NODE, &show_sccp_cmd);
237 install_element(VIEW_NODE, &show_bsc_cmd);
238 install_element(VIEW_NODE, &show_bsc_cfg_cmd);
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200239 install_element(VIEW_NODE, &show_stats_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800240
Holger Hans Peter Freyther5f291d52010-04-06 12:01:15 +0200241 openbsc_vty_add_cmds();
242
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800243 /* nat group */
244 install_element(CONFIG_NODE, &cfg_nat_cmd);
245 install_node(&nat_node, config_write_nat);
246 install_default(NAT_NODE);
247
248 /* BSC subgroups */
249 install_element(NAT_NODE, &cfg_bsc_cmd);
250 install_node(&bsc_node, config_write_bsc);
251 install_default(BSC_NODE);
252 install_element(BSC_NODE, &cfg_bsc_token_cmd);
253 install_element(BSC_NODE, &cfg_bsc_lac_cmd);
254
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800255 mgcp_vty_init();
256
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800257 return 0;
258}
259
260
261/* called by the telnet interface... we have our own init above */
262void bsc_vty_init()
263{}