2010-06-15 10:45:38 +00:00
|
|
|
/*
|
|
|
|
* BSC NAT Message filtering
|
|
|
|
*
|
|
|
|
* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2010-06-15 10:46:56 +00:00
|
|
|
* (C) 2010 by On-Waves
|
2010-06-15 10:45:38 +00:00
|
|
|
*
|
|
|
|
* 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 <openbsc/debug.h>
|
|
|
|
#include <openbsc/gsm_data.h>
|
|
|
|
#include <openbsc/bsc_nat.h>
|
2010-07-30 21:17:17 +00:00
|
|
|
#include <openbsc/bsc_nat_sccp.h>
|
2010-06-15 10:45:38 +00:00
|
|
|
|
2010-06-15 10:48:36 +00:00
|
|
|
#include <osmocore/talloc.h>
|
|
|
|
|
2010-07-29 18:53:14 +00:00
|
|
|
#include <sccp/sccp.h>
|
|
|
|
|
2010-06-15 10:45:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/* test messages for ipa */
|
2010-07-23 11:08:55 +00:00
|
|
|
static uint8_t ipa_id[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x01, 0xfe, 0x06,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* SCCP messages are below */
|
2010-07-23 11:08:55 +00:00
|
|
|
static uint8_t gsm_reset[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x12, 0xfd,
|
|
|
|
0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
|
|
|
|
0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
|
|
|
|
0x01, 0x20,
|
|
|
|
};
|
|
|
|
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t gsm_reset_ack[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x13, 0xfd,
|
|
|
|
0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
|
|
|
|
0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
|
|
|
|
0x00, 0x01, 0x31,
|
|
|
|
};
|
|
|
|
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t gsm_paging[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x20, 0xfd,
|
|
|
|
0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
|
|
|
|
0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10,
|
|
|
|
0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10,
|
|
|
|
0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* BSC -> MSC connection open */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t bssmap_cr[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x2c, 0xfd,
|
|
|
|
0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02,
|
|
|
|
0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05,
|
|
|
|
0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3,
|
|
|
|
0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33,
|
|
|
|
0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01,
|
|
|
|
0x31, 0x97, 0x61, 0x00
|
|
|
|
};
|
|
|
|
|
|
|
|
/* MSC -> BSC connection confirm */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t bssmap_cc[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x0a, 0xfd,
|
|
|
|
0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* MSC -> BSC released */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t bssmap_released[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x0e, 0xfd,
|
|
|
|
0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f,
|
|
|
|
0x02, 0x23, 0x42, 0x00,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* BSC -> MSC released */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t bssmap_release_complete[] = {
|
2010-06-15 10:45:38 +00:00
|
|
|
0x00, 0x07, 0xfd,
|
|
|
|
0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
|
|
|
|
};
|
|
|
|
|
2010-04-06 08:22:01 +00:00
|
|
|
/* both directions IT timer */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t connnection_it[] = {
|
2010-04-06 08:22:01 +00:00
|
|
|
0x00, 0x0b, 0xfd,
|
|
|
|
0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
|
|
|
|
0x00, 0x00, 0x00, 0x00,
|
|
|
|
};
|
|
|
|
|
2010-04-21 07:45:26 +00:00
|
|
|
/* error in both directions */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t proto_error[] = {
|
2010-04-21 07:45:26 +00:00
|
|
|
0x00, 0x05, 0xfd,
|
|
|
|
0x0f, 0x22, 0x33, 0x44, 0x00,
|
|
|
|
};
|
|
|
|
|
2010-04-01 06:47:12 +00:00
|
|
|
/* MGCP wrap... */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t mgcp_msg[] = {
|
2010-04-01 06:47:12 +00:00
|
|
|
0x00, 0x03, 0xfc,
|
|
|
|
0x20, 0x20, 0x20,
|
|
|
|
};
|
|
|
|
|
2010-05-14 11:49:35 +00:00
|
|
|
/* location updating request */
|
2010-07-23 11:08:55 +00:00
|
|
|
static const uint8_t bss_lu[] = {
|
2010-05-14 11:49:35 +00:00
|
|
|
0x00, 0x2e, 0xfd,
|
|
|
|
0x01, 0x91, 0x45, 0x14, 0x02, 0x02, 0x04, 0x02,
|
|
|
|
0x42, 0xfe, 0x0f, 0x21, 0x00, 0x1f, 0x57, 0x05,
|
|
|
|
0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x14, 0xc3,
|
|
|
|
0x50, 0x17, 0x12, 0x05, 0x08, 0x70, 0x72, 0xf4,
|
|
|
|
0x80, 0xff, 0xfe, 0x30, 0x08, 0x29, 0x44, 0x50,
|
|
|
|
0x12, 0x03, 0x24, 0x01, 0x95, 0x00
|
|
|
|
};
|
|
|
|
|
2010-05-15 15:54:04 +00:00
|
|
|
/* paging response */
|
|
|
|
static const uint8_t pag_resp[] = {
|
|
|
|
0x00, 0x2c, 0xfd, 0x01, 0xe5, 0x68,
|
|
|
|
0x14, 0x02, 0x02, 0x04, 0x02, 0x42, 0xfe, 0x0f,
|
|
|
|
0x1f, 0x00, 0x1d, 0x57, 0x05, 0x08, 0x00, 0x72,
|
|
|
|
0xf4, 0x80, 0x20, 0x16, 0xc3, 0x50, 0x17, 0x10,
|
|
|
|
0x06, 0x27, 0x01, 0x03, 0x30, 0x18, 0x96, 0x08,
|
|
|
|
0x29, 0x26, 0x30, 0x32, 0x11, 0x42, 0x01, 0x19,
|
|
|
|
0x00
|
|
|
|
};
|
|
|
|
|
2010-06-15 10:45:38 +00:00
|
|
|
struct filter_result {
|
2010-07-23 11:08:55 +00:00
|
|
|
const uint8_t *data;
|
2010-07-23 11:09:21 +00:00
|
|
|
const uint16_t length;
|
2010-01-30 10:53:30 +00:00
|
|
|
const int dir;
|
2010-06-15 10:45:38 +00:00
|
|
|
const int result;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct filter_result results[] = {
|
|
|
|
{
|
|
|
|
.data = ipa_id,
|
|
|
|
.length = ARRAY_SIZE(ipa_id),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 1,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = gsm_reset,
|
|
|
|
.length = ARRAY_SIZE(gsm_reset),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 1,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = gsm_reset_ack,
|
|
|
|
.length = ARRAY_SIZE(gsm_reset_ack),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_BSC,
|
|
|
|
.result = 1,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = gsm_paging,
|
|
|
|
.length = ARRAY_SIZE(gsm_paging),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_BSC,
|
|
|
|
.result = 0,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = bssmap_cr,
|
|
|
|
.length = ARRAY_SIZE(bssmap_cr),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 0,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = bssmap_cc,
|
|
|
|
.length = ARRAY_SIZE(bssmap_cc),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_BSC,
|
|
|
|
.result = 0,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = bssmap_released,
|
|
|
|
.length = ARRAY_SIZE(bssmap_released),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 0,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = bssmap_release_complete,
|
|
|
|
.length = ARRAY_SIZE(bssmap_release_complete),
|
2010-01-30 10:53:30 +00:00
|
|
|
.dir = DIR_BSC,
|
|
|
|
.result = 0,
|
2010-06-15 10:45:38 +00:00
|
|
|
},
|
2010-04-01 06:47:12 +00:00
|
|
|
{
|
|
|
|
.data = mgcp_msg,
|
|
|
|
.length = ARRAY_SIZE(mgcp_msg),
|
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 0,
|
|
|
|
},
|
2010-04-06 08:22:01 +00:00
|
|
|
{
|
|
|
|
.data = connnection_it,
|
|
|
|
.length = ARRAY_SIZE(connnection_it),
|
|
|
|
.dir = DIR_BSC,
|
|
|
|
.result = 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = connnection_it,
|
|
|
|
.length = ARRAY_SIZE(connnection_it),
|
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 0,
|
|
|
|
},
|
2010-04-21 07:45:26 +00:00
|
|
|
{
|
|
|
|
.data = proto_error,
|
|
|
|
.length = ARRAY_SIZE(proto_error),
|
|
|
|
.dir = DIR_BSC,
|
|
|
|
.result = 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = proto_error,
|
|
|
|
.length = ARRAY_SIZE(proto_error),
|
|
|
|
.dir = DIR_MSC,
|
|
|
|
.result = 0,
|
|
|
|
},
|
|
|
|
|
2010-06-15 10:45:38 +00:00
|
|
|
};
|
|
|
|
|
2010-06-15 10:48:36 +00:00
|
|
|
static void test_filter(void)
|
2010-06-15 10:45:38 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
|
|
/* start testinh with proper messages */
|
2010-06-15 10:48:36 +00:00
|
|
|
fprintf(stderr, "Testing BSS Filtering.\n");
|
2010-06-15 10:45:38 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(results); ++i) {
|
|
|
|
int result;
|
|
|
|
struct bsc_nat_parsed *parsed;
|
|
|
|
struct msgb *msg = msgb_alloc(4096, "test-message");
|
|
|
|
|
|
|
|
fprintf(stderr, "Going to test item: %d\n", i);
|
|
|
|
memcpy(msg->data, results[i].data, results[i].length);
|
|
|
|
msg->l2h = msgb_put(msg, results[i].length);
|
|
|
|
|
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
if (!parsed) {
|
|
|
|
fprintf(stderr, "FAIL: Failed to parse the message\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-01-30 10:53:30 +00:00
|
|
|
result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
|
2010-06-15 10:45:38 +00:00
|
|
|
if (result != results[i].result) {
|
|
|
|
fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n",
|
|
|
|
result, results[i].result);
|
|
|
|
}
|
|
|
|
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
2010-06-15 10:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "bsc_data.c"
|
|
|
|
|
2010-07-23 11:08:55 +00:00
|
|
|
static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length)
|
2010-06-15 10:48:36 +00:00
|
|
|
{
|
|
|
|
msgb_reset(msg);
|
|
|
|
msg->l2h = msgb_put(msg, length);
|
|
|
|
memcpy(msg->l2h, data, msgb_l2len(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define VERIFY(con_found, con, msg, ver, str) \
|
2010-06-15 10:48:55 +00:00
|
|
|
if (!con_found || con_found->bsc != con) { \
|
2010-06-15 10:48:44 +00:00
|
|
|
fprintf(stderr, "Failed to find the con: %p\n", con_found); \
|
2010-06-15 10:48:36 +00:00
|
|
|
abort(); \
|
|
|
|
} \
|
|
|
|
if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \
|
|
|
|
fprintf(stderr, "Failed to patch the %s msg.\n", str); \
|
|
|
|
abort(); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test conn tracking once */
|
|
|
|
static void test_contrack()
|
|
|
|
{
|
|
|
|
struct bsc_nat *nat;
|
2010-06-15 10:48:55 +00:00
|
|
|
struct bsc_connection *con;
|
|
|
|
struct sccp_connections *con_found;
|
2010-05-16 08:51:31 +00:00
|
|
|
struct sccp_connections *rc_con;
|
2010-06-15 10:48:36 +00:00
|
|
|
struct bsc_nat_parsed *parsed;
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
fprintf(stderr, "Testing connection tracking.\n");
|
|
|
|
nat = bsc_nat_alloc();
|
|
|
|
con = bsc_connection_alloc(nat);
|
2010-04-17 18:30:57 +00:00
|
|
|
con->cfg = bsc_config_alloc(nat, "foo", 23);
|
2010-06-15 10:48:36 +00:00
|
|
|
msg = msgb_alloc(4096, "test");
|
|
|
|
|
|
|
|
/* 1.) create a connection */
|
|
|
|
copy_to_msg(msg, bsc_cr, sizeof(bsc_cr));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-04-21 10:56:12 +00:00
|
|
|
con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
|
2010-06-15 10:48:36 +00:00
|
|
|
if (con_found != NULL) {
|
2010-06-15 10:48:44 +00:00
|
|
|
fprintf(stderr, "Con should not exist %p\n", con_found);
|
2010-06-15 10:48:36 +00:00
|
|
|
abort();
|
|
|
|
}
|
2010-05-16 08:51:31 +00:00
|
|
|
rc_con = create_sccp_src_ref(con, parsed);
|
|
|
|
if (!rc_con) {
|
2010-06-15 10:48:36 +00:00
|
|
|
fprintf(stderr, "Failed to create a ref\n");
|
|
|
|
abort();
|
|
|
|
}
|
2010-04-21 10:56:12 +00:00
|
|
|
con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
|
2010-06-15 10:48:55 +00:00
|
|
|
if (!con_found || con_found->bsc != con) {
|
2010-06-15 10:48:44 +00:00
|
|
|
fprintf(stderr, "Failed to find the con: %p\n", con_found);
|
2010-06-15 10:48:36 +00:00
|
|
|
abort();
|
|
|
|
}
|
2010-05-16 08:51:31 +00:00
|
|
|
if (con_found != rc_con) {
|
|
|
|
fprintf(stderr, "Failed to find the right connection.\n");
|
|
|
|
abort();
|
|
|
|
}
|
2010-06-15 10:48:36 +00:00
|
|
|
if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) {
|
|
|
|
fprintf(stderr, "Failed to patch the BSC CR msg.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
talloc_free(parsed);
|
|
|
|
|
|
|
|
/* 2.) get the cc */
|
|
|
|
copy_to_msg(msg, msc_cc, sizeof(msc_cc));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-06-15 10:48:55 +00:00
|
|
|
con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
|
|
|
|
VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC");
|
|
|
|
if (update_sccp_src_ref(con_found, parsed) != 0) {
|
2010-06-15 10:48:36 +00:00
|
|
|
fprintf(stderr, "Failed to update the SCCP con.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 3.) send some data */
|
|
|
|
copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-04-21 10:56:12 +00:00
|
|
|
con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
|
2010-06-15 10:48:36 +00:00
|
|
|
VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP");
|
|
|
|
|
|
|
|
/* 4.) receive some data */
|
|
|
|
copy_to_msg(msg, msc_dtap, sizeof(msc_dtap));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
|
|
|
|
VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP");
|
2010-06-15 10:45:38 +00:00
|
|
|
|
2010-06-15 10:48:36 +00:00
|
|
|
/* 5.) close the connection */
|
|
|
|
copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
|
|
|
|
VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD");
|
|
|
|
|
|
|
|
/* 6.) confirm the connection close */
|
|
|
|
copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-04-21 10:56:12 +00:00
|
|
|
con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
|
2010-06-15 10:48:55 +00:00
|
|
|
if (!con_found || con_found->bsc != con) {
|
2010-06-15 10:48:44 +00:00
|
|
|
fprintf(stderr, "Failed to find the con: %p\n", con_found);
|
2010-06-15 10:48:36 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) {
|
|
|
|
fprintf(stderr, "Failed to patch the BSC CR msg.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
remove_sccp_src_ref(con, msg, parsed);
|
2010-04-05 19:44:51 +00:00
|
|
|
talloc_free(parsed);
|
|
|
|
|
|
|
|
copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-04-21 10:56:12 +00:00
|
|
|
con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
|
2010-06-15 10:48:36 +00:00
|
|
|
|
|
|
|
/* verify that it is gone */
|
|
|
|
if (con_found != NULL) {
|
2010-06-15 10:48:44 +00:00
|
|
|
fprintf(stderr, "Con should be gone. %p\n", con_found);
|
2010-06-15 10:48:36 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
talloc_free(parsed);
|
|
|
|
|
|
|
|
|
|
|
|
talloc_free(nat);
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
|
2010-03-30 04:51:53 +00:00
|
|
|
static void test_paging(void)
|
|
|
|
{
|
2010-04-17 06:07:19 +00:00
|
|
|
int lac;
|
2010-03-30 04:51:53 +00:00
|
|
|
struct bsc_nat *nat;
|
|
|
|
struct bsc_connection *con;
|
|
|
|
struct bsc_nat_parsed *parsed;
|
2010-04-06 13:11:34 +00:00
|
|
|
struct bsc_config cfg;
|
2010-03-30 04:51:53 +00:00
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
fprintf(stderr, "Testing paging by lac.\n");
|
|
|
|
|
|
|
|
nat = bsc_nat_alloc();
|
|
|
|
con = bsc_connection_alloc(nat);
|
2010-04-06 13:11:34 +00:00
|
|
|
con->cfg = &cfg;
|
|
|
|
cfg.lac = 23;
|
2010-03-30 04:51:53 +00:00
|
|
|
con->authenticated = 1;
|
|
|
|
llist_add(&con->list_entry, &nat->bsc_connections);
|
|
|
|
msg = msgb_alloc(4096, "test");
|
|
|
|
|
|
|
|
/* Test completely bad input */
|
|
|
|
copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd));
|
2010-04-17 06:07:19 +00:00
|
|
|
if (bsc_nat_find_bsc(nat, msg, &lac) != 0) {
|
2010-03-30 04:51:53 +00:00
|
|
|
fprintf(stderr, "Should have not found anything.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test it by not finding it */
|
|
|
|
copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-04-17 06:07:19 +00:00
|
|
|
if (bsc_nat_find_bsc(nat, msg, &lac) != 0) {
|
2010-03-30 04:51:53 +00:00
|
|
|
fprintf(stderr, "Should have not found aynthing.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
talloc_free(parsed);
|
|
|
|
|
|
|
|
/* Test by finding it */
|
2010-04-06 13:11:34 +00:00
|
|
|
cfg.lac = 8213;
|
2010-03-30 04:51:53 +00:00
|
|
|
copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
2010-04-17 06:07:19 +00:00
|
|
|
if (bsc_nat_find_bsc(nat, msg, &lac) != con) {
|
2010-03-30 04:51:53 +00:00
|
|
|
fprintf(stderr, "Should have found it.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
talloc_free(parsed);
|
|
|
|
}
|
|
|
|
|
2010-04-01 01:55:27 +00:00
|
|
|
static void test_mgcp_ass_tracking(void)
|
2010-06-15 10:49:53 +00:00
|
|
|
{
|
2010-04-22 04:08:17 +00:00
|
|
|
struct bsc_connection *bsc;
|
|
|
|
struct bsc_nat *nat;
|
2010-06-15 10:49:53 +00:00
|
|
|
struct sccp_connections con;
|
|
|
|
struct bsc_nat_parsed *parsed;
|
|
|
|
struct msgb *msg;
|
|
|
|
|
|
|
|
fprintf(stderr, "Testing MGCP.\n");
|
|
|
|
memset(&con, 0, sizeof(con));
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
nat = bsc_nat_alloc();
|
|
|
|
nat->bsc_endpoints = talloc_zero_array(nat,
|
|
|
|
struct bsc_endpoint,
|
|
|
|
33);
|
|
|
|
bsc = bsc_connection_alloc(nat);
|
|
|
|
bsc->cfg = bsc_config_alloc(nat, "foo", 2323);
|
|
|
|
con.bsc = bsc;
|
|
|
|
|
2010-06-15 10:49:53 +00:00
|
|
|
msg = msgb_alloc(4096, "foo");
|
|
|
|
copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
|
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
if (bsc_mgcp_assign(&con, msg) != 0) {
|
|
|
|
fprintf(stderr, "Failed to handle assignment.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con.msc_timeslot != 21) {
|
|
|
|
fprintf(stderr, "Timeslot should be 21.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (con.bsc_timeslot != 21) {
|
|
|
|
fprintf(stderr, "Assigned timeslot should have been 21.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
talloc_free(parsed);
|
|
|
|
|
2010-04-22 04:08:17 +00:00
|
|
|
bsc_mgcp_dlcx(&con);
|
2010-06-15 10:49:53 +00:00
|
|
|
if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) {
|
|
|
|
fprintf(stderr, "Clearing should remove the mapping.\n");
|
|
|
|
abort();
|
|
|
|
}
|
2010-04-22 04:08:17 +00:00
|
|
|
|
|
|
|
talloc_free(nat);
|
2010-06-15 10:49:53 +00:00
|
|
|
}
|
|
|
|
|
2010-04-01 01:55:27 +00:00
|
|
|
/* test the code to find a given connection */
|
|
|
|
static void test_mgcp_find(void)
|
|
|
|
{
|
|
|
|
struct bsc_nat *nat;
|
|
|
|
struct bsc_connection *con;
|
|
|
|
struct sccp_connections *sccp_con;
|
|
|
|
|
|
|
|
fprintf(stderr, "Testing finding of a BSC Connection\n");
|
|
|
|
|
|
|
|
nat = bsc_nat_alloc();
|
|
|
|
con = bsc_connection_alloc(nat);
|
|
|
|
llist_add(&con->list_entry, &nat->bsc_connections);
|
|
|
|
|
|
|
|
sccp_con = talloc_zero(con, struct sccp_connections);
|
|
|
|
sccp_con->msc_timeslot = 12;
|
|
|
|
sccp_con->bsc_timeslot = 12;
|
|
|
|
sccp_con->bsc = con;
|
|
|
|
llist_add(&sccp_con->list_entry, &nat->sccp_connections);
|
|
|
|
|
|
|
|
if (bsc_mgcp_find_con(nat, 11) != NULL) {
|
|
|
|
fprintf(stderr, "Found the wrong connection.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-04-17 18:26:16 +00:00
|
|
|
if (bsc_mgcp_find_con(nat, 12) != sccp_con) {
|
2010-04-01 01:55:27 +00:00
|
|
|
fprintf(stderr, "Didn't find the connection\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
sccp_con->msc_timeslot = 0;
|
|
|
|
sccp_con->bsc_timeslot = 0;
|
2010-04-17 18:26:16 +00:00
|
|
|
if (bsc_mgcp_find_con(nat, 1) != sccp_con) {
|
2010-04-01 01:55:27 +00:00
|
|
|
fprintf(stderr, "Didn't find the connection\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* free everything */
|
|
|
|
talloc_free(nat);
|
|
|
|
}
|
|
|
|
|
2010-04-01 04:48:52 +00:00
|
|
|
static void test_mgcp_rewrite(void)
|
|
|
|
{
|
|
|
|
int i;
|
2010-04-04 16:09:10 +00:00
|
|
|
struct msgb *output;
|
2010-04-01 04:48:52 +00:00
|
|
|
fprintf(stderr, "Test rewriting MGCP messages.\n");
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) {
|
|
|
|
const char *orig = mgcp_messages[i].orig;
|
|
|
|
const char *patc = mgcp_messages[i].patch;
|
|
|
|
const char *ip = mgcp_messages[i].ip;
|
|
|
|
const int port = mgcp_messages[i].port;
|
|
|
|
|
2010-04-04 16:09:10 +00:00
|
|
|
char *input = strdup(orig);
|
2010-04-01 04:48:52 +00:00
|
|
|
|
2010-04-04 16:09:10 +00:00
|
|
|
output = bsc_mgcp_rewrite(input, strlen(input), ip, port);
|
2010-04-01 04:48:52 +00:00
|
|
|
if (msgb_l2len(output) != strlen(patc)) {
|
|
|
|
fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
|
|
|
|
fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) {
|
|
|
|
fprintf(stderr, "Broken on %d msg: '%s'\n", i, (const char *) output->l2h);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
msgb_free(output);
|
2010-04-04 16:09:10 +00:00
|
|
|
free(input);
|
2010-04-01 04:48:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-01 08:16:28 +00:00
|
|
|
static void test_mgcp_parse(void)
|
|
|
|
{
|
|
|
|
int code, ci;
|
|
|
|
char transaction[60];
|
|
|
|
|
|
|
|
fprintf(stderr, "Test MGCP response parsing.\n");
|
|
|
|
|
|
|
|
if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) {
|
|
|
|
fprintf(stderr, "Failed to parse CRCX resp.\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (code != 200) {
|
|
|
|
fprintf(stderr, "Failed to parse the CODE properly. Got: %d\n", code);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(transaction, "23265295") != 0) {
|
|
|
|
fprintf(stderr, "Failed to parse transaction id: '%s'\n", transaction);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
ci = bsc_mgcp_extract_ci(crcx_resp);
|
|
|
|
if (ci != 1) {
|
|
|
|
fprintf(stderr, "Failed to parse the CI. Got: %d\n", ci);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 11:49:35 +00:00
|
|
|
struct cr_filter {
|
2010-07-23 11:08:55 +00:00
|
|
|
const uint8_t *data;
|
2010-05-14 11:49:35 +00:00
|
|
|
int length;
|
|
|
|
int result;
|
2010-05-15 18:00:40 +00:00
|
|
|
int contype;
|
2010-05-14 11:49:35 +00:00
|
|
|
|
|
|
|
const char *bsc_imsi_allow;
|
|
|
|
const char *bsc_imsi_deny;
|
|
|
|
const char *nat_imsi_deny;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cr_filter cr_filter[] = {
|
|
|
|
{
|
|
|
|
.data = bssmap_cr,
|
|
|
|
.length = sizeof(bssmap_cr),
|
|
|
|
.result = 0,
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_CM_SERV_REQ,
|
2010-05-14 11:49:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.data = bss_lu,
|
|
|
|
.length = sizeof(bss_lu),
|
|
|
|
.result = 0,
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_LU,
|
2010-05-14 11:49:35 +00:00
|
|
|
},
|
2010-05-15 15:54:04 +00:00
|
|
|
{
|
|
|
|
.data = pag_resp,
|
|
|
|
.length = sizeof(pag_resp),
|
|
|
|
.result = 0,
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_PAG_RESP,
|
2010-05-15 15:54:04 +00:00
|
|
|
},
|
2010-05-14 11:49:35 +00:00
|
|
|
{
|
|
|
|
/* nat deny is before blank/null BSC */
|
|
|
|
.data = bss_lu,
|
|
|
|
.length = sizeof(bss_lu),
|
|
|
|
.result = -3,
|
|
|
|
.nat_imsi_deny = "[0-9]*",
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_LU,
|
2010-05-14 11:49:35 +00:00
|
|
|
},
|
|
|
|
{
|
2010-05-14 15:07:58 +00:00
|
|
|
/* BSC allow is before NAT deny */
|
2010-05-14 11:49:35 +00:00
|
|
|
.data = bss_lu,
|
|
|
|
.length = sizeof(bss_lu),
|
2010-05-14 15:07:58 +00:00
|
|
|
.result = 0,
|
2010-05-14 11:49:35 +00:00
|
|
|
.nat_imsi_deny = "[0-9]*",
|
2010-05-14 15:07:58 +00:00
|
|
|
.bsc_imsi_allow = "2440[0-9]*",
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_LU,
|
2010-05-14 11:49:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
/* BSC allow is before NAT deny */
|
|
|
|
.data = bss_lu,
|
|
|
|
.length = sizeof(bss_lu),
|
|
|
|
.result = 0,
|
|
|
|
.bsc_imsi_allow = "[0-9]*",
|
|
|
|
.nat_imsi_deny = "[0-9]*",
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_LU,
|
2010-05-14 11:49:35 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
/* filter as deny is first */
|
|
|
|
.data = bss_lu,
|
|
|
|
.length = sizeof(bss_lu),
|
|
|
|
.result = -2,
|
|
|
|
.bsc_imsi_deny = "[0-9]*",
|
|
|
|
.bsc_imsi_allow = "[0-9]*",
|
|
|
|
.nat_imsi_deny = "[0-9]*",
|
2010-05-15 18:00:40 +00:00
|
|
|
.contype = NAT_CON_TYPE_LU,
|
2010-05-14 11:49:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2010-05-14 00:12:57 +00:00
|
|
|
static void test_cr_filter()
|
|
|
|
{
|
2010-05-15 18:00:40 +00:00
|
|
|
int i, res, contype;
|
2010-05-14 00:12:57 +00:00
|
|
|
struct msgb *msg = msgb_alloc(4096, "test_cr_filter");
|
|
|
|
struct bsc_nat_parsed *parsed;
|
2010-06-08 02:14:44 +00:00
|
|
|
struct bsc_nat_acc_lst *nat_lst, *bsc_lst;
|
2010-06-08 02:53:39 +00:00
|
|
|
struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry;
|
2010-05-14 00:12:57 +00:00
|
|
|
|
2010-05-14 11:49:35 +00:00
|
|
|
struct bsc_nat *nat = bsc_nat_alloc();
|
|
|
|
struct bsc_connection *bsc = bsc_connection_alloc(nat);
|
|
|
|
bsc->cfg = bsc_config_alloc(nat, "foo", 1234);
|
2010-05-31 17:03:13 +00:00
|
|
|
bsc->cfg->acc_lst_name = "bsc";
|
|
|
|
nat->acc_lst_name = "nat";
|
2010-05-14 11:49:35 +00:00
|
|
|
|
2010-06-08 02:53:39 +00:00
|
|
|
nat_lst = bsc_nat_acc_lst_get(nat, "nat");
|
|
|
|
bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");
|
|
|
|
|
|
|
|
bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst);
|
|
|
|
nat_entry = bsc_nat_acc_lst_entry_create(nat_lst);
|
|
|
|
|
2010-05-14 11:49:35 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) {
|
|
|
|
msgb_reset(msg);
|
|
|
|
copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length);
|
|
|
|
|
2010-06-08 02:14:44 +00:00
|
|
|
nat_lst = bsc_nat_acc_lst_get(nat, "nat");
|
|
|
|
bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");
|
2010-05-31 17:03:13 +00:00
|
|
|
|
2010-06-08 02:53:39 +00:00
|
|
|
bsc_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny,
|
2010-05-14 11:49:35 +00:00
|
|
|
cr_filter[i].nat_imsi_deny ? 1 : 0,
|
|
|
|
&cr_filter[i].nat_imsi_deny);
|
2010-06-08 02:53:39 +00:00
|
|
|
bsc_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow,
|
2010-05-14 11:49:35 +00:00
|
|
|
cr_filter[i].bsc_imsi_allow ? 1 : 0,
|
|
|
|
&cr_filter[i].bsc_imsi_allow);
|
2010-06-08 02:53:39 +00:00
|
|
|
bsc_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny,
|
2010-05-14 11:49:35 +00:00
|
|
|
cr_filter[i].bsc_imsi_deny ? 1 : 0,
|
|
|
|
&cr_filter[i].bsc_imsi_deny);
|
2010-05-14 00:12:57 +00:00
|
|
|
|
2010-05-14 11:49:35 +00:00
|
|
|
parsed = bsc_nat_parse(msg);
|
|
|
|
if (!parsed) {
|
|
|
|
fprintf(stderr, "FAIL: Failed to parse the message\n");
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-05-15 18:00:40 +00:00
|
|
|
res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype);
|
2010-05-14 11:49:35 +00:00
|
|
|
if (res != cr_filter[i].result) {
|
|
|
|
fprintf(stderr, "FAIL: Wrong result %d for test %d.\n", res, i);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-05-15 18:00:40 +00:00
|
|
|
if (contype != cr_filter[i].contype) {
|
|
|
|
fprintf(stderr, "FAIL: Wrong contype %d for test %d.\n", res, contype);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2010-05-14 11:49:35 +00:00
|
|
|
talloc_free(parsed);
|
|
|
|
}
|
2010-05-14 00:12:57 +00:00
|
|
|
|
|
|
|
msgb_free(msg);
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:48:36 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2010-06-15 12:18:40 +00:00
|
|
|
struct log_target *stderr_target;
|
2010-06-15 10:48:36 +00:00
|
|
|
|
2010-07-29 18:53:14 +00:00
|
|
|
sccp_set_log_area(DSCCP);
|
2010-07-05 05:29:42 +00:00
|
|
|
log_init(&log_info);
|
2010-06-15 12:18:40 +00:00
|
|
|
stderr_target = log_target_create_stderr();
|
|
|
|
log_add_target(stderr_target);
|
|
|
|
log_set_all_filter(stderr_target, 1);
|
2010-06-15 10:48:36 +00:00
|
|
|
|
|
|
|
test_filter();
|
2010-03-30 04:51:53 +00:00
|
|
|
test_contrack();
|
|
|
|
test_paging();
|
2010-04-01 01:55:27 +00:00
|
|
|
test_mgcp_ass_tracking();
|
|
|
|
test_mgcp_find();
|
2010-04-01 04:48:52 +00:00
|
|
|
test_mgcp_rewrite();
|
2010-04-01 08:16:28 +00:00
|
|
|
test_mgcp_parse();
|
2010-05-14 00:12:57 +00:00
|
|
|
test_cr_filter();
|
2010-06-15 10:45:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-06-15 10:48:36 +00:00
|
|
|
|
|
|
|
void input_event()
|
|
|
|
{}
|
|
|
|
int nm_state_event()
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|