blob: 6a6ff8bac548410eb5b55261faf046f850d1f167 [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>
Jacob Erlbeck946d1412013-09-17 13:59:29 +020032
33#include <osmocom/core/application.h>
34#include <osmocom/core/backtrace.h>
35#include <osmocom/core/talloc.h>
36
37#include <stdio.h>
38#include <search.h>
39
Neels Hofmeyre3416182018-03-05 05:31:14 +010040void *ctx = NULL;
41
Jacob Erlbeck946d1412013-09-17 13:59:29 +020042enum test {
43 TEST_SCAN_TO_BTS,
Jacob Erlbeck946d1412013-09-17 13:59:29 +020044};
45
46/* GSM 04.08 MM INFORMATION test message */
47static uint8_t gsm48_mm_info_nn_tzt[] = {
48 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
49 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
50 0x11, 0x02, 0x73, 0x00,
51};
52
53static uint8_t gsm48_mm_info_nn_tzt_out[] = {
54 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
55 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
56 0x11, 0x02, 0x73, 0x1a,
57};
58
59static uint8_t gsm48_mm_info_nn_tzt_dst[] = {
60 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
61 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
62 0x11, 0x02, 0x73, 0x00, 0x49, 0x01, 0x00,
63};
64
65static uint8_t gsm48_mm_info_nn_tzt_dst_out[] = {
66 0x05, 0x32, 0x45, 0x08, 0x80, 0x4f, 0x77, 0xeb,
67 0x1a, 0xb6, 0x97, 0xe7, 0x47, 0x31, 0x90, 0x61,
68 0x11, 0x02, 0x73, 0x1a, 0x49, 0x01, 0x02,
69};
70
71struct test_definition {
72 const uint8_t *data;
73 const uint16_t length;
74 const int dir;
75 const int result;
76 const uint8_t *out_data;
77 const uint16_t out_length;
78 const char* params;
79 const int n_params;
80};
81
82static int get_int(const char *params, size_t nmemb, const char *key, int def, int *is_set)
83{
84 const char *kv = NULL;
85
86 kv = strstr(params, key);
87 if (kv) {
88 kv += strlen(key) + 1;
89 fprintf(stderr, "get_int(%s) -> %d\n", key, atoi(kv));
90 if (is_set)
91 *is_set = 1;
92 }
93
94 return kv ? atoi(kv) : def;
95}
96
97static const struct test_definition test_scan_defs[] = {
98 {
99 .data = gsm48_mm_info_nn_tzt_dst,
100 .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt),
101 .dir = TEST_SCAN_TO_BTS,
102 .result = 0,
103 .out_data = gsm48_mm_info_nn_tzt_dst_out,
104 .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_out),
105 .params = "tz_hr=-5 tz_mn=15 tz_dst=2",
106 .n_params = 3,
107 },
108 {
109 .data = gsm48_mm_info_nn_tzt_dst,
110 .length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst),
111 .dir = TEST_SCAN_TO_BTS,
112 .result = 0,
113 .out_data = gsm48_mm_info_nn_tzt_dst_out,
114 .out_length = ARRAY_SIZE(gsm48_mm_info_nn_tzt_dst_out),
115 .params = "tz_hr=-5 tz_mn=15 tz_dst=2",
116 .n_params = 3,
117 },
118};
119
120static void test_scan(void)
121{
122 int i;
123
Neels Hofmeyr958f2592018-05-27 01:26:31 +0200124 struct gsm_network *net = gsm_network_init(ctx);
Alexander Chemeris45f73242020-05-17 21:18:40 +0300125 struct gsm_subscriber_connection *conn = talloc_zero(net, struct gsm_subscriber_connection);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200126
Alexander Chemeris45f73242020-05-17 21:18:40 +0300127 conn->network = net;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200128
Neels Hofmeyr378a4922016-05-09 21:07:43 +0200129 /* start testing with proper messages */
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200130 printf("Testing BTS<->MSC message scan.\n");
131 for (i = 0; i < ARRAY_SIZE(test_scan_defs); ++i) {
132 const struct test_definition *test_def = &test_scan_defs[i];
133 int result;
134 struct msgb *msg = msgb_alloc(4096, "test-message");
135 int is_set = 0;
136
Neels Hofmeyr73983952016-05-10 13:29:33 +0200137 net->tz.hr = get_int(test_def->params, test_def->n_params, "tz_hr", 0, &is_set);
138 net->tz.mn = get_int(test_def->params, test_def->n_params, "tz_mn", 0, &is_set);
139 net->tz.dst = get_int(test_def->params, test_def->n_params, "tz_dst", 0, &is_set);
140 net->tz.override = 1;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200141
142 printf("Going to test item: %d\n", i);
143 msg->l3h = msgb_put(msg, test_def->length);
144 memcpy(msg->l3h, test_def->data, test_def->length);
145
146 switch (test_def->dir) {
147 case TEST_SCAN_TO_BTS:
Neels Hofmeyrfea1df82016-02-27 23:38:28 +0100148 /* override timezone of msg coming from the MSC */
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200149 result = bsc_scan_msc_msg(conn, msg);
150 break;
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200151 default:
152 abort();
153 break;
154 }
155
156 if (result != test_def->result) {
157 printf("FAIL: Not the expected result, got: %d wanted: %d\n",
158 result, test_def->result);
159 goto out;
160 }
161
162 if (msgb_l3len(msg) != test_def->out_length) {
163 printf("FAIL: Not the expected message size, got: %d wanted: %d\n",
164 msgb_l3len(msg), test_def->out_length);
165 goto out;
166 }
167
168 if (memcmp(msgb_l3(msg), test_def->out_data, test_def->out_length) != 0) {
169 printf("FAIL: Not the expected message\n");
170 goto out;
171 }
172
173out:
174 msgb_free(msg);
175 }
176
177 talloc_free(net);
178}
179
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100180static const struct log_info_cat log_categories[] = {
181 [DNM] = {
182 .name = "DNM",
183 .description = "A-bis Network Management / O&M (NM/OML)",
184 .color = "\033[1;36m",
185 .enabled = 1, .loglevel = LOGL_INFO,
186 },
Neels Hofmeyr7997bf42018-02-13 17:16:44 +0100187 [DMSC] = {
188 .name = "DMSC",
189 .description = "Mobile Switching Center",
190 .enabled = 1, .loglevel = LOGL_NOTICE,
191 },
192 [DCTRL] = {
193 .name = "DCTRL",
194 .description = "Control interface",
195 .enabled = 1, .loglevel = LOGL_NOTICE,
196 },
197 [DFILTER] = {
198 .name = "DFILTER",
199 .description = "BSC/NAT IMSI based filtering",
200 .enabled = 1, .loglevel = LOGL_DEBUG,
201 },
202};
203
204static const struct log_info log_info = {
205 .cat = log_categories,
206 .num_cat = ARRAY_SIZE(log_categories),
207};
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200208
209int main(int argc, char **argv)
210{
Neels Hofmeyre3416182018-03-05 05:31:14 +0100211 ctx = talloc_named_const(NULL, 0, "bsc-test");
212 msgb_talloc_ctx_init(ctx, 0);
213 osmo_init_logging2(ctx, &log_info);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200214
215 test_scan();
216
217 printf("Testing execution completed.\n");
Neels Hofmeyre3416182018-03-05 05:31:14 +0100218 talloc_free(ctx);
Jacob Erlbeck946d1412013-09-17 13:59:29 +0200219 return 0;
220}
Harald Welte3561bd42018-01-28 03:04:16 +0100221
222struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net) {
223 OSMO_ASSERT(0);
224}
Neels Hofmeyrc19581f2018-05-27 03:05:18 +0200225
Vadim Yanitskiy6a26e2c2020-08-26 18:49:08 +0700226void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, uint8_t dlci, enum gsm0808_cause cause) {}
Neels Hofmeyrc19581f2018-05-27 03:05:18 +0200227void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t chosen_encr) {}
Neels Hofmeyr2001dd62020-09-11 23:35:28 +0000228int bsc_compl_l3(struct gsm_lchan *lchan, struct msgb *msg, uint16_t chosen_channel)
Neels Hofmeyrc19581f2018-05-27 03:05:18 +0200229{ return 0; }
230void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) {}
231void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause) {}
232void bsc_assign_fail(struct gsm_subscriber_connection *conn, uint8_t cause, uint8_t *rr_cause) {}
Neels Hofmeyrc19581f2018-05-27 03:05:18 +0200233void bsc_cm_update(struct gsm_subscriber_connection *conn,
234 const uint8_t *cm2, uint8_t cm2_len,
235 const uint8_t *cm3, uint8_t cm3_len) {}
Neels Hofmeyr149160f2018-06-16 17:20:13 +0200236void gscon_submit_rsl_dtap(struct gsm_subscriber_connection *conn,
237 struct msgb *msg, int link_id, int allow_sacch) {}
Neels Hofmeyr31f525e2018-05-14 18:14:15 +0200238void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {}
239void lchan_activate(struct gsm_lchan *lchan, void *info) {}
Neels Hofmeyr91a202d2019-07-13 01:12:53 +0200240bool neighbor_ident_bts_entry_exists(uint8_t from_bts) { return false; }
241const char *handover_status(struct gsm_subscriber_connection *conn) { return "x"; }
Pau Espin Pedrolf8d03892019-11-12 16:30:30 +0100242int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan) { return 0; }
Pau Espin Pedrol8d4f94a2020-07-16 15:17:20 +0200243void pcu_info_update(struct gsm_bts *bts) {};
244int rsl_sacch_filling(struct gsm_bts_trx *trx, uint8_t type, const uint8_t *data, int len) { return 0; }
245int rsl_bcch_info(const struct gsm_bts_trx *trx, enum osmo_sysinfo_type si_type, const uint8_t *data, int len)
246{ return 0; }
247int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type) { return 0; }