Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1 | /* Osmocom MSC+VLR end-to-end tests */ |
| 2 | |
| 3 | /* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <getopt.h> |
| 25 | #include <stdlib.h> |
| 26 | |
| 27 | #include <osmocom/core/logging.h> |
| 28 | #include <osmocom/core/utils.h> |
| 29 | #include <osmocom/core/msgb.h> |
| 30 | #include <osmocom/core/application.h> |
| 31 | #include <osmocom/gsm/protocol/gsm_04_11.h> |
| 32 | #include <osmocom/gsm/gsup.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 33 | #include <osmocom/msc/gsup_client_mux.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 34 | #include <osmocom/msc/gsm_04_11.h> |
| 35 | #include <osmocom/msc/debug.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 36 | #include <osmocom/msc/gsm_04_08.h> |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 37 | #include <osmocom/msc/transaction.h> |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 38 | #include <osmocom/msc/ran_msg.h> |
| 39 | #include <osmocom/msc/msc_a.h> |
| 40 | #include <osmocom/msc/msc_i.h> |
| 41 | #include <osmocom/msc/msc_t.h> |
| 42 | #include <osmocom/msc/call_leg.h> |
| 43 | #include <osmocom/msc/rtp_stream.h> |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 44 | |
| 45 | #include "msc_vlr_tests.h" |
| 46 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 47 | void *msc_vlr_tests_ctx = NULL; |
Neels Hofmeyr | dbb3c72 | 2021-02-07 23:53:07 +0100 | [diff] [blame] | 48 | void *msgb_ctx = NULL; |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 49 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 50 | bool _log_lines = false; |
| 51 | |
| 52 | struct gsm_network *net = NULL; |
| 53 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 54 | const char *gsup_tx_expected = NULL; |
| 55 | bool gsup_tx_confirmed; |
| 56 | |
| 57 | struct msgb *dtap_tx_expected = NULL; |
| 58 | bool dtap_tx_confirmed; |
| 59 | |
| 60 | enum result_sent lu_result_sent; |
| 61 | enum result_sent cm_service_result_sent; |
| 62 | bool auth_request_sent; |
| 63 | const char *auth_request_expect_rand; |
| 64 | const char *auth_request_expect_autn; |
| 65 | bool cipher_mode_cmd_sent; |
| 66 | bool cipher_mode_cmd_sent_with_imeisv; |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 67 | const char *cipher_mode_expect_kc; |
| 68 | bool security_mode_ctrl_sent; |
| 69 | const char *security_mode_expect_ck; |
| 70 | const char *security_mode_expect_ik; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 71 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 72 | bool iu_release_expected = false; |
| 73 | bool iu_release_sent = false; |
| 74 | bool bssap_clear_expected = false; |
| 75 | bool bssap_clear_sent = false; |
| 76 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 77 | bool bssap_assignment_expected = false; |
| 78 | bool bssap_assignment_sent = false; |
| 79 | bool iu_rab_assignment_expected = false; |
| 80 | bool iu_rab_assignment_sent = false; |
| 81 | |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 82 | uint32_t cc_to_mncc_tx_expected_msg_type = 0; |
| 83 | const char *cc_to_mncc_tx_expected_imsi = NULL; |
| 84 | bool cc_to_mncc_tx_confirmed = false; |
| 85 | uint32_t cc_to_mncc_tx_got_callref = 0; |
| 86 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 87 | enum rtp_direction expecting_crcx = -1; |
| 88 | bool got_crcx = false; |
| 89 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 90 | extern int ran_dec_dtap_undup_pdisc_ctr_bin(uint8_t pdisc); |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 91 | |
| 92 | /* static state variables for the L3 send sequence numbers */ |
| 93 | static uint8_t n_sd[4]; |
| 94 | |
| 95 | /* patch a correct send sequence number into the given message */ |
| 96 | static void patch_l3_seq_nr(struct msgb *msg) |
| 97 | { |
| 98 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 99 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
| 100 | uint8_t *msg_type_oct = &msg->l3h[1]; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 101 | int bin = ran_dec_dtap_undup_pdisc_ctr_bin(pdisc); |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 102 | |
| 103 | if (bin >= 0 && bin < ARRAY_SIZE(n_sd)) { |
| 104 | /* patch in n_sd into the msg_type octet */ |
| 105 | *msg_type_oct = (*msg_type_oct & 0x3f) | ((n_sd[bin] & 0x3) << 6); |
| 106 | //fprintf(stderr, "pdisc=0x%02x bin=%d, patched n_sd=%u\n\n", pdisc, bin, n_sd[bin] & 3); |
| 107 | /* increment N(SD) */ |
| 108 | n_sd[bin] = (n_sd[bin] + 1) % 4; |
| 109 | } else { |
| 110 | //fprintf(stderr, "pdisc=0x%02x NO SEQ\n\n", pdisc); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* reset L3 sequence numbers (e.g. new RR connection) */ |
| 115 | static void reset_l3_seq_nr() |
| 116 | { |
| 117 | memset(n_sd, 0, sizeof(n_sd)); |
| 118 | } |
| 119 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 120 | struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex) |
| 121 | { |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 122 | struct msgb *msg = msgb_alloc_headroom(size, 4, label); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 123 | unsigned char *rc; |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 124 | msg->l2h = msg->data; |
| 125 | rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg))); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 126 | OSMO_ASSERT(rc == msg->l2h); |
| 127 | return msg; |
| 128 | } |
| 129 | |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 130 | static const char *gh_type_name(struct gsm48_hdr *gh) |
Neels Hofmeyr | 78ada64 | 2017-03-10 02:15:20 +0100 | [diff] [blame] | 131 | { |
| 132 | return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh), |
| 133 | gsm48_hdr_msg_type(gh)); |
| 134 | } |
| 135 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 136 | void gsup_rx(const char *rx_hex, const char *expect_tx_hex) |
| 137 | { |
| 138 | int rc; |
| 139 | struct msgb *msg; |
| 140 | const char *label; |
| 141 | |
| 142 | gsup_expect_tx(expect_tx_hex); |
| 143 | |
| 144 | msg = msgb_from_hex("gsup", 1024, rx_hex); |
| 145 | label = osmo_gsup_message_type_name(msg->l2h[0]); |
| 146 | fprintf(stderr, "<-- GSUP rx %s: %s\n", label, |
| 147 | osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg))); |
Neels Hofmeyr | 834f94a | 2017-09-28 03:07:16 +0200 | [diff] [blame] | 148 | /* GSUP read cb takes ownership of msgb */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 149 | rc = gsup_client_mux_rx(net->gcm->gsup_client, msg); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 150 | fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n", |
| 151 | label, rc); |
| 152 | if (expect_tx_hex) |
| 153 | OSMO_ASSERT(gsup_tx_confirmed); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 156 | bool conn_exists(const struct msub *msub) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 157 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 158 | struct msub *i; |
Max | ba8a007 | 2018-10-25 17:58:43 +0200 | [diff] [blame] | 159 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 160 | if (!msub) |
Max | ba8a007 | 2018-10-25 17:58:43 +0200 | [diff] [blame] | 161 | return false; |
| 162 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 163 | llist_for_each_entry(i, &msub_list, entry) { |
| 164 | if (i == msub) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 165 | return true; |
| 166 | } |
Max | ba8a007 | 2018-10-25 17:58:43 +0200 | [diff] [blame] | 167 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 168 | btw("msub gone"); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 172 | /* Simplified version of the cm_service_request_concludes() */ |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 173 | void conn_conclude_cm_service_req(struct msub *msub, const char *cm_service_use) |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 174 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 175 | int32_t count; |
| 176 | struct msc_a *msc_a = msub_msc_a(msub); |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 177 | btw("Concluding CM Service Request"); |
| 178 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 179 | OSMO_ASSERT(conn_exists(msub)); |
| 180 | count = osmo_use_count_by(&msc_a->use_count, cm_service_use); |
| 181 | OSMO_ASSERT(count > 0); |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 182 | |
Vadim Yanitskiy | d14422a | 2019-07-09 00:37:49 +0700 | [diff] [blame] | 183 | OSMO_ASSERT(osmo_use_count_get_put(&msc_a->use_count, cm_service_use, -count) == 0); |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 184 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 185 | ASSERT_RELEASE_CLEAR(msc_a->c.ran->type); |
| 186 | } |
| 187 | |
| 188 | void dummy_msc_i_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 189 | { |
| 190 | } |
| 191 | |
| 192 | static const struct osmo_fsm_state dummy_msc_i_states[] = { |
| 193 | { |
| 194 | .name = "0", |
| 195 | .in_event_mask = 0xffffffff, |
| 196 | .action = dummy_msc_i_action, |
| 197 | }, |
| 198 | }; |
| 199 | |
| 200 | struct osmo_fsm dummy_msc_i_fsm = { |
| 201 | .name = "dummy_msc_i", |
| 202 | .states = dummy_msc_i_states, |
| 203 | .num_states = ARRAY_SIZE(dummy_msc_i_states), |
| 204 | .log_subsys = DMSC, |
| 205 | .event_names = msc_i_fsm_event_names, |
| 206 | }; |
| 207 | |
| 208 | struct msc_i *dummy_msc_i_alloc(struct msub *msub, struct ran_infra *ran) |
| 209 | { |
| 210 | return msub_role_alloc(g_msub, MSC_ROLE_I, &dummy_msc_i_fsm, struct msc_i, ran); |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 211 | } |
| 212 | |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 213 | enum osmo_rat_type rx_from_ran = OSMO_RAT_GERAN_A; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 214 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 215 | struct msub *g_msub = NULL; |
Harald Welte | 977b548 | 2019-02-18 12:23:43 +0100 | [diff] [blame] | 216 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 217 | void dtap_expect_tx(const char *hex) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 218 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 219 | /* Has the previously expected dtap been received? */ |
| 220 | OSMO_ASSERT(!dtap_tx_expected); |
| 221 | if (!hex) |
| 222 | return; |
| 223 | dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex); |
| 224 | /* Mask the sequence number out */ |
| 225 | if (msgb_length(dtap_tx_expected) >= 2) |
| 226 | dtap_tx_expected->data[1] &= 0x3f; |
| 227 | dtap_tx_confirmed = false; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 230 | static int _validate_dtap(struct msgb *msg, enum osmo_rat_type to_ran) |
| 231 | { |
| 232 | struct gsm48_hdr *gh = (void*)msg->data; |
| 233 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
| 234 | uint8_t msgt = gsm48_hdr_msg_type(gh); |
| 235 | |
| 236 | btw("DTAP --%s--> MS: %s: %s", |
| 237 | osmo_rat_type_name(to_ran), gh_type_name((void*)msg->data), |
| 238 | osmo_hexdump_nospc(msg->data, msg->len)); |
| 239 | |
| 240 | if (pdisc == GSM48_PDISC_MM |
| 241 | && msgt == GSM48_MT_MM_CM_SERV_ACC) { |
| 242 | cm_service_result_sent |= RES_ACCEPT; |
| 243 | talloc_free(msg); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | if (pdisc == GSM48_PDISC_MM |
| 248 | && msgt == GSM48_MT_MM_CM_SERV_REJ) { |
| 249 | cm_service_result_sent |= RES_REJECT; |
| 250 | talloc_free(msg); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | OSMO_ASSERT(dtap_tx_expected); |
| 255 | |
| 256 | /* Mask the sequence number out before comparing */ |
| 257 | msg->data[1] &= 0x3f; |
Neels Hofmeyr | e0f906c | 2019-10-21 06:00:35 +0200 | [diff] [blame] | 258 | if (!msgb_eq_data_print(msg, dtap_tx_expected->data, dtap_tx_expected->len)) { |
| 259 | btw("Expected %s", osmo_hexdump(dtap_tx_expected->data, dtap_tx_expected->len)); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 260 | abort(); |
Neels Hofmeyr | e0f906c | 2019-10-21 06:00:35 +0200 | [diff] [blame] | 261 | } |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 262 | |
| 263 | btw("DTAP matches expected message"); |
| 264 | |
| 265 | talloc_free(msg); |
| 266 | dtap_tx_confirmed = true; |
| 267 | talloc_free(dtap_tx_expected); |
| 268 | dtap_tx_expected = NULL; |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static void bssap_validate_clear_cmd() |
| 274 | { |
| 275 | OSMO_ASSERT(bssap_clear_expected); |
| 276 | bssap_clear_expected = false; |
| 277 | bssap_clear_sent = true; |
| 278 | } |
| 279 | |
| 280 | static void iucs_validate_clear_cmd() |
| 281 | { |
| 282 | OSMO_ASSERT(iu_release_expected); |
| 283 | iu_release_expected = false; |
| 284 | iu_release_sent = true; |
| 285 | } |
| 286 | |
| 287 | static int bssap_validate_cipher_mode_cmd(const struct ran_cipher_mode_command *cmd) |
| 288 | { |
| 289 | int i; |
| 290 | const char *got_key; |
| 291 | cipher_mode_cmd_sent = true; |
| 292 | cipher_mode_cmd_sent_with_imeisv = cmd->geran.retrieve_imeisv; |
| 293 | btw("sending Ciphering Mode Command: retrieve_imeisv=%d", cipher_mode_cmd_sent_with_imeisv); |
| 294 | for (i = 0; i < 7; i++) { |
| 295 | if (!(cmd->geran.a5_encryption_mask & (1 << i))) |
| 296 | continue; |
| 297 | btw("...perm algo: A5/%d", i); |
| 298 | } |
| 299 | got_key = osmo_hexdump_nospc(cmd->vec->kc, sizeof(cmd->vec->kc)); |
| 300 | btw("...key: %s", got_key); |
| 301 | |
| 302 | if (!cipher_mode_expect_kc |
| 303 | || strcmp(cipher_mode_expect_kc, got_key)) { |
| 304 | log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL"); |
| 305 | OSMO_ASSERT(false); |
| 306 | } |
| 307 | return 0; |
| 308 | } |
| 309 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 310 | static void bssap_validate_assignment_cmd() |
| 311 | { |
| 312 | OSMO_ASSERT(bssap_assignment_expected); |
| 313 | bssap_assignment_expected = false; |
| 314 | bssap_assignment_sent = true; |
| 315 | } |
| 316 | |
| 317 | static void iucs_validate_assignment_cmd() |
| 318 | { |
| 319 | OSMO_ASSERT(iu_rab_assignment_expected); |
| 320 | iu_rab_assignment_expected = false; |
| 321 | iu_rab_assignment_sent = true; |
| 322 | } |
| 323 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 324 | static int iucs_validate_security_mode_ctrl(const struct ran_cipher_mode_command *cmd) |
| 325 | { |
| 326 | const char *got_ik; |
| 327 | got_ik = osmo_hexdump_nospc(cmd->vec->ik, sizeof(cmd->vec->ik)); |
| 328 | btw("sending SecurityModeControl: ik=%s", got_ik); |
| 329 | security_mode_ctrl_sent = true; |
| 330 | if (!security_mode_expect_ik |
| 331 | || strcmp(security_mode_expect_ik, got_ik)) { |
| 332 | log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL"); |
| 333 | OSMO_ASSERT(false); |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | struct msgb *dont_ran_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg) |
| 339 | { |
| 340 | struct msc_role_common *c = caller_fi->priv; |
| 341 | enum osmo_rat_type ran_type = c->ran->type; |
| 342 | const char *ran_name = osmo_rat_type_name(ran_type); |
| 343 | LOG_RAN_ENC(caller_fi, DMSC, LOGL_INFO, "%s on %s\n", ran_msg_type_name(ran_enc_msg->msg_type), |
| 344 | ran_name); |
| 345 | |
| 346 | switch (ran_enc_msg->msg_type) { |
| 347 | case RAN_MSG_DTAP: |
| 348 | _validate_dtap(ran_enc_msg->dtap, ran_type); |
| 349 | break; |
| 350 | case RAN_MSG_CLEAR_COMMAND: |
| 351 | switch (ran_type) { |
| 352 | case OSMO_RAT_GERAN_A: |
| 353 | bssap_validate_clear_cmd(); |
| 354 | break; |
| 355 | case OSMO_RAT_UTRAN_IU: |
| 356 | iucs_validate_clear_cmd(); |
| 357 | break; |
| 358 | default: |
| 359 | OSMO_ASSERT(false); |
| 360 | } |
| 361 | break; |
| 362 | case RAN_MSG_CIPHER_MODE_COMMAND: |
| 363 | switch (ran_type) { |
| 364 | case OSMO_RAT_GERAN_A: |
| 365 | bssap_validate_cipher_mode_cmd(&ran_enc_msg->cipher_mode_command); |
| 366 | break; |
| 367 | case OSMO_RAT_UTRAN_IU: |
| 368 | iucs_validate_security_mode_ctrl(&ran_enc_msg->cipher_mode_command); |
| 369 | break; |
| 370 | default: |
| 371 | OSMO_ASSERT(false); |
| 372 | } |
| 373 | break; |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 374 | case RAN_MSG_ASSIGNMENT_COMMAND: |
| 375 | switch (ran_type) { |
| 376 | case OSMO_RAT_GERAN_A: |
| 377 | bssap_validate_assignment_cmd(); |
| 378 | break; |
| 379 | case OSMO_RAT_UTRAN_IU: |
| 380 | iucs_validate_assignment_cmd(); |
| 381 | break; |
| 382 | default: |
| 383 | OSMO_ASSERT(false); |
| 384 | } |
| 385 | break; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 386 | default: |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | /* We're testing MSC and VLR interaction, not message encoding. |
| 391 | * Return whatever. The test msc_i instance is a dummy and drops these. |
| 392 | * But it must be msg_free()-able. |
| 393 | */ |
| 394 | return msgb_alloc(1, "unused dummy msg"); |
| 395 | } |
| 396 | |
| 397 | struct ran_infra test_ran_infra[] = { |
| 398 | [OSMO_RAT_GERAN_A] = { |
| 399 | .type = OSMO_RAT_GERAN_A, |
| 400 | .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_48006, |
| 401 | .log_subsys = DBSSAP, |
| 402 | .tdefs = msc_tdefs_geran, |
| 403 | .ran_encode = dont_ran_encode, |
| 404 | }, |
| 405 | [OSMO_RAT_UTRAN_IU] = { |
| 406 | .type = OSMO_RAT_UTRAN_IU, |
| 407 | .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_25413, |
| 408 | .log_subsys = DIUCS, |
| 409 | .tdefs = msc_tdefs_utran, |
| 410 | .ran_encode = dont_ran_encode, |
| 411 | }, |
| 412 | }; |
| 413 | |
| 414 | static int fake_msc_a_ran_dec(const struct ran_msg *ran_dec_msg) |
| 415 | { |
| 416 | struct msc_a_ran_dec_data d = { |
| 417 | .from_role = MSC_ROLE_I, |
| 418 | }; |
| 419 | return msc_a_ran_decode_cb(g_msub->role[MSC_ROLE_A], &d, ran_dec_msg); |
| 420 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 421 | |
| 422 | void rx_from_ms(struct msgb *msg) |
| 423 | { |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 424 | struct gsm48_hdr *gh = msgb_l3(msg); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 425 | struct ran_msg ran_dec_msg; |
| 426 | struct gsm0808_cell_id cell_id = { |
| 427 | .id_discr = CELL_IDENT_LAI_AND_LAC, |
| 428 | .id.lai_and_lac = { |
| 429 | .plmn = { |
| 430 | .mcc = 1, |
| 431 | .mnc = 2, |
| 432 | }, |
| 433 | .lac = 23, |
| 434 | }, |
| 435 | }; |
| 436 | struct msc_a *msc_a; |
Neels Hofmeyr | 78ada64 | 2017-03-10 02:15:20 +0100 | [diff] [blame] | 437 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 438 | log("MSC <--%s-- MS: %s", osmo_rat_type_name(rx_from_ran), gh_type_name(gh)); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 439 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 440 | if (!conn_exists(g_msub)) |
| 441 | g_msub = NULL; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 442 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 443 | if (!g_msub) { |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 444 | log("new conn"); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 445 | g_msub = msub_alloc(net); |
| 446 | msc_a_alloc(g_msub, &test_ran_infra[rx_from_ran]); |
| 447 | dummy_msc_i_alloc(g_msub, &test_ran_infra[rx_from_ran]); |
| 448 | |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 449 | reset_l3_seq_nr(); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 450 | ran_dec_msg = (struct ran_msg){ |
| 451 | .msg_type = RAN_MSG_COMPL_L3, |
| 452 | .compl_l3 = { |
| 453 | .cell_id = &cell_id, |
| 454 | .msg = msg, |
| 455 | }, |
| 456 | }; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 457 | } else { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 458 | ran_dec_msg = (struct ran_msg){ |
| 459 | .msg_type = RAN_MSG_DTAP, |
| 460 | .dtap = msg, |
| 461 | }; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 464 | msc_a = msub_msc_a(g_msub); |
| 465 | msc_a_get(msc_a, __func__); |
| 466 | |
| 467 | patch_l3_seq_nr(msg); |
| 468 | fake_msc_a_ran_dec(&ran_dec_msg); |
| 469 | |
| 470 | msc_a_put(msc_a, __func__); |
| 471 | |
| 472 | if (!conn_exists(g_msub)) |
| 473 | g_msub = NULL; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | void ms_sends_msg(const char *hex) |
| 477 | { |
| 478 | struct msgb *msg; |
| 479 | |
| 480 | msg = msgb_from_hex("ms_sends_msg", 1024, hex); |
| 481 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 482 | rx_from_ms(msg); |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 483 | msgb_free(msg); |
| 484 | } |
| 485 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 486 | void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark) |
Neels Hofmeyr | 3117b70 | 2018-09-13 03:23:07 +0200 | [diff] [blame] | 487 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 488 | struct ran_msg ran_dec = { |
| 489 | .msg_type = RAN_MSG_CLASSMARK_UPDATE, |
| 490 | .classmark_update = { |
| 491 | .classmark = classmark, |
| 492 | }, |
| 493 | }; |
| 494 | fake_msc_a_ran_dec(&ran_dec); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 497 | static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 498 | { |
| 499 | int rc; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 500 | struct ran_msg ran_dec; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 501 | struct msgb *msg; |
| 502 | struct gsm48_hdr *gh; |
| 503 | |
| 504 | msg = msgb_alloc(1024, "ms_sends_msg_fake"); |
| 505 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 506 | |
| 507 | gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh)); |
| 508 | gh->proto_discr = pdisc; |
| 509 | gh->msg_type = msg_type; |
| 510 | /* some amount of data, whatever */ |
| 511 | msgb_put(msg, 123); |
| 512 | |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 513 | patch_l3_seq_nr(msg); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 514 | |
| 515 | ran_dec = (struct ran_msg){ |
| 516 | .msg_type = RAN_MSG_DTAP, |
| 517 | .dtap = msg, |
| 518 | }; |
| 519 | rc = fake_msc_a_ran_dec(&ran_dec); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 520 | |
| 521 | talloc_free(msg); |
| 522 | return rc; |
| 523 | } |
| 524 | |
Max | 6817190 | 2018-02-06 17:55:19 +0100 | [diff] [blame] | 525 | static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype) |
| 526 | { |
| 527 | int rc = ms_sends_msg_fake(val, msgtype); |
| 528 | if (rc != -EACCES) |
| 529 | log("Unexpected return value %u != %u for %s/%s", |
| 530 | -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype)); |
| 531 | } |
| 532 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 533 | void thwart_rx_non_initial_requests() |
| 534 | { |
| 535 | log("requests shall be thwarted"); |
Max | 6817190 | 2018-02-06 17:55:19 +0100 | [diff] [blame] | 536 | |
| 537 | ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP); |
| 538 | ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */ |
| 539 | ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1); |
| 540 | ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | void send_sms(struct vlr_subscr *receiver, |
| 544 | struct vlr_subscr *sender, |
| 545 | char *str) |
| 546 | { |
Harald Welte | 39b5548 | 2018-04-09 19:19:33 +0200 | [diff] [blame] | 547 | struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str); |
Vadim Yanitskiy | 24e025e | 2018-11-22 15:42:39 +0700 | [diff] [blame] | 548 | gsm411_send_sms(net, receiver, sms); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | unsigned char next_rand_byte = 0; |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 552 | /* override, requires '-Wl,--wrap=osmo_get_rand_id' */ |
| 553 | int __real_osmo_get_rand_id(uint8_t *buf, size_t num); |
| 554 | int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 555 | { |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 556 | size_t i; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 557 | for (i = 0; i < num; i++) |
| 558 | buf[i] = next_rand_byte++; |
| 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | /* override, requires '-Wl,--wrap=gsm340_gen_scts' */ |
| 563 | void __real_gsm340_gen_scts(uint8_t *scts, time_t time); |
| 564 | void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time) |
| 565 | { |
Neels Hofmeyr | 05230ea | 2017-08-22 18:06:42 +0200 | [diff] [blame] | 566 | /* Write fixed time bytes for deterministic test results */ |
| 567 | osmo_hexparse("07101000000000", scts, 7); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | const char *paging_expecting_imsi = NULL; |
| 571 | uint32_t paging_expecting_tmsi; |
| 572 | bool paging_sent; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 573 | |
| 574 | void paging_expect_imsi(const char *imsi) |
| 575 | { |
| 576 | paging_expecting_imsi = imsi; |
| 577 | paging_expecting_tmsi = GSM_RESERVED_TMSI; |
| 578 | } |
| 579 | |
| 580 | void paging_expect_tmsi(uint32_t tmsi) |
| 581 | { |
| 582 | paging_expecting_tmsi = tmsi; |
| 583 | paging_expecting_imsi = NULL; |
| 584 | } |
| 585 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 586 | /* override, requires '-Wl,--wrap=ran_peers_down_paging' */ |
| 587 | int __real_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub, |
| 588 | enum paging_cause cause); |
| 589 | int __wrap_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub, |
| 590 | enum paging_cause cause) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 591 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 592 | log("paging request (%s) to %s on %s", paging_cause_name(cause), vlr_subscr_name(vsub), |
| 593 | osmo_rat_type_name(sri->ran->type)); |
| 594 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 595 | OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI)); |
| 596 | if (paging_expecting_imsi) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 597 | VERBOSE_ASSERT(strcmp(paging_expecting_imsi, vsub->imsi), == 0, "%d"); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 598 | if (paging_expecting_tmsi != GSM_RESERVED_TMSI) { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 599 | VERBOSE_ASSERT(paging_expecting_tmsi, == vsub->tmsi, "0x%08x"); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 600 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 601 | paging_sent = true; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 602 | return 1; |
| 603 | } |
| 604 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 605 | void clear_vlr() |
| 606 | { |
| 607 | struct vlr_subscr *vsub, *n; |
| 608 | llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) { |
| 609 | vlr_subscr_free(vsub); |
| 610 | } |
| 611 | |
| 612 | net->authentication_required = false; |
Harald Welte | 7b222aa | 2017-12-23 19:30:32 +0100 | [diff] [blame] | 613 | net->a5_encryption_mask = (1 << 0); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 614 | net->vlr->cfg.check_imei_rqd = false; |
| 615 | net->vlr->cfg.assign_tmsi = false; |
Neels Hofmeyr | 54a706c | 2017-07-18 15:39:27 +0200 | [diff] [blame] | 616 | net->vlr->cfg.retrieve_imeisv_early = false; |
| 617 | net->vlr->cfg.retrieve_imeisv_ciphered = false; |
Neels Hofmeyr | 7b1418e | 2017-10-29 02:12:16 +0100 | [diff] [blame] | 618 | net->vlr->cfg.auth_tuple_max_reuse_count = 0; |
| 619 | net->vlr->cfg.auth_reuse_old_sets_on_error = false; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 620 | |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 621 | rx_from_ran = OSMO_RAT_GERAN_A; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 622 | auth_request_sent = false; |
| 623 | auth_request_expect_rand = NULL; |
| 624 | auth_request_expect_autn = NULL; |
| 625 | |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 626 | cipher_mode_cmd_sent = false; |
| 627 | cipher_mode_cmd_sent_with_imeisv = false; |
| 628 | cipher_mode_expect_kc = NULL; |
| 629 | |
| 630 | security_mode_ctrl_sent = false; |
| 631 | security_mode_expect_ck = NULL; |
| 632 | security_mode_expect_ik = NULL; |
| 633 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 634 | next_rand_byte = 0; |
| 635 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 636 | iu_release_expected = false; |
| 637 | iu_release_sent = false; |
| 638 | bssap_clear_expected = false; |
| 639 | bssap_clear_sent = false; |
| 640 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 641 | osmo_gettimeofday_override = false; |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 642 | |
| 643 | expecting_crcx = -1; |
| 644 | got_crcx = false; |
| 645 | |
| 646 | bssap_assignment_expected = false; |
| 647 | bssap_assignment_sent = false; |
| 648 | iu_rab_assignment_expected = false; |
| 649 | iu_rab_assignment_sent = false; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static struct log_info_cat test_categories[] = { |
| 653 | [DMSC] = { |
| 654 | .name = "DMSC", |
| 655 | .description = "Mobile Switching Center", |
| 656 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 657 | }, |
| 658 | [DRLL] = { |
| 659 | .name = "DRLL", |
| 660 | .description = "A-bis Radio Link Layer (RLL)", |
| 661 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 662 | }, |
| 663 | [DMM] = { |
| 664 | .name = "DMM", |
| 665 | .description = "Layer3 Mobility Management (MM)", |
| 666 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 667 | }, |
| 668 | [DRR] = { |
| 669 | .name = "DRR", |
| 670 | .description = "Layer3 Radio Resource (RR)", |
| 671 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 672 | }, |
| 673 | [DCC] = { |
| 674 | .name = "DCC", |
| 675 | .description = "Layer3 Call Control (CC)", |
Neels Hofmeyr | 12e17be | 2018-03-12 23:59:37 +0100 | [diff] [blame] | 676 | .enabled = 1, .loglevel = LOGL_DEBUG, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 677 | }, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 678 | [DVLR] = { |
| 679 | .name = "DVLR", |
| 680 | .description = "Visitor Location Register", |
| 681 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 682 | }, |
| 683 | [DREF] = { |
| 684 | .name = "DREF", |
| 685 | .description = "Reference Counting", |
| 686 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 687 | }, |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 688 | [DPAG] = { |
| 689 | .name = "DPAG", |
| 690 | .description = "Paging Subsystem", |
| 691 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 692 | }, |
| 693 | [DIUCS] = { |
| 694 | .name = "DIUCS", |
| 695 | .description = "Iu-CS Protocol", |
| 696 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 697 | }, |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 698 | [DMNCC] = { |
| 699 | .name = "DMNCC", |
| 700 | .description = "MNCC API for Call Control application", |
| 701 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 702 | }, |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 703 | [DBSSAP] = { |
| 704 | .name = "DBSSAP", |
| 705 | .description = "BSSAP Protocol (A Interface)", |
| 706 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 707 | }, |
Neels Hofmeyr | 979b057 | 2019-05-09 15:24:49 +0200 | [diff] [blame] | 708 | [DSS] = { |
| 709 | .name = "DSS", |
| 710 | .description = "Supplementary Services", |
| 711 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 712 | }, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 713 | }; |
| 714 | |
| 715 | static struct log_info info = { |
| 716 | .cat = test_categories, |
| 717 | .num_cat = ARRAY_SIZE(test_categories), |
| 718 | }; |
| 719 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 720 | struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL; |
| 721 | |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 722 | int mncc_recv(struct gsm_network *net, struct msgb *msg) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 723 | { |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 724 | struct gsm_mncc *mncc = (void*)msg->data; |
| 725 | log("MSC --> MNCC: callref 0x%x: %s", mncc->callref, |
| 726 | get_mncc_name(mncc->msg_type)); |
| 727 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 728 | if (mncc->msg_type == MNCC_REL_IND && on_call_release_mncc_sends_to_cc_data) { |
| 729 | |
| 730 | log("MNCC: callref 0x%x: Call Release triggering %s", mncc->callref, |
| 731 | get_mncc_name(on_call_release_mncc_sends_to_cc_data->msg_type)); |
| 732 | |
Neels Hofmeyr | 5255874 | 2019-05-09 01:23:09 +0200 | [diff] [blame] | 733 | mncc_tx_to_cc(net, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 734 | on_call_release_mncc_sends_to_cc_data); |
| 735 | |
| 736 | on_call_release_mncc_sends_to_cc_data = NULL; |
| 737 | return 0; |
| 738 | } |
| 739 | |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 740 | OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type); |
| 741 | if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) { |
| 742 | log("Mismatch! Expected MNCC msg type: %s", |
| 743 | get_mncc_name(cc_to_mncc_tx_expected_msg_type)); |
| 744 | abort(); |
| 745 | } |
| 746 | |
| 747 | if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) { |
| 748 | log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'", |
| 749 | cc_to_mncc_tx_expected_imsi, |
| 750 | mncc->imsi); |
| 751 | abort(); |
| 752 | } |
| 753 | |
| 754 | cc_to_mncc_tx_confirmed = true; |
| 755 | cc_to_mncc_tx_got_callref = mncc->callref; |
| 756 | cc_to_mncc_tx_expected_imsi = NULL; |
| 757 | cc_to_mncc_tx_expected_msg_type = 0; |
| 758 | talloc_free(msg); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 762 | struct osmo_gsup_client * |
Stefan Sperling | afa030d | 2018-12-06 12:06:59 +0100 | [diff] [blame] | 763 | __real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr, |
| 764 | unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb, |
| 765 | struct osmo_oap_client_config *oap_config); |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 766 | struct osmo_gsup_client * |
Stefan Sperling | afa030d | 2018-12-06 12:06:59 +0100 | [diff] [blame] | 767 | __wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr, |
| 768 | unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb, |
| 769 | struct osmo_oap_client_config *oap_config) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 770 | { |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 771 | struct osmo_gsup_client *gsupc; |
| 772 | gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 773 | OSMO_ASSERT(gsupc); |
| 774 | return gsupc; |
| 775 | } |
| 776 | |
| 777 | /* override, requires '-Wl,--wrap=gsup_client_send' */ |
Harald Welte | 1ea6baf | 2018-07-31 19:40:52 +0200 | [diff] [blame] | 778 | int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg); |
| 779 | int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 780 | { |
Max | 34d306b | 2019-01-15 18:37:31 +0100 | [diff] [blame] | 781 | uint8_t buf[512]; |
| 782 | int len; |
| 783 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 784 | fprintf(stderr, "GSUP --> HLR: %s: %s\n", |
Max | 34d306b | 2019-01-15 18:37:31 +0100 | [diff] [blame] | 785 | osmo_gsup_message_type_name(msg->data[0]), osmo_hexdump_nospc(msg->data, msg->len)); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 786 | |
| 787 | OSMO_ASSERT(gsup_tx_expected); |
Max | 34d306b | 2019-01-15 18:37:31 +0100 | [diff] [blame] | 788 | OSMO_ASSERT(strlen(gsup_tx_expected) <= (sizeof(buf) * 2)); |
| 789 | |
| 790 | len = osmo_hexparse(gsup_tx_expected, buf, sizeof(buf)); |
| 791 | if (len < 1) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 792 | abort(); |
Max | 34d306b | 2019-01-15 18:37:31 +0100 | [diff] [blame] | 793 | |
Neels Hofmeyr | be5f033 | 2018-12-26 01:11:57 +0100 | [diff] [blame] | 794 | /* Compare only the length expected. Extra data is fine, to not care about new GSUP IEs invented later. */ |
| 795 | if (msg->len < len) { |
| 796 | fprintf(stderr, "ERROR: GSUP message too short, expected '%s'\n", gsup_tx_expected); |
Max | 34d306b | 2019-01-15 18:37:31 +0100 | [diff] [blame] | 797 | abort(); |
Neels Hofmeyr | be5f033 | 2018-12-26 01:11:57 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | if (memcmp(msg->data, buf, len)) { |
| 801 | fprintf(stderr, "ERROR: GSUP message mismatch, expected it to start with '%s'\n", gsup_tx_expected); |
| 802 | abort(); |
| 803 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 804 | |
| 805 | talloc_free(msg); |
| 806 | gsup_tx_confirmed = true; |
| 807 | gsup_tx_expected = NULL; |
| 808 | return 0; |
| 809 | } |
| 810 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 811 | struct rtp_stream fake_rtp[2] = { |
| 812 | { |
| 813 | .dir = RTP_TO_RAN, |
| 814 | .local = { |
| 815 | .ip = "10.23.42.1", |
| 816 | .port = 99, |
| 817 | }, |
| 818 | .remote = { |
| 819 | .ip = "10.23.42.2", |
| 820 | .port = 100, |
| 821 | }, |
| 822 | }, |
| 823 | { |
| 824 | .dir = RTP_TO_CN, |
| 825 | .local = { |
| 826 | .ip = "10.23.42.1", |
| 827 | .port = 23, |
| 828 | }, |
| 829 | .remote = { |
| 830 | .ip = "10.23.42.2", |
| 831 | .port = 42, |
| 832 | }, |
| 833 | }, |
| 834 | }; |
| 835 | |
| 836 | void expect_crcx(enum rtp_direction towards) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 837 | { |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 838 | OSMO_ASSERT(expecting_crcx == -1); |
| 839 | expecting_crcx = towards; |
| 840 | got_crcx = false; |
| 841 | } |
| 842 | |
| 843 | /* override, requires '-Wl,--wrap=call_leg_ensure_ci' */ |
| 844 | int __real_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans, |
| 845 | const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known); |
| 846 | int __wrap_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans, |
| 847 | const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known) |
| 848 | { |
| 849 | if (!cl->rtp[dir]) { |
| 850 | log("MGW <--CRCX to %s-- MSC: callref=0x%x", rtp_direction_name(dir), call_id); |
| 851 | |
| 852 | OSMO_ASSERT(expecting_crcx == dir); |
| 853 | expecting_crcx = -1; |
| 854 | got_crcx = true; |
| 855 | |
| 856 | call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans); |
| 857 | if (codec_if_known) |
| 858 | rtp_stream_set_codec(cl->rtp[dir], *codec_if_known); |
| 859 | if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known)) |
| 860 | rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known); |
| 861 | } |
| 862 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 866 | void crcx_ok(enum rtp_direction dir) |
| 867 | { |
| 868 | struct msc_a *msc_a = msub_msc_a(g_msub); |
| 869 | struct call_leg *cl = msc_a->cc.call_leg; |
| 870 | OSMO_ASSERT(cl); |
| 871 | OSMO_ASSERT(cl->rtp[dir]); |
| 872 | osmo_sockaddr_str_from_str(&cl->rtp[dir]->local, "10.23.23.1", 23); |
| 873 | //osmo_sockaddr_str_from_str(&cl->rtp[dir].remote, "10.42.42.1", 42); |
| 874 | log("MGW --CRCX OK to %s--> MSC", rtp_direction_name(dir)); |
| 875 | osmo_fsm_inst_dispatch(cl->fi, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, cl->rtp[dir]); |
| 876 | } |
| 877 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 878 | static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi) |
| 879 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 880 | struct msc_a *msc_a = msc_conn_ref; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 881 | if (send_tmsi == GSM_RESERVED_TMSI) |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 882 | btw("sending LU Accept for %s", msc_a->c.fi->id); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 883 | else |
| 884 | btw("sending LU Accept for %s, with TMSI 0x%08x", |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 885 | msc_a->c.fi->id, send_tmsi); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 886 | lu_result_sent |= RES_ACCEPT; |
| 887 | return 0; |
| 888 | } |
| 889 | |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 890 | static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 891 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 892 | struct msc_a *msc_a = msc_conn_ref; |
| 893 | btw("sending LU Reject for %s, cause %u", msc_a->c.fi->id, cause); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 894 | lu_result_sent |= RES_REJECT; |
| 895 | return 0; |
| 896 | } |
| 897 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 898 | static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type, |
| 899 | enum gsm48_reject_value cause) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 900 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 901 | struct msc_a *msc_a = msc_conn_ref; |
| 902 | btw("sending CM Service Reject (%s) for %s, cause: %s", |
| 903 | osmo_cm_service_type_name(cm_service_type), msc_a->c.fi->id, gsm48_reject_value_name(cause)); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 904 | cm_service_result_sent |= RES_REJECT; |
| 905 | return 0; |
| 906 | } |
| 907 | |
Neels Hofmeyr | 8b6e536 | 2018-11-30 02:57:33 +0100 | [diff] [blame] | 908 | static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 909 | bool send_autn) |
| 910 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 911 | struct msc_a *msc_a = msc_conn_ref; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 912 | char *hex; |
| 913 | bool ok = true; |
| 914 | btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...", |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 915 | send_autn? "UMTS" : "GSM", msc_a->c.fi->id, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 916 | at->use_count, at->key_seq, at->vec.auth_types); |
| 917 | |
| 918 | hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand)); |
| 919 | btw("...rand=%s", hex); |
| 920 | if (!auth_request_expect_rand |
| 921 | || strcmp(hex, auth_request_expect_rand) != 0) { |
| 922 | ok = false; |
| 923 | log("FAILURE: expected rand=%s", |
| 924 | auth_request_expect_rand ? auth_request_expect_rand : "-"); |
| 925 | } |
| 926 | |
| 927 | if (send_autn) { |
| 928 | hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn)); |
| 929 | btw("...autn=%s", hex); |
| 930 | if (!auth_request_expect_autn |
| 931 | || strcmp(hex, auth_request_expect_autn) != 0) { |
| 932 | ok = false; |
| 933 | log("FAILURE: expected autn=%s", |
| 934 | auth_request_expect_autn ? auth_request_expect_autn : "-"); |
| 935 | } |
| 936 | } else if (auth_request_expect_autn) { |
| 937 | ok = false; |
| 938 | log("FAILURE: no AUTN sent, expected AUTN = %s", |
| 939 | auth_request_expect_autn); |
| 940 | } |
| 941 | |
| 942 | if (send_autn) |
| 943 | btw("...expecting res=%s", |
| 944 | osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len)); |
| 945 | else |
| 946 | btw("...expecting sres=%s", |
| 947 | osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres))); |
| 948 | |
| 949 | auth_request_sent = ok; |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static int fake_vlr_tx_auth_rej(void *msc_conn_ref) |
| 954 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 955 | struct msc_a *msc_a = msc_conn_ref; |
| 956 | btw("sending Auth Reject for %s", msc_a->c.fi->id); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 957 | return 0; |
| 958 | } |
| 959 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 960 | void ms_sends_ciphering_mode_complete(const char *inner_ran_msg) |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 961 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 962 | struct ran_msg ran_dec; |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 963 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 964 | msc_a_get(msub_msc_a(g_msub), __func__); |
| 965 | |
| 966 | ran_dec = (struct ran_msg){ |
| 967 | .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE, |
| 968 | }; |
| 969 | fake_msc_a_ran_dec(&ran_dec); |
| 970 | |
| 971 | if (inner_ran_msg) { |
| 972 | struct msgb *msg = msgb_from_hex("cipher_mode_complete_ran", 1024, inner_ran_msg); |
| 973 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 974 | ran_dec = (struct ran_msg){ |
| 975 | .msg_type = RAN_MSG_DTAP, |
| 976 | .dtap = msg, |
| 977 | }; |
| 978 | patch_l3_seq_nr(msg); |
| 979 | fake_msc_a_ran_dec(&ran_dec); |
| 980 | msgb_free(msg); |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 981 | } |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 982 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 983 | msc_a_put(msub_msc_a(g_msub), __func__); |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 984 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 985 | if (!conn_exists(g_msub)) |
| 986 | g_msub = NULL; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 989 | void ms_sends_security_mode_complete() |
| 990 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 991 | struct ran_msg ran_dec; |
| 992 | |
| 993 | ran_dec = (struct ran_msg){ |
| 994 | .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE, |
| 995 | }; |
| 996 | fake_msc_a_ran_dec(&ran_dec); |
| 997 | |
| 998 | if (!conn_exists(g_msub)) |
| 999 | g_msub = NULL; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Neels Hofmeyr | 3f391dd | 2019-08-29 03:53:11 +0200 | [diff] [blame] | 1002 | void ms_sends_assignment_complete(enum mgcp_codecs assigned_codec) |
| 1003 | { |
| 1004 | struct ran_msg ran_dec; |
| 1005 | |
| 1006 | ran_dec = (struct ran_msg){ |
| 1007 | .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE, |
| 1008 | .assignment_complete = { |
| 1009 | .codec = assigned_codec, |
| 1010 | }, |
| 1011 | }; |
| 1012 | fake_msc_a_ran_dec(&ran_dec); |
| 1013 | |
| 1014 | if (!conn_exists(g_msub)) |
| 1015 | g_msub = NULL; |
| 1016 | } |
| 1017 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1018 | void ran_sends_clear_complete() |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 1019 | { |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1020 | struct ran_msg ran_dec; |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 1021 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1022 | ran_dec = (struct ran_msg){ |
| 1023 | .msg_type = RAN_MSG_CLEAR_COMPLETE, |
| 1024 | }; |
| 1025 | fake_msc_a_ran_dec(&ran_dec); |
| 1026 | |
| 1027 | if (!conn_exists(g_msub)) |
| 1028 | g_msub = NULL; |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1031 | const struct timeval fake_time_start_time = { 123, 456 }; |
| 1032 | |
| 1033 | void fake_time_start() |
| 1034 | { |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1035 | struct timespec *clock_override; |
| 1036 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1037 | osmo_gettimeofday_override_time = fake_time_start_time; |
| 1038 | osmo_gettimeofday_override = true; |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 1039 | clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC); |
| 1040 | OSMO_ASSERT(clock_override); |
| 1041 | clock_override->tv_sec = fake_time_start_time.tv_sec; |
| 1042 | clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000; |
| 1043 | osmo_clock_override_enable(CLOCK_MONOTONIC, true); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1044 | fake_time_passes(0, 0); |
| 1045 | } |
| 1046 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1047 | static struct { |
| 1048 | bool verbose; |
| 1049 | int run_test_nr; |
| 1050 | } cmdline_opts = { |
| 1051 | .verbose = false, |
| 1052 | .run_test_nr = -1, |
| 1053 | }; |
| 1054 | |
| 1055 | static void print_help(const char *program) |
| 1056 | { |
| 1057 | printf("Usage:\n" |
| 1058 | " %s [-v] [N [N...]]\n" |
| 1059 | "Options:\n" |
| 1060 | " -h --help show this text.\n" |
| 1061 | " -v --verbose print source file and line numbers\n" |
| 1062 | " N run only the Nth test (first test is N=1)\n", |
| 1063 | program |
| 1064 | ); |
| 1065 | } |
| 1066 | |
| 1067 | static void handle_options(int argc, char **argv) |
| 1068 | { |
| 1069 | while (1) { |
| 1070 | int option_index = 0, c; |
| 1071 | static struct option long_options[] = { |
| 1072 | {"help", 0, 0, 'h'}, |
| 1073 | {"verbose", 1, 0, 'v'}, |
| 1074 | {0, 0, 0, 0} |
| 1075 | }; |
| 1076 | |
| 1077 | c = getopt_long(argc, argv, "hv", |
| 1078 | long_options, &option_index); |
| 1079 | if (c == -1) |
| 1080 | break; |
| 1081 | |
| 1082 | switch (c) { |
| 1083 | case 'h': |
| 1084 | print_help(argv[0]); |
| 1085 | exit(0); |
| 1086 | case 'v': |
| 1087 | cmdline_opts.verbose = true; |
| 1088 | break; |
| 1089 | default: |
| 1090 | /* catch unknown options *as well as* missing arguments. */ |
| 1091 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 1092 | exit(-1); |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1098 | static void run_tests(int nr) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1099 | { |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1100 | int test_nr; |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 1101 | |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 1102 | nr--; /* arg's first test is 1, in here it's 0 */ |
| 1103 | for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) { |
Neels Hofmeyr | dbb3c72 | 2021-02-07 23:53:07 +0100 | [diff] [blame] | 1104 | size_t talloc_blocks_before_test; |
| 1105 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1106 | if (nr >= 0 && test_nr != nr) |
| 1107 | continue; |
| 1108 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1109 | if (cmdline_opts.verbose) |
| 1110 | fprintf(stderr, "(test nr %d)\n", test_nr + 1); |
| 1111 | |
Neels Hofmeyr | dbb3c72 | 2021-02-07 23:53:07 +0100 | [diff] [blame] | 1112 | talloc_blocks_before_test = talloc_total_blocks(msc_vlr_tests_ctx); |
| 1113 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1114 | msc_vlr_tests[test_nr](); |
| 1115 | |
Neels Hofmeyr | dbb3c72 | 2021-02-07 23:53:07 +0100 | [diff] [blame] | 1116 | if (talloc_total_blocks(msc_vlr_tests_ctx) != talloc_blocks_before_test) { |
| 1117 | fprintf(stderr, "ERROR: talloc leak: %zu blocks\n", |
| 1118 | talloc_total_blocks(msc_vlr_tests_ctx) - talloc_blocks_before_test); |
| 1119 | talloc_report_full(msc_vlr_tests_ctx, stderr); |
| 1120 | fprintf(stderr, "\n"); |
| 1121 | } |
| 1122 | |
| 1123 | if (talloc_total_blocks(msgb_ctx) > 1) { |
| 1124 | fprintf(stderr, "ERROR: msgb leak:\n"); |
| 1125 | talloc_report_full(msgb_ctx, stderr); |
| 1126 | fprintf(stderr, "\n"); |
| 1127 | } |
| 1128 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1129 | if (cmdline_opts.verbose) |
| 1130 | fprintf(stderr, "(test nr %d)\n", test_nr + 1); |
Max | 29ce08a | 2018-02-06 18:46:57 +0100 | [diff] [blame] | 1131 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 1134 | struct gsm_network *test_net(void *ctx) |
| 1135 | { |
Neels Hofmeyr | 7f48420 | 2018-02-27 12:59:45 +0100 | [diff] [blame] | 1136 | struct gsm_network *net = gsm_network_init(ctx, mncc_recv); |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 1137 | |
| 1138 | net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server"); |
| 1139 | net->gsup_server_port = 0; |
| 1140 | |
| 1141 | OSMO_ASSERT(msc_vlr_alloc(net) == 0); |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 1142 | OSMO_ASSERT(net->vlr); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1143 | OSMO_ASSERT(msc_gsup_client_start(net) == 0); |
| 1144 | OSMO_ASSERT(net->gcm); |
| 1145 | OSMO_ASSERT(msc_vlr_start(net) == 0); |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 1146 | |
| 1147 | net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc; |
| 1148 | net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1149 | net->vlr->ops.tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc; |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 1150 | net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej; |
| 1151 | net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req; |
| 1152 | net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1153 | net->vlr->ops.set_ciph_mode = msc_a_vlr_set_cipher_mode; |
| 1154 | |
| 1155 | /* Allocate fake SCCP Ran Instances */ |
| 1156 | net->a.sri = talloc_zero(net, struct sccp_ran_inst); |
| 1157 | *net->a.sri = (struct sccp_ran_inst){ |
| 1158 | .ran = &test_ran_infra[OSMO_RAT_GERAN_A], |
| 1159 | }; |
| 1160 | INIT_LLIST_HEAD(&net->a.sri->ran_peers); |
| 1161 | INIT_LLIST_HEAD(&net->a.sri->ran_conns); |
| 1162 | |
| 1163 | net->iu.sri = talloc_zero(net, struct sccp_ran_inst); |
| 1164 | *net->iu.sri = (struct sccp_ran_inst){ |
| 1165 | .ran = &test_ran_infra[OSMO_RAT_UTRAN_IU], |
| 1166 | }; |
| 1167 | INIT_LLIST_HEAD(&net->iu.sri->ran_peers); |
| 1168 | INIT_LLIST_HEAD(&net->iu.sri->ran_conns); |
| 1169 | |
| 1170 | net->mgw.tdefs = g_mgw_tdefs; |
| 1171 | mgcp_client_conf_init(&net->mgw.conf); |
| 1172 | net->mgw.tdefs = g_mgw_tdefs; |
| 1173 | net->mgw.client = mgcp_client_init(net, &net->mgw.conf); |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 1174 | |
| 1175 | return net; |
| 1176 | } |
| 1177 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1178 | int main(int argc, char **argv) |
| 1179 | { |
| 1180 | handle_options(argc, argv); |
| 1181 | |
Neels Hofmeyr | e4f7e71 | 2019-03-24 21:07:33 +0100 | [diff] [blame] | 1182 | osmo_fsm_term_safely(true); |
| 1183 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 1184 | msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx"); |
| 1185 | msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 1186 | osmo_init_logging2(msc_vlr_tests_ctx, &info); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1187 | |
| 1188 | _log_lines = cmdline_opts.verbose; |
| 1189 | |
| 1190 | OSMO_ASSERT(osmo_stderr_target); |
| 1191 | log_set_use_color(osmo_stderr_target, 0); |
| 1192 | log_set_print_timestamp(osmo_stderr_target, 0); |
Pau Espin Pedrol | cad22fd | 2021-02-19 13:37:00 +0100 | [diff] [blame] | 1193 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1194 | log_set_print_category(osmo_stderr_target, 1); |
Pau Espin Pedrol | 8d02d99 | 2021-02-19 13:36:47 +0100 | [diff] [blame] | 1195 | log_set_print_category_hex(osmo_stderr_target, 0); |
Max | 4813152 | 2019-01-16 12:47:39 +0100 | [diff] [blame] | 1196 | log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1197 | |
Neels Hofmeyr | 39cb0dd | 2018-12-18 02:30:18 +0100 | [diff] [blame] | 1198 | if (cmdline_opts.verbose) { |
Neels Hofmeyr | 39cb0dd | 2018-12-18 02:30:18 +0100 | [diff] [blame] | 1199 | log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME); |
| 1200 | log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END); |
| 1201 | log_set_use_color(osmo_stderr_target, 1); |
| 1202 | log_set_print_level(osmo_stderr_target, 1); |
| 1203 | } |
Neels Hofmeyr | 3f5b780 | 2017-12-15 03:49:55 +0100 | [diff] [blame] | 1204 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 1205 | net = test_net(msc_vlr_tests_ctx); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1206 | |
| 1207 | osmo_fsm_log_addr(false); |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1208 | osmo_fsm_log_timeouts(cmdline_opts.verbose); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1209 | |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1210 | call_leg_init(net); |
| 1211 | |
| 1212 | OSMO_ASSERT(osmo_fsm_register(&dummy_msc_i_fsm) == 0); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1213 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 1214 | clear_vlr(); |
| 1215 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1216 | if (optind >= argc) |
| 1217 | run_tests(-1); |
| 1218 | else { |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1219 | int arg; |
| 1220 | long int nr; |
| 1221 | for (arg = optind; arg < argc; arg++) { |
Neels Hofmeyr | 9c848b5 | 2017-11-22 01:59:36 +0100 | [diff] [blame] | 1222 | errno = 0; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1223 | nr = strtol(argv[arg], NULL, 10); |
| 1224 | if (errno) { |
| 1225 | fprintf(stderr, "Invalid argument: %s\n", |
| 1226 | argv[arg]); |
| 1227 | exit(1); |
| 1228 | } |
| 1229 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 1230 | run_tests(nr); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | printf("Done\n"); |
| 1235 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1236 | return 0; |
| 1237 | } |