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