[nat] Add VTY support to the BSC nat application

* Create struct bsc_nat and move the various lists into this structure
* Create the VTY code
* Call the VTY init and parsing code
* Create functions to create the types..
* Add some stuff into the bsc_connection to be used for the NAT with
  proper config files. E.g. to close the connection if the BSC does not
  respond to a given command.
This commit is contained in:
Holger Hans Peter Freyther 2010-06-15 18:46:11 +08:00
parent 9f8f3d09a6
commit 9a85ef3266
3 changed files with 324 additions and 13 deletions

View File

@ -27,6 +27,7 @@
#include "select.h"
#include "msgb.h"
#include "timer.h"
#define DIR_BSC 1
#define DIR_MSC 2
@ -75,6 +76,12 @@ struct bsc_connection {
/* the fd we use to communicate */
struct bsc_fd bsc_fd;
/* the LAC assigned to this connection */
unsigned int lac;
/* a timeout node */
struct timer_list id_timeout;
};
/*
@ -91,6 +98,38 @@ struct sccp_connections {
struct sccp_source_reference patched_ref;
};
/**
* One BSC entry in the config
*/
struct bsc_config {
struct llist_head entry;
char *token;
unsigned int lac;
int nr;
struct bsc_nat *nat;
};
/**
* the structure of the "nat" network
*/
struct bsc_nat {
/* active SCCP connections that need patching */
struct llist_head sccp_connections;
/* active BSC connections that need patching */
struct llist_head bsc_connections;
/* known BSC's */
struct llist_head bsc_configs;
int num_bsc;
};
/* create and init the structures */
struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac);
struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num);
/**
* parse the given message into the above structure
@ -101,5 +140,6 @@ struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg);
* filter based on IP Access header in both directions
*/
int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed);
int bsc_nat_vty_init(struct bsc_nat *nat);
#endif

View File

