blob: a03b5d178df2ca9b842b8e7eb4233b5eaa188790 [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 Freyther81395532010-04-17 07:48:45 +020059 vty_out(vty, " msc port %d%s", _nat->msc_port, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080060 return CMD_SUCCESS;
61}
62
63static void config_write_bsc_single(struct vty *vty, struct bsc_config *bsc)
64{
65 vty_out(vty, " bsc %u%s", bsc->nr, VTY_NEWLINE);
66 vty_out(vty, " token %s%s", bsc->token, VTY_NEWLINE);
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020067 vty_out(vty, " location_area_code %u%s", bsc->lac, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +020068 if (bsc->imsi_allow)
69 vty_out(vty, " imsi allow %s%s", bsc->imsi_allow, VTY_NEWLINE);
70 if (bsc->imsi_deny)
71 vty_out(vty, " imsi deny %s%s", bsc->imsi_deny, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080072}
73
74static int config_write_bsc(struct vty *vty)
75{
76 struct bsc_config *bsc;
77
78 llist_for_each_entry(bsc, &_nat->bsc_configs, entry)
79 config_write_bsc_single(vty, bsc);
80 return CMD_SUCCESS;
81}
82
83
Holger Hans Peter Freyther01a0b1b2010-04-19 16:06:43 +080084DEFUN(show_sccp, show_sccp_cmd, "show sccp connections",
85 SHOW_STR "Display information about current SCCP connections")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080086{
87 struct sccp_connections *con;
88 llist_for_each_entry(con, &_nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +020089 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",
90 con->bsc->cfg ? con->bsc->cfg->nr : -1,
91 con->bsc->cfg ? con->bsc->cfg->lac : -1,
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080092 sccp_src_ref_to_int(&con->real_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020093 sccp_src_ref_to_int(&con->patched_ref),
94 con->msc_timeslot, con->bsc_timeslot,
95 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080096 }
97
98 return CMD_SUCCESS;
99}
100
Holger Hans Peter Freyther01a0b1b2010-04-19 16:06:43 +0800101DEFUN(show_bsc, show_bsc_cmd, "show bsc connections",
102 SHOW_STR "Display information about current BSCs")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800103{
104 struct bsc_connection *con;
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200105 struct sockaddr_in sock;
106 socklen_t len = sizeof(sock);
107
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800108 llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200109 getpeername(con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
110 vty_out(vty, "BSC lac: %d, %d auth: %d fd: %d peername: %s%s",
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200111 con->cfg ? con->cfg->nr : -1,
112 con->cfg ? con->cfg->lac : -1,
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200113 con->authenticated, con->write_queue.bfd.fd,
114 inet_ntoa(sock.sin_addr), VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800115 }
116
117 return CMD_SUCCESS;
118}
119
Holger Hans Peter Freyther57f874a2010-04-12 07:29:22 +0200120DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "bsc config show",
Holger Freytherd6b616a2010-04-17 23:10:34 +0800121 "Display information about known BSC configs")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800122{
123 struct bsc_config *conf;
124 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
125 vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s",
126 conf->token, conf->lac, conf->nr, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200127 vty_out(vty, " imsi_allow: '%s' imsi_deny: '%s'%s",
128 conf->imsi_allow ? conf->imsi_allow: "any",
129 conf->imsi_deny ? conf->imsi_deny : "none",
130 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800131 }
132
133 return CMD_SUCCESS;
134}
135
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200136DEFUN(show_stats,
137 show_stats_cmd,
138 "show statistics",
139 SHOW_STR "Display network statistics\n")
140{
141 struct bsc_config *conf;
142
143 vty_out(vty, "NAT statistics%s", VTY_NEWLINE);
144 vty_out(vty, " SCCP Connections %lu total, %lu calls%s",
145 counter_get(_nat->stats.sccp.conn),
146 counter_get(_nat->stats.sccp.calls), VTY_NEWLINE);
147 vty_out(vty, " MSC Connections %lu%s",
148 counter_get(_nat->stats.msc.reconn), VTY_NEWLINE);
149 vty_out(vty, " BSC Connections %lu total, %lu auth failed.%s",
150 counter_get(_nat->stats.bsc.reconn),
151 counter_get(_nat->stats.bsc.auth_fail), VTY_NEWLINE);
152
153 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
154 vty_out(vty, " BSC lac: %d nr: %d%s",
155 conf->lac, conf->nr, VTY_NEWLINE);
156 vty_out(vty, " SCCP Connnections %lu total, %lu calls%s",
157 counter_get(conf->stats.sccp.conn),
158 counter_get(conf->stats.sccp.calls), VTY_NEWLINE);
159 vty_out(vty, " BSC Connections %lu total%s",
160 counter_get(conf->stats.net.reconn), VTY_NEWLINE);
161 }
162
163 return CMD_SUCCESS;
164}
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800165
166DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT")
167{
168 vty->index = _nat;
169 vty->node = NAT_NODE;
170
171 return CMD_SUCCESS;
172}
173
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200174static void parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
175{
176 if (*imsi) {
177 talloc_free(*imsi);
178 *imsi = NULL;
179 }
180 regfree(reg);
181
182 if (argc > 0) {
183 *imsi = talloc_strdup(ctx, argv[0]);
184 regcomp(reg, argv[0], 0);
185 }
186}
187
188DEFUN(cfg_nat_imsi_allow,
189 cfg_nat_imsi_allow_cmd,
190 "imsi allow [REGEXP]",
191 "Allow matching IMSIs to talk to the MSC. "
192 "The defualt is to allow everyone.")
193{
194 parse_reg(_nat, &_nat->imsi_allow_re, &_nat->imsi_allow, argc, argv);
195 return CMD_SUCCESS;
196}
197
198DEFUN(cfg_nat_imsi_deny,
199 cfg_nat_imsi_deny_cmd,
200 "imsi deny [REGEXP]",
201 "Deny matching IMSIs to talk to the MSC. "
202 "The defualt is to not deny.")
203{
204 parse_reg(_nat, &_nat->imsi_deny_re, &_nat->imsi_deny, argc, argv);
205 return CMD_SUCCESS;
206}
207
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800208DEFUN(cfg_nat_msc_ip,
209 cfg_nat_msc_ip_cmd,
210 "msc ip IP",
211 "Set the IP address of the MSC.")
212{
213 bsc_nat_set_msc_ip(_nat, argv[0]);
214 return CMD_SUCCESS;
215}
216
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200217DEFUN(cfg_nat_msc_port,
218 cfg_nat_msc_port_cmd,
219 "msc port <1-65500>",
220 "Set the port of the MSC.")
221{
222 _nat->msc_port = atoi(argv[0]);
223 return CMD_SUCCESS;
224}
225
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800226/* per BSC configuration */
227DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n")
228{
229 int bsc_nr = atoi(argv[0]);
230 struct bsc_config *bsc;
231
232 if (bsc_nr > _nat->num_bsc) {
233 vty_out(vty, "%% The next unused BSC number is %u%s",
234 _nat->num_bsc, VTY_NEWLINE);
235 return CMD_WARNING;
236 } else if (bsc_nr == _nat->num_bsc) {
237 /* allocate a new one */
238 bsc = bsc_config_alloc(_nat, "unknown", 0);
239 } else
240 bsc = bsc_config_num(_nat, bsc_nr);
241
242 if (!bsc)
243 return CMD_WARNING;
244
245 vty->index = bsc;
246 vty->node = BSC_NODE;
247
248 return CMD_SUCCESS;
249}
250
251DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
252{
253 struct bsc_config *conf = vty->index;
254
255 if (conf->token)
256 talloc_free(conf->token);
257 conf->token = talloc_strdup(conf, argv[0]);
258 return CMD_SUCCESS;
259}
260
261DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
262 "Set the Location Area Code (LAC) of this BSC\n")
263{
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200264 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800265 struct bsc_config *conf = vty->index;
266
267 int lac = atoi(argv[0]);
268
269 if (lac < 0 || lac > 0xffff) {
270 vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
271 lac, VTY_NEWLINE);
272 return CMD_WARNING;
273 }
274
275 if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
276 vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
277 lac, VTY_NEWLINE);
278 return CMD_WARNING;
279 }
280
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200281 /* verify that the LACs are unique */
282 llist_for_each_entry(tmp, &_nat->bsc_configs, entry) {
283 if (tmp->lac == lac) {
284 vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE);
285 return CMD_ERR_INCOMPLETE;
286 }
287 }
288
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800289 conf->lac = lac;
290
291 return CMD_SUCCESS;
292}
293
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200294DEFUN(cfg_bsc_imsi_allow,
295 cfg_bsc_imsi_allow_cmd,
296 "imsi allow [REGEXP]",
297 "Allow IMSIs with the following network to talk to the MSC."
298 "The default is to allow everyone)")
299{
300 struct bsc_config *conf = vty->index;
301
302 parse_reg(conf, &conf->imsi_allow_re, &conf->imsi_allow, argc, argv);
303 return CMD_SUCCESS;
304}
305
306DEFUN(cfg_bsc_imsi_deny,
307 cfg_bsc_imsi_deny_cmd,
308 "imsi deny [REGEXP]",
309 "Deny IMSIs with the following network to talk to the MSC."
310 "The default is to not deny anyone.)")
311{
312 struct bsc_config *conf = vty->index;
313
314 parse_reg(conf, &conf->imsi_deny_re, &conf->imsi_deny, argc, argv);
315 return CMD_SUCCESS;
316}
317
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800318int bsc_nat_vty_init(struct bsc_nat *nat)
319{
320 _nat = nat;
321
322 cmd_init(1);
323 vty_init();
324
325 /* show commands */
326 install_element(VIEW_NODE, &show_sccp_cmd);
327 install_element(VIEW_NODE, &show_bsc_cmd);
328 install_element(VIEW_NODE, &show_bsc_cfg_cmd);
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200329 install_element(VIEW_NODE, &show_stats_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800330
Holger Hans Peter Freyther5f291d52010-04-06 12:01:15 +0200331 openbsc_vty_add_cmds();
332
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800333 /* nat group */
334 install_element(CONFIG_NODE, &cfg_nat_cmd);
335 install_node(&nat_node, config_write_nat);
336 install_default(NAT_NODE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200337 install_element(NAT_NODE, &cfg_nat_imsi_allow_cmd);
338 install_element(NAT_NODE, &cfg_nat_imsi_deny_cmd);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800339 install_element(NAT_NODE, &cfg_nat_msc_ip_cmd);
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200340 install_element(NAT_NODE, &cfg_nat_msc_port_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800341
342 /* BSC subgroups */
343 install_element(NAT_NODE, &cfg_bsc_cmd);
344 install_node(&bsc_node, config_write_bsc);
345 install_default(BSC_NODE);
346 install_element(BSC_NODE, &cfg_bsc_token_cmd);
347 install_element(BSC_NODE, &cfg_bsc_lac_cmd);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200348 install_element(BSC_NODE, &cfg_bsc_imsi_allow_cmd);
349 install_element(BSC_NODE, &cfg_bsc_imsi_deny_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800350
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800351 mgcp_vty_init();
352
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800353 return 0;
354}
355
356
357/* called by the telnet interface... we have our own init above */
358void bsc_vty_init()
359{}