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