Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 1 | /* |
| 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 Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 27 | #include <osmocom/bsc/debug.h> |
| 28 | #include <osmocom/bsc/gsm_data.h> |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 29 | |
Neels Hofmeyr | c016479 | 2017-09-04 15:15:32 +0200 | [diff] [blame] | 30 | #include <osmocom/bsc/osmo_bsc.h> |
| 31 | #include <osmocom/bsc/bsc_msc_data.h> |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 32 | |
Neels Hofmeyr | 4ae338d | 2020-09-17 17:54:39 +0200 | [diff] [blame] | 33 | #include <osmocom/gsm/gad.h> |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 34 | #include <osmocom/core/application.h> |
| 35 | #include <osmocom/core/backtrace.h> |
| 36 | #include <osmocom/core/talloc.h> |
| 37 | |
| 38 | #include <stdio.h> |
| 39 | #include <search.h> |
| 40 | |
Neels Hofmeyr | e341618 | 2018-03-05 05:31:14 +0100 | [diff] [blame] | 41 | void *ctx = NULL; |
| 42 | |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 43 | enum test { |
| 44 | TEST_SCAN_TO_BTS, |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /* GSM 04.08 MM INFORMATION test message */ |
| 48 | static 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 | |
| 54 | static 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 | |
| 60 | static 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 | |
| 66 | static 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 | |
| 72 | struct 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 | |
| 83 | static 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 | |
| 98 | static 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 | |
| 121 | static void test_scan(void) |
| 122 | { |
| 123 | int i; |
| 124 | |
Neels Hofmeyr | 958f259 | 2018-05-27 01:26:31 +0200 | [diff] [blame] | 125 | struct gsm_network *net = gsm_network_init(ctx); |
Alexander Chemeris | 45f7324 | 2020-05-17 21:18:40 +0300 | [diff] [blame] | 126 | struct gsm_subscriber_connection *conn = talloc_zero(net, struct gsm_subscriber_connection); |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 127 | |
Alexander Chemeris | 45f7324 | 2020-05-17 21:18:40 +0300 | [diff] [blame] | 128 | conn->network = net; |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 129 | |
Neels Hofmeyr | 378a492 | 2016-05-09 21:07:43 +0200 | [diff] [blame] | 130 | /* start testing with proper messages */ |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 131 | printf("Testing BTS<->MSC message scan.\n"); |
| 132 | for (i = 0; i < ARRAY_SIZE(test_scan_defs); ++i) { |
| 133 | const struct test_definition *test_def = &test_scan_defs[i]; |
| 134 | int result; |
| 135 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 136 | int is_set = 0; |
| 137 | |
Neels Hofmeyr | 7398395 | 2016-05-10 13:29:33 +0200 | [diff] [blame] | 138 | net->tz.hr = get_int(test_def->params, test_def->n_params, "tz_hr", 0, &is_set); |
| 139 | net->tz.mn = get_int(test_def->params, test_def->n_params, "tz_mn", 0, &is_set); |
| 140 | net->tz.dst = get_int(test_def->params, test_def->n_params, "tz_dst", 0, &is_set); |
| 141 | net->tz.override = 1; |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 142 | |
| 143 | printf("Going to test item: %d\n", i); |
| 144 | msg->l3h = msgb_put(msg, test_def->length); |
| 145 | memcpy(msg->l3h, test_def->data, test_def->length); |
| 146 | |
| 147 | switch (test_def->dir) { |
| 148 | case TEST_SCAN_TO_BTS: |
Neels Hofmeyr | fea1df8 | 2016-02-27 23:38:28 +0100 | [diff] [blame] | 149 | /* override timezone of msg coming from the MSC */ |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 150 | result = bsc_scan_msc_msg(conn, msg); |
| 151 | break; |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 152 | default: |
| 153 | abort(); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | if (result != test_def->result) { |
| 158 | printf("FAIL: Not the expected result, got: %d wanted: %d\n", |
| 159 | result, test_def->result); |
| 160 | goto out; |
| 161 | } |
| 162 | |
| 163 | if (msgb_l3len(msg) != test_def->out_length) { |
| 164 | printf("FAIL: Not the expected message size, got: %d wanted: %d\n", |
| 165 | msgb_l3len(msg), test_def->out_length); |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | if (memcmp(msgb_l3(msg), test_def->out_data, test_def->out_length) != 0) { |
| 170 | printf("FAIL: Not the expected message\n"); |
| 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | out: |
| 175 | msgb_free(msg); |
| 176 | } |
| 177 | |
| 178 | talloc_free(net); |
| 179 | } |
| 180 | |
Neels Hofmeyr | 7997bf4 | 2018-02-13 17:16:44 +0100 | [diff] [blame] | 181 | static const struct log_info_cat log_categories[] = { |
| 182 | [DNM] = { |
| 183 | .name = "DNM", |
| 184 | .description = "A-bis Network Management / O&M (NM/OML)", |
| 185 | .color = "\033[1;36m", |
| 186 | .enabled = 1, .loglevel = LOGL_INFO, |
| 187 | }, |
Neels Hofmeyr | 7997bf4 | 2018-02-13 17:16:44 +0100 | [diff] [blame] | 188 | [DMSC] = { |
| 189 | .name = "DMSC", |
| 190 | .description = "Mobile Switching Center", |
| 191 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 192 | }, |
| 193 | [DCTRL] = { |
| 194 | .name = "DCTRL", |
| 195 | .description = "Control interface", |
| 196 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 197 | }, |
| 198 | [DFILTER] = { |
| 199 | .name = "DFILTER", |
| 200 | .description = "BSC/NAT IMSI based filtering", |
| 201 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 202 | }, |
| 203 | }; |
| 204 | |
| 205 | static const struct log_info log_info = { |
| 206 | .cat = log_categories, |
| 207 | .num_cat = ARRAY_SIZE(log_categories), |
| 208 | }; |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 209 | |
| 210 | int main(int argc, char **argv) |
| 211 | { |
Neels Hofmeyr | e341618 | 2018-03-05 05:31:14 +0100 | [diff] [blame] | 212 | ctx = talloc_named_const(NULL, 0, "bsc-test"); |
| 213 | msgb_talloc_ctx_init(ctx, 0); |
| 214 | osmo_init_logging2(ctx, &log_info); |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 215 | |
| 216 | test_scan(); |
| 217 | |
| 218 | printf("Testing execution completed.\n"); |
Neels Hofmeyr | e341618 | 2018-03-05 05:31:14 +0100 | [diff] [blame] | 219 | talloc_free(ctx); |
Jacob Erlbeck | 946d141 | 2013-09-17 13:59:29 +0200 | [diff] [blame] | 220 | return 0; |
| 221 | } |
Harald Welte | 3561bd4 | 2018-01-28 03:04:16 +0100 | [diff] [blame] | 222 | |
| 223 | struct gsm_subscriber_connection *bsc_subscr_con_allocate(struct gsm_network *net) { |
| 224 | OSMO_ASSERT(0); |
| 225 | } |
Neels Hofmeyr | c19581f | 2018-05-27 03:05:18 +0200 | [diff] [blame] | 226 | |
Vadim Yanitskiy | 6a26e2c | 2020-08-26 18:49:08 +0700 | [diff] [blame] | 227 | void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, uint8_t dlci, enum gsm0808_cause cause) {} |
Neels Hofmeyr | c19581f | 2018-05-27 03:05:18 +0200 | [diff] [blame] | 228 | void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn, struct msgb *msg, uint8_t chosen_encr) {} |
Neels Hofmeyr | 2001dd6 | 2020-09-11 23:35:28 +0000 | [diff] [blame] | 229 | int bsc_compl_l3(struct gsm_lchan *lchan, struct msgb *msg, uint16_t chosen_channel) |
Neels Hofmeyr | c19581f | 2018-05-27 03:05:18 +0200 | [diff] [blame] | 230 | { return 0; } |
| 231 | void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) {} |
| 232 | void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause) {} |
| 233 | void bsc_assign_fail(struct gsm_subscriber_connection *conn, uint8_t cause, uint8_t *rr_cause) {} |
Neels Hofmeyr | c19581f | 2018-05-27 03:05:18 +0200 | [diff] [blame] | 234 | void bsc_cm_update(struct gsm_subscriber_connection *conn, |
| 235 | const uint8_t *cm2, uint8_t cm2_len, |
| 236 | const uint8_t *cm3, uint8_t cm3_len) {} |
Neels Hofmeyr | 149160f | 2018-06-16 17:20:13 +0200 | [diff] [blame] | 237 | void gscon_submit_rsl_dtap(struct gsm_subscriber_connection *conn, |
| 238 | struct msgb *msg, int link_id, int allow_sacch) {} |
Neels Hofmeyr | 31f525e | 2018-05-14 18:14:15 +0200 | [diff] [blame] | 239 | void ts_fsm_alloc(struct gsm_bts_trx_ts *ts) {} |
| 240 | void lchan_activate(struct gsm_lchan *lchan, void *info) {} |
Neels Hofmeyr | 91a202d | 2019-07-13 01:12:53 +0200 | [diff] [blame] | 241 | bool neighbor_ident_bts_entry_exists(uint8_t from_bts) { return false; } |
| 242 | const char *handover_status(struct gsm_subscriber_connection *conn) { return "x"; } |
Pau Espin Pedrol | f8d0389 | 2019-11-12 16:30:30 +0100 | [diff] [blame] | 243 | int rsl_chan_ms_power_ctrl(struct gsm_lchan *lchan) { return 0; } |
Pau Espin Pedrol | 8d4f94a | 2020-07-16 15:17:20 +0200 | [diff] [blame] | 244 | void pcu_info_update(struct gsm_bts *bts) {}; |
| 245 | int rsl_sacch_filling(struct gsm_bts_trx *trx, uint8_t type, const uint8_t *data, int len) { return 0; } |
| 246 | int rsl_bcch_info(const struct gsm_bts_trx *trx, enum osmo_sysinfo_type si_type, const uint8_t *data, int len) |
| 247 | { return 0; } |
| 248 | int gsm_generate_si(struct gsm_bts *bts, enum osmo_sysinfo_type si_type) { return 0; } |