blob: ff6a5ecc0f1fbc715201f4701749b090724b4635 [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 Freytherda35a8d2010-05-05 16:57:38 +080060 vty_out(vty, " timeout auth %d%s", _nat->auth_timeout, VTY_NEWLINE);
61 vty_out(vty, " timeout ping %d%s", _nat->ping_timeout, VTY_NEWLINE);
62 vty_out(vty, " timeout pong %d%s", _nat->pong_timeout, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080063 return CMD_SUCCESS;
64}
65
66static void config_write_bsc_single(struct vty *vty, struct bsc_config *bsc)
67{
68 vty_out(vty, " bsc %u%s", bsc->nr, VTY_NEWLINE);
69 vty_out(vty, " token %s%s", bsc->token, VTY_NEWLINE);
Holger Hans Peter Freytherd1278c12010-04-16 16:52:20 +020070 vty_out(vty, " location_area_code %u%s", bsc->lac, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +020071 if (bsc->imsi_allow)
72 vty_out(vty, " imsi allow %s%s", bsc->imsi_allow, VTY_NEWLINE);
73 if (bsc->imsi_deny)
74 vty_out(vty, " imsi deny %s%s", bsc->imsi_deny, VTY_NEWLINE);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +080075 vty_out(vty, " paging forbidden %d%s", bsc->forbid_paging, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080076}
77
78static int config_write_bsc(struct vty *vty)
79{
80 struct bsc_config *bsc;
81
82 llist_for_each_entry(bsc, &_nat->bsc_configs, entry)
83 config_write_bsc_single(vty, bsc);
84 return CMD_SUCCESS;
85}
86
87
Holger Hans Peter Freyther01a0b1b2010-04-19 16:06:43 +080088DEFUN(show_sccp, show_sccp_cmd, "show sccp connections",
89 SHOW_STR "Display information about current SCCP connections")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080090{
91 struct sccp_connections *con;
Holger Hans Peter Freytherb9af2fa2010-04-27 15:35:14 +080092 vty_out(vty, "Listing all opening SCCP connections%s", VTY_NEWLINE);
Holger Hans Peter Freyther4bd22942010-04-27 13:11:18 +080093
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080094 llist_for_each_entry(con, &_nat->sccp_connections, list_entry) {
Holger Hans Peter Freyther4bd22942010-04-27 13:11:18 +080095 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 +020096 con->bsc->cfg ? con->bsc->cfg->nr : -1,
97 con->bsc->cfg ? con->bsc->cfg->lac : -1,
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +080098 sccp_src_ref_to_int(&con->real_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +020099 sccp_src_ref_to_int(&con->patched_ref),
Holger Hans Peter Freyther4bd22942010-04-27 13:11:18 +0800100 con->has_remote_ref,
101 sccp_src_ref_to_int(&con->remote_ref),
Holger Hans Peter Freyther32d34362010-04-05 10:10:33 +0200102 con->msc_timeslot, con->bsc_timeslot,
103 VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800104 }
105
106 return CMD_SUCCESS;
107}
108
Holger Hans Peter Freyther01a0b1b2010-04-19 16:06:43 +0800109DEFUN(show_bsc, show_bsc_cmd, "show bsc connections",
110 SHOW_STR "Display information about current BSCs")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800111{
112 struct bsc_connection *con;
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200113 struct sockaddr_in sock;
114 socklen_t len = sizeof(sock);
115
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800116 llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200117 getpeername(con->write_queue.bfd.fd, (struct sockaddr *) &sock, &len);
Holger Hans Peter Freyther3b879812010-05-01 15:32:32 +0800118 vty_out(vty, "BSC nr: %d lac: %d auth: %d fd: %d peername: %s%s",
Holger Hans Peter Freyther47dd4942010-04-06 15:11:34 +0200119 con->cfg ? con->cfg->nr : -1,
120 con->cfg ? con->cfg->lac : -1,
Holger Hans Peter Freyther870663a2010-04-08 10:35:20 +0200121 con->authenticated, con->write_queue.bfd.fd,
122 inet_ntoa(sock.sin_addr), VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800123 }
124
125 return CMD_SUCCESS;
126}
127
Holger Hans Peter Freytherab7539c2010-04-22 13:36:46 +0800128DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "show bsc config",
129 SHOW_STR "Display information about known BSC configs")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800130{
131 struct bsc_config *conf;
132 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
133 vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s",
134 conf->token, conf->lac, conf->nr, VTY_NEWLINE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200135 vty_out(vty, " imsi_allow: '%s' imsi_deny: '%s'%s",
136 conf->imsi_allow ? conf->imsi_allow: "any",
137 conf->imsi_deny ? conf->imsi_deny : "none",
138 VTY_NEWLINE);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800139 vty_out(vty, " paging forbidden: %d%s",
140 conf->forbid_paging, VTY_NEWLINE);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800141 }
142
143 return CMD_SUCCESS;
144}
145
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200146DEFUN(show_stats,
147 show_stats_cmd,
Holger Hans Peter Freyther314191d2010-05-02 18:59:24 +0800148 "show statistics [NR]",
Holger Hans Peter Freytherb9af2fa2010-04-27 15:35:14 +0800149 SHOW_STR "Display network statistics")
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200150{
151 struct bsc_config *conf;
152
Holger Hans Peter Freyther314191d2010-05-02 18:59:24 +0800153 int nr = -1;
154
155 if (argc == 1)
156 nr = atoi(argv[0]);
157
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200158 vty_out(vty, "NAT statistics%s", VTY_NEWLINE);
159 vty_out(vty, " SCCP Connections %lu total, %lu calls%s",
160 counter_get(_nat->stats.sccp.conn),
161 counter_get(_nat->stats.sccp.calls), VTY_NEWLINE);
162 vty_out(vty, " MSC Connections %lu%s",
163 counter_get(_nat->stats.msc.reconn), VTY_NEWLINE);
164 vty_out(vty, " BSC Connections %lu total, %lu auth failed.%s",
165 counter_get(_nat->stats.bsc.reconn),
166 counter_get(_nat->stats.bsc.auth_fail), VTY_NEWLINE);
167
168 llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
Holger Hans Peter Freyther314191d2010-05-02 18:59:24 +0800169 if (argc == 1 && nr != conf->nr)
170 continue;
171
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200172 vty_out(vty, " BSC lac: %d nr: %d%s",
173 conf->lac, conf->nr, VTY_NEWLINE);
174 vty_out(vty, " SCCP Connnections %lu total, %lu calls%s",
175 counter_get(conf->stats.sccp.conn),
176 counter_get(conf->stats.sccp.calls), VTY_NEWLINE);
177 vty_out(vty, " BSC Connections %lu total%s",
178 counter_get(conf->stats.net.reconn), VTY_NEWLINE);
179 }
180
181 return CMD_SUCCESS;
182}
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800183
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800184DEFUN(close_bsc,
185 close_bsc_cmd,
186 "close bsc connection BSC_NR",
Holger Hans Peter Freytherb9af2fa2010-04-27 15:35:14 +0800187 "Close the connection with the BSC identified by the config number.")
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800188{
189 struct bsc_connection *bsc;
190 int bsc_nr = atoi(argv[0]);
191
192 llist_for_each_entry(bsc, &_nat->bsc_connections, list_entry) {
193 if (!bsc->cfg || bsc->cfg->nr != bsc_nr)
194 continue;
195 bsc_close_connection(bsc);
196 break;
197 }
198
199 return CMD_SUCCESS;
200}
201
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800202DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT")
203{
204 vty->index = _nat;
205 vty->node = NAT_NODE;
206
207 return CMD_SUCCESS;
208}
209
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200210static void parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv)
211{
212 if (*imsi) {
213 talloc_free(*imsi);
214 *imsi = NULL;
215 }
216 regfree(reg);
217
218 if (argc > 0) {
219 *imsi = talloc_strdup(ctx, argv[0]);
220 regcomp(reg, argv[0], 0);
221 }
222}
223
224DEFUN(cfg_nat_imsi_allow,
225 cfg_nat_imsi_allow_cmd,
226 "imsi allow [REGEXP]",
227 "Allow matching IMSIs to talk to the MSC. "
228 "The defualt is to allow everyone.")
229{
230 parse_reg(_nat, &_nat->imsi_allow_re, &_nat->imsi_allow, argc, argv);
231 return CMD_SUCCESS;
232}
233
234DEFUN(cfg_nat_imsi_deny,
235 cfg_nat_imsi_deny_cmd,
236 "imsi deny [REGEXP]",
237 "Deny matching IMSIs to talk to the MSC. "
238 "The defualt is to not deny.")
239{
240 parse_reg(_nat, &_nat->imsi_deny_re, &_nat->imsi_deny, argc, argv);
241 return CMD_SUCCESS;
242}
243
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800244DEFUN(cfg_nat_msc_ip,
245 cfg_nat_msc_ip_cmd,
246 "msc ip IP",
247 "Set the IP address of the MSC.")
248{
249 bsc_nat_set_msc_ip(_nat, argv[0]);
250 return CMD_SUCCESS;
251}
252
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200253DEFUN(cfg_nat_msc_port,
254 cfg_nat_msc_port_cmd,
255 "msc port <1-65500>",
256 "Set the port of the MSC.")
257{
258 _nat->msc_port = atoi(argv[0]);
259 return CMD_SUCCESS;
260}
261
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800262DEFUN(cfg_nat_auth_time,
263 cfg_nat_auth_time_cmd,
264 "timeout auth <1-256>",
265 "The time to wait for an auth response.")
266{
267 _nat->auth_timeout = atoi(argv[0]);
268 return CMD_SUCCESS;
269}
270
271DEFUN(cfg_nat_ping_time,
272 cfg_nat_ping_time_cmd,
273 "timeout ping NR",
274 "Send a ping every NR seconds. Negative to disable.")
275{
276 _nat->ping_timeout = atoi(argv[0]);
277 return CMD_SUCCESS;
278}
279
280DEFUN(cfg_nat_pong_time,
281 cfg_nat_pong_time_cmd,
282 "timeout pong NR",
283 "Wait NR seconds for the PONG response. Should be smaller than ping.")
284{
285 _nat->pong_timeout = atoi(argv[0]);
286 return CMD_SUCCESS;
287}
288
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800289/* per BSC configuration */
Holger Hans Peter Freytherb9af2fa2010-04-27 15:35:14 +0800290DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800291{
292 int bsc_nr = atoi(argv[0]);
293 struct bsc_config *bsc;
294
295 if (bsc_nr > _nat->num_bsc) {
296 vty_out(vty, "%% The next unused BSC number is %u%s",
297 _nat->num_bsc, VTY_NEWLINE);
298 return CMD_WARNING;
299 } else if (bsc_nr == _nat->num_bsc) {
300 /* allocate a new one */
301 bsc = bsc_config_alloc(_nat, "unknown", 0);
302 } else
303 bsc = bsc_config_num(_nat, bsc_nr);
304
305 if (!bsc)
306 return CMD_WARNING;
307
308 vty->index = bsc;
309 vty->node = BSC_NODE;
310
311 return CMD_SUCCESS;
312}
313
314DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
315{
316 struct bsc_config *conf = vty->index;
317
318 if (conf->token)
319 talloc_free(conf->token);
320 conf->token = talloc_strdup(conf, argv[0]);
321 return CMD_SUCCESS;
322}
323
324DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
Holger Hans Peter Freytherb9af2fa2010-04-27 15:35:14 +0800325 "Set the Location Area Code (LAC) of this BSC")
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800326{
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200327 struct bsc_config *tmp;
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800328 struct bsc_config *conf = vty->index;
329
330 int lac = atoi(argv[0]);
331
332 if (lac < 0 || lac > 0xffff) {
333 vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
334 lac, VTY_NEWLINE);
335 return CMD_WARNING;
336 }
337
338 if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
339 vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
340 lac, VTY_NEWLINE);
341 return CMD_WARNING;
342 }
343
Holger Hans Peter Freyther39ee8772010-03-30 06:08:56 +0200344 /* verify that the LACs are unique */
345 llist_for_each_entry(tmp, &_nat->bsc_configs, entry) {
346 if (tmp->lac == lac) {
347 vty_out(vty, "%% LAC %d is already used.%s", lac, VTY_NEWLINE);
348 return CMD_ERR_INCOMPLETE;
349 }
350 }
351
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800352 conf->lac = lac;
353
354 return CMD_SUCCESS;
355}
356
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200357DEFUN(cfg_bsc_imsi_allow,
358 cfg_bsc_imsi_allow_cmd,
359 "imsi allow [REGEXP]",
360 "Allow IMSIs with the following network to talk to the MSC."
361 "The default is to allow everyone)")
362{
363 struct bsc_config *conf = vty->index;
364
365 parse_reg(conf, &conf->imsi_allow_re, &conf->imsi_allow, argc, argv);
366 return CMD_SUCCESS;
367}
368
369DEFUN(cfg_bsc_imsi_deny,
370 cfg_bsc_imsi_deny_cmd,
371 "imsi deny [REGEXP]",
372 "Deny IMSIs with the following network to talk to the MSC."
373 "The default is to not deny anyone.)")
374{
375 struct bsc_config *conf = vty->index;
376
377 parse_reg(conf, &conf->imsi_deny_re, &conf->imsi_deny, argc, argv);
378 return CMD_SUCCESS;
379}
380
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800381DEFUN(cfg_bsc_paging,
382 cfg_bsc_paging_cmd,
383 "paging forbidden (0|1)",
384 "Forbid sending PAGING REQUESTS to the BSC.")
385{
386 struct bsc_config *conf = vty->index;
387
Holger Hans Peter Freyther834f1df2010-04-21 20:07:07 +0800388 if (strcmp("1", argv[0]) == 0)
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800389 conf->forbid_paging = 1;
390 else
391 conf->forbid_paging = 0;
392
393 return CMD_SUCCESS;
394}
395
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800396int bsc_nat_vty_init(struct bsc_nat *nat)
397{
398 _nat = nat;
399
400 cmd_init(1);
401 vty_init();
402
403 /* show commands */
404 install_element(VIEW_NODE, &show_sccp_cmd);
405 install_element(VIEW_NODE, &show_bsc_cmd);
406 install_element(VIEW_NODE, &show_bsc_cfg_cmd);
Holger Hans Peter Freytherbcfebb22010-04-12 12:33:27 +0200407 install_element(VIEW_NODE, &show_stats_cmd);
Holger Hans Peter Freyther2f9dcf02010-04-27 13:21:39 +0800408 install_element(VIEW_NODE, &close_bsc_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800409
Holger Hans Peter Freyther5f291d52010-04-06 12:01:15 +0200410 openbsc_vty_add_cmds();
411
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800412 /* nat group */
413 install_element(CONFIG_NODE, &cfg_nat_cmd);
414 install_node(&nat_node, config_write_nat);
415 install_default(NAT_NODE);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200416 install_element(NAT_NODE, &cfg_nat_imsi_allow_cmd);
417 install_element(NAT_NODE, &cfg_nat_imsi_deny_cmd);
Holger Hans Peter Freythera88742c2010-06-15 18:51:04 +0800418 install_element(NAT_NODE, &cfg_nat_msc_ip_cmd);
Holger Hans Peter Freyther81395532010-04-17 07:48:45 +0200419 install_element(NAT_NODE, &cfg_nat_msc_port_cmd);
Holger Hans Peter Freytherda35a8d2010-05-05 16:57:38 +0800420 install_element(NAT_NODE, &cfg_nat_auth_time_cmd);
421 install_element(NAT_NODE, &cfg_nat_ping_time_cmd);
422 install_element(NAT_NODE, &cfg_nat_pong_time_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800423
424 /* BSC subgroups */
425 install_element(NAT_NODE, &cfg_bsc_cmd);
426 install_node(&bsc_node, config_write_bsc);
427 install_default(BSC_NODE);
428 install_element(BSC_NODE, &cfg_bsc_token_cmd);
429 install_element(BSC_NODE, &cfg_bsc_lac_cmd);
Holger Hans Peter Freytherc16cf272010-04-13 09:24:37 +0200430 install_element(BSC_NODE, &cfg_bsc_imsi_allow_cmd);
431 install_element(BSC_NODE, &cfg_bsc_imsi_deny_cmd);
Holger Hans Peter Freyther62e58432010-04-21 19:05:14 +0800432 install_element(BSC_NODE, &cfg_bsc_paging_cmd);
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800433
Holger Hans Peter Freytherf7d33352010-06-15 18:50:26 +0800434 mgcp_vty_init();
435
Holger Hans Peter Freyther9a85ef32010-06-15 18:46:11 +0800436 return 0;
437}
438
439
440/* called by the telnet interface... we have our own init above */
441void bsc_vty_init()
442{}