blob: 08d376016dfdd4a4fc88ccdaa3f66a1954faa493 [file] [log] [blame]
Jacob Erlbeck946d1412013-09-17 13:59:29 +02001/*
2 * BSC Message filtering
3 *
4 * (C) 2013 by sysmocom s.f.m.c. GmbH
5 * Written by Jacob Erlbeck <jerlbeck@sysmocom.de>
6 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
7 * (C) 2010-2013 by On-Waves
8 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25
26
Neels Hofmeyrc0164792017-09-04 15:15:32 +020027#include <osmocom/bsc/debug.h>
28#include <osmocom/bsc/gsm_data.h>
Jacob Erlbeck946d1412013-09-17 13:59:29 +020029
Neels Hofmeyrc0164792017-09-04 15:15:32 +020030#include <osmocom/bsc/osmo_bsc.h>
31#include <osmocom/bsc/bsc_msc_data.h>
32#include <osmocom/bsc/gsm_04_80.h>
Harald Welte9f7b1952017-12-18 18:54:20 +010033#include <osmocom/bsc/common_bsc.h>
Jacob Erlbeck946d1412013-09-17 13:59:29 +020034
35#include <osmocom/core/application.h>
36#include <osmocom/core/backtrace.h>
37#include <osmocom/core/talloc.h>
38
39#include <stdio.h>
40#include <search.h>
41
Neels Hofmeyre3416182018-03-05 05:31:14 +010042void *ctx = NULL;
43
Jacob Erlbeck946d1412013-09-17 13:59:29 +020044enum test {
45 TEST_SCAN_TO_BTS,
Jacob Erlbeck946d1412013-09-17 13:59:29 +020046};
47
48/* GSM 04.08 MM INFORMATION test message */
49static uint8_t gsm48_mm_info_nn_tzt[] = {
50 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
51 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
52 0x11, 0x02, 0x73, 0x00,
53};
54
55static uint8_t gsm48_mm_info_nn_tzt_out[] = {
56 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
57 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
58 0x11, 0x02, 0x73, 0x1a,
59};
60
61static uint8_t gsm48_mm_info_nn_tzt_dst[] = {
62 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
63 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
64 0x11, 0x02, 0x73, 0x00, 0x49, 0x01, 0x00,
65};
66
67static uint8_t gsm48_mm_info_nn_tzt_dst_out[] = {
68 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
69 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
70 0x11, 0x02, 0x73, 0x1a, 0x49, 0x01, 0x02,
71};
72
73struct test_definition {
74 const uint8_t *data;
75 const uint16_t length;
76 const int dir;
77 const int result;
78 const uint8_t *out_data;
79 const uint16_t out_length;
80 const char* params;
81 const int n_params;
82};
83
84static int get_int(const char *params, size_t nmemb, const char *key, int def, int *is_set)
85{
86 const char *kv = NULL;
87
88 kv = strstr(params, key);
89 if (kv) {
90 kv += strlen(key) + 1;
91 fprintf(stderr, "get_int(%s) -> %d\n", key, atoi(kv));
92 if (is_set)
93 *is_set = 1;
94 }
95
96 return kv ? atoi(kv) : def;
97}
98
99static const struct test_definition test_scan_defs[] = {
100 {
101 .data = gsm48_mm_info_nn_tzt_dst,
102 .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt),
103 .dir = TEST_SCAN_TO_BTS,
104 .result = 0,
105 .out_data = gsm48_mm_info_nn_tzt_dst_out,
106 .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_out),
107 .params = "tz_hr=-5 tz_mn=15 tz_dst=2",
108 .n_params = 3,
109 },
110 {
111 .data = gsm48_mm_info_nn_tzt_dst,
112 .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst),
113 .dir = TEST_SCAN_TO_BTS,
114 .result = 0,
115 .out_data = gsm48_mm_info_nn_tzt_dst_out,
116 .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst_out),
117 .params = "tz_hr=-5 tz_mn=15 tz_dst=2",
118 .n_params = 3,
119 },
120};
121
122static void test_scan(void)
123{
124 int i;
125
Neels Hofmeyre3416182018-03-05 05:31:14 +0100126 struct gsm_network *net = bsc_network_init(ctx);
Harald Welte9f7b1952017-12-18 18:54:20 +0100127 struct gsm_bts *bts = gsm_bts_alloc(net, 0);
Neels Hofmeyra369e242017-02-23 21:57:23 +0100128 struct bsc_msc_data *msc;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200129 struct gsm_subscriber_connection *conn;
130
Neels Hofmeyra369e242017-02-23 21:57:23 +0100131 msc = talloc_zero(net, struct bsc_msc_data);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200132 conn = talloc_zero(net, struct gsm_subscriber_connection);
133
134 bts->network = net;
Harald Welte519c7e12018-01-28 02:45:46 +0100135 conn->sccp.msc = msc;
Harald Welte9f7b1952017-12-18 18:54:20 +0100136 conn->lchan = &bts->c0->ts[1].lchan[0];
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200137
Neels Hofmeyr378a4922016-05-09 21:07:43 +0200138 /* start testing with proper messages */
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200139 printf("Testing BTS<->MSC message scan.\n");
140 for (i = 0; i < ARRAY_SIZE(test_scan_defs); ++i) {
141 const struct test_definition *test_def = &test_scan_defs[i];
142 int result;
143 struct msgb *msg = msgb_alloc(4096, "test-message");
144 int is_set = 0;
145
Neels Hofmeyr73983952016-05-10 13:29:33 +0200146 net->tz.hr = get_int(test_def->params, test_def->n_params, "tz_hr", 0, &is_set);
147 net->tz.mn = get_int(test_def->params, test_def->n_params, "tz_mn", 0, &is_set);
148 net->tz.dst = get_int(test_def->params, test_def->n_params, "tz_dst", 0, &is_set);
149 net->tz.override = 1;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200150
151 printf("Going to test item: %d\n", i);
152 msg->l3h = msgb_put(msg, test_def->length);
153 memcpy(msg->l3h, test_def->data, test_def->length);
154
155 switch (test_def->dir) {
156 case TEST_SCAN_TO_BTS:
Neels Hofmeyrfea1df82016-02-27 23:38:28 +0100157 /* override timezone of msg coming from the MSC */
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200158 result = bsc_scan_msc_msg(conn, msg);
159 break;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200160 default:
161 abort();
162 break;
163 }
164
165 if (result != test_def->result) {
166 printf("FAIL: Not the expected result, got: %d wanted: %d\n",
167 result, test_def->result);
168 goto out;
169 }
170
171 if (msgb_l3len(msg) != test_def->out_length) {
172 printf("FAIL: Not the expected message size, got: %d wanted: %d\n",
173 msgb_l3len(msg), test_def->out_length);
174 goto out;
175 }
176
177 if (memcmp(msgb_l3(msg), test_def->out_data, test_def->out_length) != 0) {
178 printf("FAIL: Not the expected message\n");
179 goto out;
180 }
181
182out:
183 msgb_free(msg);
184 }
185
186 talloc_free(net);
187}
188
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100189static const struct log_info_cat log_categories[] = {
190 [DNM] = {
191 .name = "DNM",
192 .description = "A-bis Network Management / O&M (NM/OML)",
193 .color = "\033[1;36m",
194 .enabled = 1, .loglevel = LOGL_INFO,
195 },
196 [DNAT] = {
197 .name = "DNAT",
198 .description = "GSM 08.08 NAT/Multiplexer",
199 .enabled = 1, .loglevel = LOGL_NOTICE,
200 },
201 [DMSC] = {
202 .name = "DMSC",
203 .description = "Mobile Switching Center",
204 .enabled = 1, .loglevel = LOGL_NOTICE,
205 },
206 [DCTRL] = {
207 .name = "DCTRL",
208 .description = "Control interface",
209 .enabled = 1, .loglevel = LOGL_NOTICE,
210 },
211 [DFILTER] = {
212 .name = "DFILTER",
213 .description = "BSC/NAT IMSI based filtering",
214 .enabled = 1, .loglevel = LOGL_DEBUG,
215 },
216};
217
218static const struct log_info log_info = {
219 .cat = log_categories,
220 .num_cat = ARRAY_SIZE(log_categories),
221};
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200222
223int main(int argc, char **argv)
224{
Neels Hofmeyre3416182018-03-05 05:31:14 +0100225 ctx = talloc_named_const(NULL, 0, "bsc-test");
226 msgb_talloc_ctx_init(ctx, 0);
227 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200228
229 test_scan();
230
231 printf("Testing execution completed.\n");
Neels Hofmeyre3416182018-03-05 05:31:14 +0100232 talloc_free(ctx);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200233 return 0;
234}
Harald Welte3561bd42018-01-28 03:04:16 +0100235
236struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net) {
237 OSMO_ASSERT(0);
238}