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