blob: 7501767a21c0404d8265cf6e9e775f0137638268 [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 Freyther62e58432010-04-21 19:05:14 +080072 vty_out(vty, " paging forbidden %d%s", bsc->forbid_paging, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080073}
74
75static int config_write_bsc(struct vty *vty)
76{
77 struct bsc_config *bsc;
78
79 llist_for_each_entry(bsc, &_nat->bsc_configs, entry)
80 config_write_bsc_single(vty, bsc);
81 return CMD_SUCCESS;
82}
83
84
Holger Hans Peter Freyther01a0b1b2010-04-19 16:06:43 +080085DEFUN(show_sccp, show_sccp_cmd, "show sccp connections",
86 SHOW_STR "Display information about current SCCP connections")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080087{
88 struct sccp_connections *con;
Holger Hans Peter Freyther4bd22942010-04-27 13:11:18 +080089 vty_out(vty, "Listing all opening SCCP connections\n");
90
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080091 llist_for_each_entry(con, &_nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther4bd22942010-04-27 13:11:18 +080092 vty_out(vty, "For BSC Nr: %d lac: %d; BSC ref: 0x%x; MUX ref: 0x%x; Network has ref: %d ref: 0x%x MSC/BSC mux: 0x%x/0x%x%s",
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +020093 con->bsc->cfg ? con->bsc->cfg->nr : -1,
94 con->bsc->cfg ? con->bsc->cfg->lac : -1,
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080095 sccp_src_ref_to_int(&con->real_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020096 sccp_src_ref_to_int(&con->patched_ref),
Holger Hans Peter Freyther4bd22942010-04-27 13:11:18 +080097 con->has_remote_ref,
98 sccp_src_ref_to_int(&con->remote_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020099 con->msc_timeslot, con->bsc_timeslot,
100 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800101 }
102
103 return CMD_SUCCESS;
104}
105
Holger Hans Peter Freyther01a0b1b2010-04-19 16:06:43 +0800106DEFUN(show_bsc, show_bsc_cmd, "show bsc connections",
107 SHOW_STR "Display information about current BSCs")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800108{
109 struct bsc_connection *con;
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200110 struct sockaddr_in sock;
111 socklen_t len = sizeof(sock);
112
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800113 llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200114 getpeername(con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
115 vty_out(vty, "BSC lac: %d, %d auth: %d fd: %d peername: %s%s",
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200116 con->cfg ? con->cfg->nr : -1,
117 con->cfg ? con->cfg->lac : -1,
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200118 con->authenticated, con->write_queue.bfd.fd,
119 inet_ntoa(sock.sin_addr), VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800120 }
121
122 return CMD_SUCCESS;
123}
124
Holger Hans Peter Freytherab7539c2010-04-22 13:36:46 +0800125DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "show bsc config",
126 SHOW_STR "Display information about known BSC configs")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800127{
128 struct bsc_config *conf;
129 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
130 vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s",
131 conf->token, conf->lac, conf->nr, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200132 vty_out(vty, " imsi_allow: '%s' imsi_deny: '%s'%s",
133 conf->imsi_allow ? conf->imsi_allow: "any",
134 conf->imsi_deny ? conf->imsi_deny : "none",
135 VTY_NEWLINE);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800136 vty_out(vty, " paging forbidden: %d%s",
137 conf->forbid_paging, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800138 }
139
140 return CMD_SUCCESS;
141}
142
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200143DEFUN(show_stats,
144 show_stats_cmd,
145 "show statistics",
146 SHOW_STR "Display network statistics\n")
147{
148 struct bsc_config *conf;
149
150 vty_out(vty, "NAT statistics%s", VTY_NEWLINE);
151 vty_out(vty, " SCCP Connections %lu total, %lu calls%s",
152 counter_get(_nat->stats.sccp.conn),
153 counter_get(_nat->stats.sccp.calls), VTY_NEWLINE);
154 vty_out(vty, " MSC Connections %lu%s",
155 counter_get(_nat->stats.msc.reconn), VTY_NEWLINE);
156 vty_out(vty, " BSC Connections %lu total, %lu auth failed.%s",
157 counter_get(_nat->stats.bsc.reconn),
158 counter_get(_nat->stats.bsc.auth_fail), VTY_NEWLINE);
159
160 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
161 vty_out(vty, " BSC lac: %d nr: %d%s",
162 conf->lac, conf->nr, VTY_NEWLINE);
163 vty_out(vty, " SCCP Connnections %lu total, %lu calls%s",
164 counter_get(conf->stats.sccp.conn),
165 counter_get(conf->stats.sccp.calls), VTY_NEWLINE);
166 vty_out(vty, " BSC Connections %lu total%s",
167 counter_get(conf->stats.net.reconn), VTY_NEWLINE);
168 }
169
170 return CMD_SUCCESS;
171}
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800172
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800173DEFUN(close_bsc,
174 close_bsc_cmd,
175 "close bsc connection BSC_NR",
176 "Close the connection with the BSC identified by the config number.\n")
177{
178 struct bsc_connection *bsc;
179 int bsc_nr = atoi(argv[0]);
180
181 llist_for_each_entry(bsc, &_nat->bsc_connections, list_entry) {
182 if (!bsc->cfg || bsc->cfg->nr != bsc_nr)
183 continue;
184 bsc_close_connection(bsc);
185 break;
186 }
187
188 return CMD_SUCCESS;
189}
190
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800191DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT")
192{
193 vty->index = _nat;
194 vty->node = NAT_NODE;
195
196 return CMD_SUCCESS;
197}
198
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200199static void parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
200{
201 if (*imsi) {
202 talloc_free(*imsi);
203 *imsi = NULL;
204 }
205 regfree(reg);
206
207 if (argc > 0) {
208 *imsi = talloc_strdup(ctx, argv[0]);
209 regcomp(reg, argv[0], 0);
210 }
211}
212
213DEFUN(cfg_nat_imsi_allow,
214 cfg_nat_imsi_allow_cmd,
215 "imsi allow [REGEXP]",
216 "Allow matching IMSIs to talk to the MSC. "
217 "The defualt is to allow everyone.")
218{
219 parse_reg(_nat, &_nat->imsi_allow_re, &_nat->imsi_allow, argc, argv);
220 return CMD_SUCCESS;
221}
222
223DEFUN(cfg_nat_imsi_deny,
224 cfg_nat_imsi_deny_cmd,
225 "imsi deny [REGEXP]",
226 "Deny matching IMSIs to talk to the MSC. "
227 "The defualt is to not deny.")
228{
229 parse_reg(_nat, &_nat->imsi_deny_re, &_nat->imsi_deny, argc, argv);
230 return CMD_SUCCESS;
231}
232
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800233DEFUN(cfg_nat_msc_ip,
234 cfg_nat_msc_ip_cmd,
235 "msc ip IP",
236 "Set the IP address of the MSC.")
237{
238 bsc_nat_set_msc_ip(_nat, argv[0]);
239 return CMD_SUCCESS;
240}
241
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200242DEFUN(cfg_nat_msc_port,
243 cfg_nat_msc_port_cmd,
244 "msc port <1-65500>",
245 "Set the port of the MSC.")
246{
247 _nat->msc_port = atoi(argv[0]);
248 return CMD_SUCCESS;
249}
250
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800251/* per BSC configuration */
252DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n")
253{
254 int bsc_nr = atoi(argv[0]);
255 struct bsc_config *bsc;
256
257 if (bsc_nr > _nat->num_bsc) {
258 vty_out(vty, "%% The next unused BSC number is %u%s",
259 _nat->num_bsc, VTY_NEWLINE);
260 return CMD_WARNING;
261 } else if (bsc_nr == _nat->num_bsc) {
262 /* allocate a new one */
263 bsc = bsc_config_alloc(_nat, "unknown", 0);
264 } else
265 bsc = bsc_config_num(_nat, bsc_nr);
266
267 if (!bsc)
268 return CMD_WARNING;
269
270 vty->index = bsc;
271 vty->node = BSC_NODE;
272
273 return CMD_SUCCESS;
274}
275
276DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
277{
278 struct bsc_config *conf = vty->index;
279
280 if (conf->token)
281 talloc_free(conf->token);
282 conf->token = talloc_strdup(conf, argv[0]);
283 return CMD_SUCCESS;
284}
285
286DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
287 "Set the Location Area Code (LAC) of this BSC\n")
288{
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200289 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800290 struct bsc_config *conf = vty->index;
291
292 int lac = atoi(argv[0]);
293
294 if (lac < 0 || lac > 0xffff) {
295 vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
296 lac, VTY_NEWLINE);
297 return CMD_WARNING;
298 }
299
300 if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
301 vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
302 lac, VTY_NEWLINE);
303 return CMD_WARNING;
304 }
305
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200306 /* verify that the LACs are unique */
307 llist_for_each_entry(tmp, &_nat->bsc_configs, entry) {
308 if (tmp->lac == lac) {
309 vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE);
310 return CMD_ERR_INCOMPLETE;
311 }
312 }
313
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800314 conf->lac = lac;
315
316 return CMD_SUCCESS;
317}
318
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200319DEFUN(cfg_bsc_imsi_allow,
320 cfg_bsc_imsi_allow_cmd,
321 "imsi allow [REGEXP]",
322 "Allow IMSIs with the following network to talk to the MSC."
323 "The default is to allow everyone)")
324{
325 struct bsc_config *conf = vty->index;
326
327 parse_reg(conf, &conf->imsi_allow_re, &conf->imsi_allow, argc, argv);
328 return CMD_SUCCESS;
329}
330
331DEFUN(cfg_bsc_imsi_deny,
332 cfg_bsc_imsi_deny_cmd,
333 "imsi deny [REGEXP]",
334 "Deny IMSIs with the following network to talk to the MSC."
335 "The default is to not deny anyone.)")
336{
337 struct bsc_config *conf = vty->index;
338
339 parse_reg(conf, &conf->imsi_deny_re, &conf->imsi_deny, argc, argv);
340 return CMD_SUCCESS;
341}
342
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800343DEFUN(cfg_bsc_paging,
344 cfg_bsc_paging_cmd,
345 "paging forbidden (0|1)",
346 "Forbid sending PAGING REQUESTS to the BSC.")
347{
348 struct bsc_config *conf = vty->index;
349
Holger Hans Peter Freyther834f1df2010-04-21 20:07:07 +0800350 if (strcmp("1", argv[0]) == 0)
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800351 conf->forbid_paging = 1;
352 else
353 conf->forbid_paging = 0;
354
355 return CMD_SUCCESS;
356}
357
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800358int bsc_nat_vty_init(struct bsc_nat *nat)
359{
360 _nat = nat;
361
362 cmd_init(1);
363 vty_init();
364
365 /* show commands */
366 install_element(VIEW_NODE, &show_sccp_cmd);
367 install_element(VIEW_NODE, &show_bsc_cmd);
368 install_element(VIEW_NODE, &show_bsc_cfg_cmd);
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200369 install_element(VIEW_NODE, &show_stats_cmd);
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800370 install_element(VIEW_NODE, &close_bsc_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800371
Holger Hans Peter Freyther5f291d52010-04-06 12:01:15 +0200372 openbsc_vty_add_cmds();
373
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800374 /* nat group */
375 install_element(CONFIG_NODE, &cfg_nat_cmd);
376 install_node(&nat_node, config_write_nat);
377 install_default(NAT_NODE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200378 install_element(NAT_NODE, &cfg_nat_imsi_allow_cmd);
379 install_element(NAT_NODE, &cfg_nat_imsi_deny_cmd);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800380 install_element(NAT_NODE, &cfg_nat_msc_ip_cmd);
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200381 install_element(NAT_NODE, &cfg_nat_msc_port_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800382
383 /* BSC subgroups */
384 install_element(NAT_NODE, &cfg_bsc_cmd);
385 install_node(&bsc_node, config_write_bsc);
386 install_default(BSC_NODE);
387 install_element(BSC_NODE, &cfg_bsc_token_cmd);
388 install_element(BSC_NODE, &cfg_bsc_lac_cmd);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200389 install_element(BSC_NODE, &cfg_bsc_imsi_allow_cmd);
390 install_element(BSC_NODE, &cfg_bsc_imsi_deny_cmd);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800391 install_element(BSC_NODE, &cfg_bsc_paging_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800392
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800393 mgcp_vty_init();
394
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800395 return 0;
396}
397
398
399/* called by the telnet interface... we have our own init above */
400void bsc_vty_init()
401{}