blob: a91da7c4e10e90e69f2e5a561f50e3fdd4af323e [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);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +020054 if (_nat->imsi_allow)
55 vty_out(vty, " imsi allow %s%s", _nat->imsi_allow, VTY_NEWLINE);
56 if (_nat->imsi_deny)
57 vty_out(vty, " insi deny %s%s", _nat->imsi_deny, VTY_NEWLINE);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +080058 vty_out(vty, " msc ip %s%s", _nat->msc_ip, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080059 return CMD_SUCCESS;
60}
61
62static void config_write_bsc_single(struct vty *vty, struct bsc_config *bsc)
63{
64 vty_out(vty, " bsc %u%s", bsc->nr, VTY_NEWLINE);
65 vty_out(vty, " token %s%s", bsc->token, VTY_NEWLINE);
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020066 vty_out(vty, " location_area_code %u%s", bsc->lac, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +020067 if (bsc->imsi_allow)
68 vty_out(vty, " imsi allow %s%s", bsc->imsi_allow, VTY_NEWLINE);
69 if (bsc->imsi_deny)
70 vty_out(vty, " imsi deny %s%s", bsc->imsi_deny, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080071}
72
73static int config_write_bsc(struct vty *vty)
74{
75 struct bsc_config *bsc;
76
77 llist_for_each_entry(bsc, &_nat->bsc_configs, entry)
78 config_write_bsc_single(vty, bsc);
79 return CMD_SUCCESS;
80}
81
82
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +020083DEFUN(show_sccp, show_sccp_cmd, "sccp connections show",
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080084 SHOW_STR "Display information about current SCCP connections")
85{
86 struct sccp_connections *con;
87 llist_for_each_entry(con, &_nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +020088 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",
89 con->bsc->cfg ? con->bsc->cfg->nr : -1,
90 con->bsc->cfg ? con->bsc->cfg->lac : -1,
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080091 sccp_src_ref_to_int(&con->real_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020092 sccp_src_ref_to_int(&con->patched_ref),
93 con->msc_timeslot, con->bsc_timeslot,
94 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080095 }
96
97 return CMD_SUCCESS;
98}
99
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +0200100DEFUN(show_bsc, show_bsc_cmd, "bsc connections show",
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800101 SHOW_STR "Display information about current BSCs")
102{
103 struct bsc_connection *con;
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200104 struct sockaddr_in sock;
105 socklen_t len = sizeof(sock);
106
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800107 llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200108 getpeername(con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
109 vty_out(vty, "BSC lac: %d, %d auth: %d fd: %d peername: %s%s",
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200110 con->cfg ? con->cfg->nr : -1,
111 con->cfg ? con->cfg->lac : -1,
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200112 con->authenticated, con->write_queue.bfd.fd,
113 inet_ntoa(sock.sin_addr), VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800114 }
115
116 return CMD_SUCCESS;
117}
118
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +0200119DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "bsc config show",
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800120 SHOW_STR "Display information about known BSC configs")
121{
122 struct bsc_config *conf;
123 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
124 vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s",
125 conf->token, conf->lac, conf->nr, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200126 vty_out(vty, " imsi_allow: '%s' imsi_deny: '%s'%s",
127 conf->imsi_allow ? conf->imsi_allow: "any",
128 conf->imsi_deny ? conf->imsi_deny : "none",
129 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800130 }
131
132 return CMD_SUCCESS;
133}
134
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200135DEFUN(show_stats,
136 show_stats_cmd,
137 "show statistics",
138 SHOW_STR "Display network statistics\n")
139{
140 struct bsc_config *conf;
141
142 vty_out(vty, "NAT statistics%s", VTY_NEWLINE);
143 vty_out(vty, " SCCP Connections %lu total, %lu calls%s",
144 counter_get(_nat->stats.sccp.conn),
145 counter_get(_nat->stats.sccp.calls), VTY_NEWLINE);
146 vty_out(vty, " MSC Connections %lu%s",
147 counter_get(_nat->stats.msc.reconn), VTY_NEWLINE);
148 vty_out(vty, " BSC Connections %lu total, %lu auth failed.%s",
149 counter_get(_nat->stats.bsc.reconn),
150 counter_get(_nat->stats.bsc.auth_fail), VTY_NEWLINE);
151
152 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
153 vty_out(vty, " BSC lac: %d nr: %d%s",
154 conf->lac, conf->nr, VTY_NEWLINE);
155 vty_out(vty, " SCCP Connnections %lu total, %lu calls%s",
156 counter_get(conf->stats.sccp.conn),
157 counter_get(conf->stats.sccp.calls), VTY_NEWLINE);
158 vty_out(vty, " BSC Connections %lu total%s",
159 counter_get(conf->stats.net.reconn), VTY_NEWLINE);
160 }
161
162 return CMD_SUCCESS;
163}
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800164
165DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT")
166{
167 vty->index = _nat;
168 vty->node = NAT_NODE;
169
170 return CMD_SUCCESS;
171}
172
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200173static void parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
174{
175 if (*imsi) {
176 talloc_free(*imsi);
177 *imsi = NULL;
178 }
179 regfree(reg);
180
181 if (argc > 0) {
182 *imsi = talloc_strdup(ctx, argv[0]);
183 regcomp(reg, argv[0], 0);
184 }
185}
186
187DEFUN(cfg_nat_imsi_allow,
188 cfg_nat_imsi_allow_cmd,
189 "imsi allow [REGEXP]",
190 "Allow matching IMSIs to talk to the MSC. "
191 "The defualt is to allow everyone.")
192{
193 parse_reg(_nat, &_nat->imsi_allow_re, &_nat->imsi_allow, argc, argv);
194 return CMD_SUCCESS;
195}
196
197DEFUN(cfg_nat_imsi_deny,
198 cfg_nat_imsi_deny_cmd,
199 "imsi deny [REGEXP]",
200 "Deny matching IMSIs to talk to the MSC. "
201 "The defualt is to not deny.")
202{
203 parse_reg(_nat, &_nat->imsi_deny_re, &_nat->imsi_deny, argc, argv);
204 return CMD_SUCCESS;
205}
206
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800207DEFUN(cfg_nat_msc_ip,
208 cfg_nat_msc_ip_cmd,
209 "msc ip IP",
210 "Set the IP address of the MSC.")
211{
212 bsc_nat_set_msc_ip(_nat, argv[0]);
213 return CMD_SUCCESS;
214}
215
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800216/* per BSC configuration */
217DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n")
218{
219 int bsc_nr = atoi(argv[0]);
220 struct bsc_config *bsc;
221
222 if (bsc_nr > _nat->num_bsc) {
223 vty_out(vty, "%% The next unused BSC number is %u%s",
224 _nat->num_bsc, VTY_NEWLINE);
225 return CMD_WARNING;
226 } else if (bsc_nr == _nat->num_bsc) {
227 /* allocate a new one */
228 bsc = bsc_config_alloc(_nat, "unknown", 0);
229 } else
230 bsc = bsc_config_num(_nat, bsc_nr);
231
232 if (!bsc)
233 return CMD_WARNING;
234
235 vty->index = bsc;
236 vty->node = BSC_NODE;
237
238 return CMD_SUCCESS;
239}
240
241DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
242{
243 struct bsc_config *conf = vty->index;
244
245 if (conf->token)
246 talloc_free(conf->token);
247 conf->token = talloc_strdup(conf, argv[0]);
248 return CMD_SUCCESS;
249}
250
251DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
252 "Set the Location Area Code (LAC) of this BSC\n")
253{
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200254 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800255 struct bsc_config *conf = vty->index;
256
257 int lac = atoi(argv[0]);
258
259 if (lac < 0 || lac > 0xffff) {
260 vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
261 lac, VTY_NEWLINE);
262 return CMD_WARNING;
263 }
264
265 if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
266 vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
267 lac, VTY_NEWLINE);
268 return CMD_WARNING;
269 }
270
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200271 /* verify that the LACs are unique */
272 llist_for_each_entry(tmp, &_nat->bsc_configs, entry) {
273 if (tmp->lac == lac) {
274 vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE);
275 return CMD_ERR_INCOMPLETE;
276 }
277 }
278
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800279 conf->lac = lac;
280
281 return CMD_SUCCESS;
282}
283
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200284DEFUN(cfg_bsc_imsi_allow,
285 cfg_bsc_imsi_allow_cmd,
286 "imsi allow [REGEXP]",
287 "Allow IMSIs with the following network to talk to the MSC."
288 "The default is to allow everyone)")
289{
290 struct bsc_config *conf = vty->index;
291
292 parse_reg(conf, &conf->imsi_allow_re, &conf->imsi_allow, argc, argv);
293 return CMD_SUCCESS;
294}
295
296DEFUN(cfg_bsc_imsi_deny,
297 cfg_bsc_imsi_deny_cmd,
298 "imsi deny [REGEXP]",
299 "Deny IMSIs with the following network to talk to the MSC."
300 "The default is to not deny anyone.)")
301{
302 struct bsc_config *conf = vty->index;
303
304 parse_reg(conf, &conf->imsi_deny_re, &conf->imsi_deny, argc, argv);
305 return CMD_SUCCESS;
306}
307
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800308int bsc_nat_vty_init(struct bsc_nat *nat)
309{
310 _nat = nat;
311
312 cmd_init(1);
313 vty_init();
314
315 /* show commands */
316 install_element(VIEW_NODE, &show_sccp_cmd);
317 install_element(VIEW_NODE, &show_bsc_cmd);
318 install_element(VIEW_NODE, &show_bsc_cfg_cmd);
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200319 install_element(VIEW_NODE, &show_stats_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800320
Holger Hans Peter Freyther5f291d52010-04-06 12:01:15 +0200321 openbsc_vty_add_cmds();
322
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800323 /* nat group */
324 install_element(CONFIG_NODE, &cfg_nat_cmd);
325 install_node(&nat_node, config_write_nat);
326 install_default(NAT_NODE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200327 install_element(NAT_NODE, &cfg_nat_imsi_allow_cmd);
328 install_element(NAT_NODE, &cfg_nat_imsi_deny_cmd);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800329 install_element(NAT_NODE, &cfg_nat_msc_ip_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800330
331 /* BSC subgroups */
332 install_element(NAT_NODE, &cfg_bsc_cmd);
333 install_node(&bsc_node, config_write_bsc);
334 install_default(BSC_NODE);
335 install_element(BSC_NODE, &cfg_bsc_token_cmd);
336 install_element(BSC_NODE, &cfg_bsc_lac_cmd);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200337 install_element(BSC_NODE, &cfg_bsc_imsi_allow_cmd);
338 install_element(BSC_NODE, &cfg_bsc_imsi_deny_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800339
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800340 mgcp_vty_init();
341
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800342 return 0;
343}
344
345
346/* called by the telnet interface... we have our own init above */
347void bsc_vty_init()
348{}