blob: 106b08bc777af45d38281ff7ee37180fe1571491 [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,
46 TEST_SCAN_TO_MSC,
47};
48
49/* GSM 04.08 MM INFORMATION test message */
50static uint8_t gsm48_mm_info_nn_tzt[] = {
51 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
52 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
53 0x11, 0x02, 0x73, 0x00,
54};
55
56static uint8_t gsm48_mm_info_nn_tzt_out[] = {
57 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
58 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
59 0x11, 0x02, 0x73, 0x1a,
60};
61
62static uint8_t gsm48_mm_info_nn_tzt_dst[] = {
63 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
64 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
65 0x11, 0x02, 0x73, 0x00, 0x49, 0x01, 0x00,
66};
67
68static uint8_t gsm48_mm_info_nn_tzt_dst_out[] = {
69 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
70 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
71 0x11, 0x02, 0x73, 0x1a, 0x49, 0x01, 0x02,
72};
73
74struct test_definition {
75 const uint8_t *data;
76 const uint16_t length;
77 const int dir;
78 const int result;
79 const uint8_t *out_data;
80 const uint16_t out_length;
81 const char* params;
82 const int n_params;
83};
84
85static int get_int(const char *params, size_t nmemb, const char *key, int def, int *is_set)
86{
87 const char *kv = NULL;
88
89 kv = strstr(params, key);
90 if (kv) {
91 kv += strlen(key) + 1;
92 fprintf(stderr, "get_int(%s) -> %d\n", key, atoi(kv));
93 if (is_set)
94 *is_set = 1;
95 }
96
97 return kv ? atoi(kv) : def;
98}
99
100static const struct test_definition test_scan_defs[] = {
101 {
102 .data = gsm48_mm_info_nn_tzt_dst,
103 .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt),
104 .dir = TEST_SCAN_TO_BTS,
105 .result = 0,
106 .out_data = gsm48_mm_info_nn_tzt_dst_out,
107 .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_out),
108 .params = "tz_hr=-5 tz_mn=15 tz_dst=2",
109 .n_params = 3,
110 },
111 {
112 .data = gsm48_mm_info_nn_tzt_dst,
113 .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst),
114 .dir = TEST_SCAN_TO_BTS,
115 .result = 0,
116 .out_data = gsm48_mm_info_nn_tzt_dst_out,
117 .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst_out),
118 .params = "tz_hr=-5 tz_mn=15 tz_dst=2",
119 .n_params = 3,
120 },
121};
122
123static void test_scan(void)
124{
125 int i;
126
Neels Hofmeyre3416182018-03-05 05:31:14 +0100127 struct gsm_network *net = bsc_network_init(ctx);
Harald Welte9f7b1952017-12-18 18:54:20 +0100128 struct gsm_bts *bts = gsm_bts_alloc(net, 0);
Neels Hofmeyra369e242017-02-23 21:57:23 +0100129 struct bsc_msc_data *msc;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200130 struct gsm_subscriber_connection *conn;
131
Neels Hofmeyra369e242017-02-23 21:57:23 +0100132 msc = talloc_zero(net, struct bsc_msc_data);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200133 conn = talloc_zero(net, struct gsm_subscriber_connection);
134
135 bts->network = net;
Harald Welte519c7e12018-01-28 02:45:46 +0100136 conn->sccp.msc = msc;
Harald Welte9f7b1952017-12-18 18:54:20 +0100137 conn->lchan = &bts->c0->ts[1].lchan[0];
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200138
Neels Hofmeyr378a4922016-05-09 21:07:43 +0200139 /* start testing with proper messages */
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200140 printf("Testing BTS<->MSC message scan.\n");
141 for (i = 0; i < ARRAY_SIZE(test_scan_defs); ++i) {
142 const struct test_definition *test_def = &test_scan_defs[i];
143 int result;
144 struct msgb *msg = msgb_alloc(4096, "test-message");
145 int is_set = 0;
146
Neels Hofmeyr73983952016-05-10 13:29:33 +0200147 net->tz.hr = get_int(test_def->params, test_def->n_params, "tz_hr", 0, &is_set);
148 net->tz.mn = get_int(test_def->params, test_def->n_params, "tz_mn", 0, &is_set);
149 net->tz.dst = get_int(test_def->params, test_def->n_params, "tz_dst", 0, &is_set);
150 net->tz.override = 1;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200151
152 printf("Going to test item: %d\n", i);
153 msg->l3h = msgb_put(msg, test_def->length);
154 memcpy(msg->l3h, test_def->data, test_def->length);
155
156 switch (test_def->dir) {
157 case TEST_SCAN_TO_BTS:
Neels Hofmeyrfea1df82016-02-27 23:38:28 +0100158 /* override timezone of msg coming from the MSC */
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200159 result = bsc_scan_msc_msg(conn, msg);
160 break;
161 case TEST_SCAN_TO_MSC:
Neels Hofmeyrfea1df82016-02-27 23:38:28 +0100162 /* override timezone of msg coming from the BSC */
163 /* FIXME: no test for this case is defined in
164 * test_scan_defs[], so this is never used. */
165 result = bsc_scan_bts_msg(conn, msg);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200166 break;
167 default:
168 abort();
169 break;
170 }
171
172 if (result != test_def->result) {
173 printf("FAIL: Not the expected result, got: %d wanted: %d\n",
174 result, test_def->result);
175 goto out;
176 }
177
178 if (msgb_l3len(msg) != test_def->out_length) {
179 printf("FAIL: Not the expected message size, got: %d wanted: %d\n",
180 msgb_l3len(msg), test_def->out_length);
181 goto out;
182 }
183
184 if (memcmp(msgb_l3(msg), test_def->out_data, test_def->out_length) != 0) {
185 printf("FAIL: Not the expected message\n");
186 goto out;
187 }
188
189out:
190 msgb_free(msg);
191 }
192
193 talloc_free(net);
194}
195
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100196static const struct log_info_cat log_categories[] = {
197 [DNM] = {
198 .name = "DNM",
199 .description = "A-bis Network Management / O&M (NM/OML)",
200 .color = "\033[1;36m",
201 .enabled = 1, .loglevel = LOGL_INFO,
202 },
203 [DNAT] = {
204 .name = "DNAT",
205 .description = "GSM 08.08 NAT/Multiplexer",
206 .enabled = 1, .loglevel = LOGL_NOTICE,
207 },
208 [DMSC] = {
209 .name = "DMSC",
210 .description = "Mobile Switching Center",
211 .enabled = 1, .loglevel = LOGL_NOTICE,
212 },
213 [DCTRL] = {
214 .name = "DCTRL",
215 .description = "Control interface",
216 .enabled = 1, .loglevel = LOGL_NOTICE,
217 },
218 [DFILTER] = {
219 .name = "DFILTER",
220 .description = "BSC/NAT IMSI based filtering",
221 .enabled = 1, .loglevel = LOGL_DEBUG,
222 },
223};
224
225static const struct log_info log_info = {
226 .cat = log_categories,
227 .num_cat = ARRAY_SIZE(log_categories),
228};
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200229
230int main(int argc, char **argv)
231{
Neels Hofmeyre3416182018-03-05 05:31:14 +0100232 ctx = talloc_named_const(NULL, 0, "bsc-test");
233 msgb_talloc_ctx_init(ctx, 0);
234 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200235
236 test_scan();
237
238 printf("Testing execution completed.\n");
Neels Hofmeyre3416182018-03-05 05:31:14 +0100239 talloc_free(ctx);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200240 return 0;
241}
Harald Welte3561bd42018-01-28 03:04:16 +0100242
243struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net) {
244 OSMO_ASSERT(0);
245}