blob: d5f83ebdd3cc153c7ee083ef4c9bb1c9f7bbaa11 [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
74DEFUN(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 Freyther560af502010-04-05 18:03:39 +020079 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 Freyther9a85ef32010-06-15 18:46:11 +080080 con->bsc->lac,
81 sccp_src_ref_to_int(&con->real_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020082 sccp_src_ref_to_int(&con->patched_ref),
83 con->msc_timeslot, con->bsc_timeslot,
84 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080085 }
86
87 return CMD_SUCCESS;
88}
89
90DEFUN(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 Freythered07a3f2010-06-15 18:47:10 +080096 con->lac, con->authenticated, con->write_queue.bfd.fd, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080097 }
98
99 return CMD_SUCCESS;
100}
101
102DEFUN(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
115DEFUN(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 */
124DEFUN(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
148DEFUN(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
158DEFUN(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 Freyther39ee8772010-03-30 06:08:56 +0200161 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800162 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 Freyther39ee8772010-03-30 06:08:56 +0200178 /* 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 Freyther9a85ef32010-06-15 18:46:11 +0800186 conf->lac = lac;
187
188 return CMD_SUCCESS;
189}
190
191int 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 Freyther5f291d52010-04-06 12:01:15 +0200203 openbsc_vty_add_cmds();
204
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800205 /* 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 Freytherf7d33352010-06-15 18:50:26 +0800217 mgcp_vty_init();
218
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800219 return 0;
220}
221
222
223/* called by the telnet interface... we have our own init above */
224void bsc_vty_init()
225{}