@ -43,19 +43,69 @@
#include <openbsc/ipaccess.h>
#include <openbsc/abis_nm.h>
#include <openbsc/talloc.h>
#include <openbsc/linuxlist.h>
#include <openbsc/telnet_interface.h>
#include <vty/vty.h>
#include <sccp/sccp.h>
static const char *config_file = "openbsc.cfg";
static const char *config_file = "bsc-nat.cfg";
static char *msc_address = "127.0.0.1";
static struct in_addr local_addr;
static struct bsc_fd msc_connection;
static struct bsc_fd bsc_connection;
static LLIST_HEAD(bsc_connections);
static LLIST_HEAD(sccp_connections);
static struct bsc_nat *nat;
static struct bsc_nat *bsc_nat_alloc(void)
{
struct bsc_nat *nat = talloc_zero(tall_bsc_ctx, struct bsc_nat);
if (!nat)
return NULL;
INIT_LLIST_HEAD(&nat->sccp_connections);
INIT_LLIST_HEAD(&nat->bsc_connections);
INIT_LLIST_HEAD(&nat->bsc_configs);
return nat;
}
static struct bsc_connection *bsc_connection_alloc(void)
{
struct bsc_connection *con = talloc_zero(nat, struct bsc_connection);
if (!con)
return NULL;
return con;
}
struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac)
{
struct bsc_config *conf = talloc_zero(nat, struct bsc_config);
if (!conf)
return NULL;
conf->token = talloc_strdup(conf, token);
conf->lac = lac;
conf->nr = nat->num_bsc;
conf->nat = nat;
llist_add(&conf->entry, &nat->bsc_configs);
++nat->num_bsc;
return conf;
}
struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num)
{
struct bsc_config *conf;
llist_for_each_entry(conf, &nat->bsc_configs, entry)
if (conf->nr == num)
return conf;
return NULL;
}
/*
* below are stubs we need to link
@ -95,7 +145,7 @@ static int sccp_ref_is_free(struct sccp_source_reference *ref)
{
struct sccp_connections *conn;
llist_for_each_entry(conn, &sccp_connections, list_entry) {
llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
if (memcmp(ref, &conn->patched_ref, sizeof(*ref)) == 0)
return -1;
}
@ -137,7 +187,7 @@ static int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, str
{
struct sccp_connections *conn;
conn = talloc_zero(tall_bsc_ctx, struct sccp_connections);
conn = talloc_zero(nat, struct sccp_connections);
if (!conn) {
LOGP(DNAT, LOGL_ERROR, "Memory allocation failure.\n");
return -1;
@ -157,7 +207,7 @@ static void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, st
{
struct sccp_connections *conn;
llist_for_each_entry(conn, &sccp_connections, list_entry) {
llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
if (memcmp(parsed->src_local_ref,
&conn->real_ref, sizeof(conn->real_ref)) == 0) {
if (bsc != conn->bsc) {
@ -178,7 +228,7 @@ static void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, st
static struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, struct bsc_nat_parsed *parsed)
{
struct sccp_connections *conn;
llist_for_each_entry(conn, &sccp_connections, list_entry) {
llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
if (memcmp(parsed->dest_local_ref,
&conn->real_ref, sizeof(*parsed->dest_local_ref)) == 0) {
memcpy(parsed->dest_local_ref,
@ -193,7 +243,7 @@ static struct bsc_connection *patch_sccp_src_ref_to_bsc(struct msgb *msg, struct
static struct bsc_connection *patch_sccp_src_ref_to_msc(struct msgb *msg, struct bsc_nat_parsed *parsed)
{
struct sccp_connections *conn;
llist_for_each_entry(conn, &sccp_connections, list_entry) {
llist_for_each_entry(conn, &nat->sccp_connections, list_entry) {
if (memcmp(parsed->src_local_ref,
&conn->real_ref, sizeof(*parsed->src_local_ref)) == 0) {
memcpy(parsed->src_local_ref,
@ -262,7 +312,7 @@ static int forward_sccp_to_bts(struct msgb *msg)
send_to_all:
/* currently send this to every BSC connected */
llist_for_each_entry(bsc, &bsc_connections, list_entry) {
llist_for_each_entry(bsc, &nat->bsc_connections, list_entry) {
rc = write(bsc->bsc_fd.fd, msg->data, msg->len);
/* try the next one */
@ -324,7 +374,7 @@ static void remove_bsc_connection(struct bsc_connection *connection)
llist_del(&connection->list_entry);
/* remove all SCCP connections */
llist_for_each_entry_safe(sccp_patch, tmp, &sccp_connections, list_entry) {
llist_for_each_entry_safe(sccp_patch, tmp, &nat->sccp_connections, list_entry) {
if (sccp_patch->bsc != connection)
continue;
@ -451,7 +501,7 @@ static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
/*
*
*/
bsc = talloc_zero(tall_bsc_ctx, struct bsc_connection);
bsc = bsc_connection_alloc();
if (!bsc) {
LOGP(DNAT, LOGL_ERROR, "Failed to allocate BSC struct.\n");
close(ret);
@ -470,7 +520,7 @@ static int ipaccess_listen_bsc_cb(struct bsc_fd *bfd, unsigned int what)
}
LOGP(DNAT, LOGL_INFO, "Registered new BSC\n");
llist_add(&bsc->list_entry, &bsc_connections);
llist_add(&bsc->list_entry, &nat->bsc_connections);
ipaccess_send_id_ack(ret);
return 0;
}
@ -600,6 +650,20 @@ int main(int argc, char** argv)
local_addr.s_addr = INADDR_ANY;
handle_options(argc, argv);
nat = bsc_nat_alloc();
if (!nat) {
fprintf(stderr, "Failed to allocate the BSC nat.\n");
return -4;
}
/* init vty and parse */
bsc_nat_vty_init(nat);
telnet_init(NULL, 4244);
if (vty_read_config_file(config_file) < 0) {
fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
return -3;
}
/* seed the PRNG */
srand(time(NULL));

View File

@ -0,0 +1,207 @@
/* OpenBSC NAT interface to quagga VTY */
/* (C) 2010 by Holger Hans Peter Freyther
* (C) 2010 by On-Waves
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <vty/command.h>
#include <vty/buffer.h>
#include <vty/vty.h>
#include <openbsc/bsc_nat.h>
#include <openbsc/gsm_04_08.h>
#include <openbsc/talloc.h>
#include <sccp/sccp.h>
#include <stdlib.h>
static struct bsc_nat *_nat;
static struct cmd_node nat_node = {
NAT_NODE,
"%s(nat)#",
1,
};
static struct cmd_node bsc_node = {
BSC_NODE,
"%s(bsc)#",
1,
};
static int config_write_nat(struct vty *vty)
{
vty_out(vty, "nat%s", VTY_NEWLINE);
return CMD_SUCCESS;
}
static void config_write_bsc_single(struct vty *vty, struct bsc_config *bsc)
{
vty_out(vty, " bsc %u%s", bsc->nr, VTY_NEWLINE);
vty_out(vty, " token %s%s", bsc->token, VTY_NEWLINE);
vty_out(vty, " lac %u%s", bsc->lac, VTY_NEWLINE);
}
static int config_write_bsc(struct vty *vty)
{
struct bsc_config *bsc;
llist_for_each_entry(bsc, &_nat->bsc_configs, entry)
config_write_bsc_single(vty, bsc);
return CMD_SUCCESS;
}
DEFUN(show_sccp, show_sccp_cmd, "show connections sccp",
SHOW_STR "Display information about current SCCP connections")
{
struct sccp_connections *con;
llist_for_each_entry(con, &_nat->sccp_connections, list_entry) {
vty_out(vty, "SCCP for BSC: %d BSC ref: %u Local ref: %u%s",
con->bsc->lac,
sccp_src_ref_to_int(&con->real_ref),
sccp_src_ref_to_int(&con->patched_ref), VTY_NEWLINE);
}
return CMD_SUCCESS;
}
DEFUN(show_bsc, show_bsc_cmd, "show connections bsc",
SHOW_STR "Display information about current BSCs")
{
struct bsc_connection *con;
llist_for_each_entry(con, &_nat->bsc_connections, list_entry) {
vty_out(vty, "BSC lac: %d auth: %d fd: %d%s",
con->lac, con->authenticated, con->bsc_fd.fd, VTY_NEWLINE);
}
return CMD_SUCCESS;
}
DEFUN(show_bsc_cfg, show_bsc_cfg_cmd, "show bsc config",
SHOW_STR "Display information about known BSC configs")
{
struct bsc_config *conf;
llist_for_each_entry(conf, &_nat->bsc_configs, entry) {
vty_out(vty, "BSC token: '%s' lac: %u nr: %u%s",
conf->token, conf->lac, conf->nr, VTY_NEWLINE);
}
return CMD_SUCCESS;
}
DEFUN(cfg_nat, cfg_nat_cmd, "nat", "Configute the NAT")
{
vty->index = _nat;
vty->node = NAT_NODE;
return CMD_SUCCESS;
}
/* per BSC configuration */
DEFUN(cfg_bsc, cfg_bsc_cmd, "bsc BSC_NR", "Select a BSC to configure\n")
{
int bsc_nr = atoi(argv[0]);
struct bsc_config *bsc;
if (bsc_nr > _nat->num_bsc) {
vty_out(vty, "%% The next unused BSC number is %u%s",
_nat->num_bsc, VTY_NEWLINE);
return CMD_WARNING;
} else if (bsc_nr == _nat->num_bsc) {
/* allocate a new one */
bsc = bsc_config_alloc(_nat, "unknown", 0);
} else
bsc = bsc_config_num(_nat, bsc_nr);
if (!bsc)
return CMD_WARNING;
vty->index = bsc;
vty->node = BSC_NODE;
return CMD_SUCCESS;
}
DEFUN(cfg_bsc_token, cfg_bsc_token_cmd, "token TOKEN", "Set the token")
{
struct bsc_config *conf = vty->index;
if (conf->token)
talloc_free(conf->token);
conf->token = talloc_strdup(conf, argv[0]);
return CMD_SUCCESS;
}
DEFUN(cfg_bsc_lac, cfg_bsc_lac_cmd, "location_area_code <0-65535>",
"Set the Location Area Code (LAC) of this BSC\n")
{
struct bsc_config *conf = vty->index;
int lac = atoi(argv[0]);
if (lac < 0 || lac > 0xffff) {
vty_out(vty, "%% LAC %d is not in the valid range (0-65535)%s",
lac, VTY_NEWLINE);
return CMD_WARNING;
}
if (lac == GSM_LAC_RESERVED_DETACHED || lac == GSM_LAC_RESERVED_ALL_BTS) {
vty_out(vty, "%% LAC %d is reserved by GSM 04.08%s",
lac, VTY_NEWLINE);
return CMD_WARNING;
}
conf->lac = lac;
return CMD_SUCCESS;
}
int bsc_nat_vty_init(struct bsc_nat *nat)
{
_nat = nat;
cmd_init(1);
vty_init();
/* show commands */
install_element(VIEW_NODE, &show_sccp_cmd);
install_element(VIEW_NODE, &show_bsc_cmd);
install_element(VIEW_NODE, &show_bsc_cfg_cmd);
/* nat group */
install_element(CONFIG_NODE, &cfg_nat_cmd);
install_node(&nat_node, config_write_nat);
install_default(NAT_NODE);
/* BSC subgroups */
install_element(NAT_NODE, &cfg_bsc_cmd);
install_node(&bsc_node, config_write_bsc);
install_default(BSC_NODE);
install_element(BSC_NODE, &cfg_bsc_token_cmd);
install_element(BSC_NODE, &cfg_bsc_lac_cmd);
return 0;
}
/* called by the telnet interface... we have our own init above */
void bsc_vty_init()
{}