blob: 6dae0d65ce1b48834df8e29272ec80fba68409e7 [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 Freyther6c45f2e2010-06-15 19:06:18 +080028
29#include <osmocore/talloc.h>
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080030
31#include <sccp/sccp.h>
32
33#include <stdlib.h>
34
35static struct bsc_nat *_nat;
36
37static struct cmd_node nat_node = {
38 NAT_NODE,
39 "%s(nat)#",
40 1,
41};
42
43static struct cmd_node bsc_node = {
44 BSC_NODE,
45 "%s(bsc)#",
46 1,
47};
48
49static int config_write_nat(struct vty *vty)
50{
51 vty_out(vty, "nat%s", VTY_NEWLINE);
52 return CMD_SUCCESS;
53}
54
55static 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
62static 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
72DEFUN(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
86DEFUN(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 Freythered07a3f2010-06-15 18:47:10 +080092 con->lac, con->authenticated, con->write_queue.bfd.fd, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080093 }
94
95 return CMD_SUCCESS;
96}
97
98DEFUN(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
111DEFUN(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 */
120DEFUN(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
144DEFUN(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
154DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
155 "Set the Location Area Code (LAC) of this BSC\n")
156{
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200157 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800158 struct bsc_config *conf = vty->index;
159
160 int lac = atoi(argv[0]);
161
162 if (lac < 0 || lac > 0xffff) {
163 vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
164 lac, VTY_NEWLINE);
165 return CMD_WARNING;
166 }
167
168 if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
169 vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
170 lac, VTY_NEWLINE);
171 return CMD_WARNING;
172 }
173
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200174 /* verify that the LACs are unique */
175 llist_for_each_entry(tmp, &_nat->bsc_configs, entry) {
176 if (tmp->lac == lac) {
177 vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE);
178 return CMD_ERR_INCOMPLETE;
179 }
180 }
181
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800182 conf->lac = lac;
183
184 return CMD_SUCCESS;
185}
186
187int bsc_nat_vty_init(struct bsc_nat *nat)
188{
189 _nat = nat;
190
191 cmd_init(1);
192 vty_init();
193
194 /* show commands */
195 install_element(VIEW_NODE, &show_sccp_cmd);
196 install_element(VIEW_NODE, &show_bsc_cmd);
197 install_element(VIEW_NODE, &show_bsc_cfg_cmd);
198
199 /* nat group */
200 install_element(CONFIG_NODE, &cfg_nat_cmd);
201 install_node(&nat_node, config_write_nat);
202 install_default(NAT_NODE);
203
204 /* BSC subgroups */
205 install_element(NAT_NODE, &cfg_bsc_cmd);
206 install_node(&bsc_node, config_write_bsc);
207 install_default(BSC_NODE);
208 install_element(BSC_NODE, &cfg_bsc_token_cmd);
209 install_element(BSC_NODE, &cfg_bsc_lac_cmd);
210
211 return 0;
212}
213
214
215/* called by the telnet interface... we have our own init above */
216void bsc_vty_init()
217{}