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 | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 33 | #include <osmocom/msc/gsup_client.h> |
| 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 | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 38 | |
| 39 | #if BUILD_IU |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 40 | #include <osmocom/msc/iucs_ranap.h> |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 41 | #include <osmocom/ranap/iu_client.h> |
| 42 | #else |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 43 | #include <osmocom/msc/iu_dummy.h> |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 44 | #endif |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 45 | |
| 46 | #include "msc_vlr_tests.h" |
| 47 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 48 | void *msc_vlr_tests_ctx = NULL; |
| 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 | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 77 | uint32_t cc_to_mncc_tx_expected_msg_type = 0; |
| 78 | const char *cc_to_mncc_tx_expected_imsi = NULL; |
| 79 | bool cc_to_mncc_tx_confirmed = false; |
| 80 | uint32_t cc_to_mncc_tx_got_callref = 0; |
| 81 | |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 82 | extern int gsm0407_pdisc_ctr_bin(uint8_t pdisc); |
| 83 | |
| 84 | /* static state variables for the L3 send sequence numbers */ |
| 85 | static uint8_t n_sd[4]; |
| 86 | |
| 87 | /* patch a correct send sequence number into the given message */ |
| 88 | static void patch_l3_seq_nr(struct msgb *msg) |
| 89 | { |
| 90 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 91 | uint8_t pdisc = gsm48_hdr_pdisc(gh); |
| 92 | uint8_t *msg_type_oct = &msg->l3h[1]; |
| 93 | int bin = gsm0407_pdisc_ctr_bin(pdisc); |
| 94 | |
| 95 | if (bin >= 0 && bin < ARRAY_SIZE(n_sd)) { |
| 96 | /* patch in n_sd into the msg_type octet */ |
| 97 | *msg_type_oct = (*msg_type_oct & 0x3f) | ((n_sd[bin] & 0x3) << 6); |
| 98 | //fprintf(stderr, "pdisc=0x%02x bin=%d, patched n_sd=%u\n\n", pdisc, bin, n_sd[bin] & 3); |
| 99 | /* increment N(SD) */ |
| 100 | n_sd[bin] = (n_sd[bin] + 1) % 4; |
| 101 | } else { |
| 102 | //fprintf(stderr, "pdisc=0x%02x NO SEQ\n\n", pdisc); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* reset L3 sequence numbers (e.g. new RR connection) */ |
| 107 | static void reset_l3_seq_nr() |
| 108 | { |
| 109 | memset(n_sd, 0, sizeof(n_sd)); |
| 110 | } |
| 111 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 112 | struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex) |
| 113 | { |
| 114 | struct msgb *msg = msgb_alloc(size, label); |
| 115 | unsigned char *rc; |
| 116 | msg->l2h = msg->head; |
| 117 | rc = msgb_put(msg, osmo_hexparse(hex, msg->head, msgb_tailroom(msg))); |
| 118 | OSMO_ASSERT(rc == msg->l2h); |
| 119 | return msg; |
| 120 | } |
| 121 | |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 122 | static const char *gh_type_name(struct gsm48_hdr *gh) |
Neels Hofmeyr | 78ada64 | 2017-03-10 02:15:20 +0100 | [diff] [blame] | 123 | { |
| 124 | return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh), |
| 125 | gsm48_hdr_msg_type(gh)); |
| 126 | } |
| 127 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 128 | void dtap_expect_tx(const char *hex) |
| 129 | { |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 130 | /* Has the previously expected dtap been received? */ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 131 | OSMO_ASSERT(!dtap_tx_expected); |
| 132 | if (!hex) |
| 133 | return; |
| 134 | dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex); |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 135 | /* Mask the sequence number out */ |
| 136 | if (msgb_length(dtap_tx_expected) >= 2) |
| 137 | dtap_tx_expected->data[1] &= 0x3f; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 138 | dtap_tx_confirmed = false; |
| 139 | } |
| 140 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 141 | int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg); |
| 142 | |
| 143 | void gsup_rx(const char *rx_hex, const char *expect_tx_hex) |
| 144 | { |
| 145 | int rc; |
| 146 | struct msgb *msg; |
| 147 | const char *label; |
| 148 | |
| 149 | gsup_expect_tx(expect_tx_hex); |
| 150 | |
| 151 | msg = msgb_from_hex("gsup", 1024, rx_hex); |
| 152 | label = osmo_gsup_message_type_name(msg->l2h[0]); |
| 153 | fprintf(stderr, "<-- GSUP rx %s: %s\n", label, |
| 154 | osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg))); |
Neels Hofmeyr | 834f94a | 2017-09-28 03:07:16 +0200 | [diff] [blame] | 155 | /* GSUP read cb takes ownership of msgb */ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 156 | rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg); |
| 157 | fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n", |
| 158 | label, rc); |
| 159 | if (expect_tx_hex) |
| 160 | OSMO_ASSERT(gsup_tx_confirmed); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | bool conn_exists(struct gsm_subscriber_connection *conn) |
| 164 | { |
| 165 | struct gsm_subscriber_connection *c; |
| 166 | llist_for_each_entry(c, &net->subscr_conns, entry) { |
| 167 | if (c == conn) |
| 168 | return true; |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Vadim Yanitskiy | 2760585 | 2018-06-15 23:57:30 +0700 | [diff] [blame] | 173 | /* Simplified version of the cm_service_request_concludes() */ |
| 174 | void conn_conclude_cm_service_req(struct gsm_subscriber_connection *conn, |
| 175 | enum ran_type via_ran) |
| 176 | { |
| 177 | btw("Concluding CM Service Request"); |
| 178 | |
| 179 | OSMO_ASSERT(conn); |
| 180 | OSMO_ASSERT(conn->received_cm_service_request); |
| 181 | |
| 182 | conn->received_cm_service_request = false; |
| 183 | msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE); |
| 184 | |
| 185 | ASSERT_RELEASE_CLEAR(via_ran); |
| 186 | } |
| 187 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 188 | enum ran_type rx_from_ran = RAN_GERAN_A; |
| 189 | |
| 190 | struct gsm_subscriber_connection *conn_new(void) |
| 191 | { |
| 192 | struct gsm_subscriber_connection *conn; |
Neels Hofmeyr | 93c7463 | 2018-04-02 23:10:28 +0200 | [diff] [blame] | 193 | conn = msc_subscr_conn_alloc(net, rx_from_ran, 23); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 194 | if (conn->via_ran == RAN_UTRAN_IU) { |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 195 | struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx); |
| 196 | *ue_ctx = (struct ranap_ue_conn_ctx){ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 197 | .conn_id = 42, |
| 198 | }; |
| 199 | conn->iu.ue_ctx = ue_ctx; |
| 200 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 201 | return conn; |
| 202 | } |
| 203 | |
| 204 | struct gsm_subscriber_connection *g_conn = NULL; |
| 205 | |
| 206 | void rx_from_ms(struct msgb *msg) |
| 207 | { |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 208 | struct gsm48_hdr *gh = msgb_l3(msg); |
Neels Hofmeyr | 78ada64 | 2017-03-10 02:15:20 +0100 | [diff] [blame] | 209 | |
| 210 | log("MSC <--%s-- MS: %s", |
| 211 | ran_type_name(rx_from_ran), |
| 212 | gh_type_name(gh)); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 213 | |
| 214 | if (g_conn && !conn_exists(g_conn)) |
| 215 | g_conn = NULL; |
| 216 | |
| 217 | if (!g_conn) { |
| 218 | log("new conn"); |
| 219 | g_conn = conn_new(); |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 220 | reset_l3_seq_nr(); |
| 221 | patch_l3_seq_nr(msg); |
Neels Hofmeyr | e3d3dc6 | 2018-03-31 00:02:14 +0200 | [diff] [blame] | 222 | msc_compl_l3(g_conn, msg, 23); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 223 | } else { |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 224 | patch_l3_seq_nr(msg); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 225 | if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR) |
| 226 | && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL)) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 227 | msc_cipher_mode_compl(g_conn, msg, 0); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 228 | else |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 229 | msc_dtap(g_conn, 23, msg); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | if (g_conn && !conn_exists(g_conn)) |
| 233 | g_conn = NULL; |
| 234 | } |
| 235 | |
| 236 | void ms_sends_msg(const char *hex) |
| 237 | { |
| 238 | struct msgb *msg; |
| 239 | |
| 240 | msg = msgb_from_hex("ms_sends_msg", 1024, hex); |
| 241 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 242 | rx_from_ms(msg); |
| 243 | talloc_free(msg); |
| 244 | } |
| 245 | |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | int rc; |
| 249 | struct msgb *msg; |
| 250 | struct gsm48_hdr *gh; |
| 251 | |
| 252 | msg = msgb_alloc(1024, "ms_sends_msg_fake"); |
| 253 | msg->l1h = msg->l2h = msg->l3h = msg->data; |
| 254 | |
| 255 | gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh)); |
| 256 | gh->proto_discr = pdisc; |
| 257 | gh->msg_type = msg_type; |
| 258 | /* some amount of data, whatever */ |
| 259 | msgb_put(msg, 123); |
| 260 | |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 261 | patch_l3_seq_nr(msg); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 262 | rc = gsm0408_dispatch(g_conn, msg); |
| 263 | |
| 264 | talloc_free(msg); |
| 265 | return rc; |
| 266 | } |
| 267 | |
Max | 6817190 | 2018-02-06 17:55:19 +0100 | [diff] [blame] | 268 | static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype) |
| 269 | { |
| 270 | int rc = ms_sends_msg_fake(val, msgtype); |
| 271 | if (rc != -EACCES) |
| 272 | log("Unexpected return value %u != %u for %s/%s", |
| 273 | -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype)); |
| 274 | } |
| 275 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 276 | void thwart_rx_non_initial_requests() |
| 277 | { |
| 278 | log("requests shall be thwarted"); |
Max | 6817190 | 2018-02-06 17:55:19 +0100 | [diff] [blame] | 279 | |
| 280 | ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP); |
| 281 | ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */ |
| 282 | ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1); |
| 283 | ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void send_sms(struct vlr_subscr *receiver, |
| 287 | struct vlr_subscr *sender, |
| 288 | char *str) |
| 289 | { |
Harald Welte | 39b5548 | 2018-04-09 19:19:33 +0200 | [diff] [blame] | 290 | struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 291 | gsm411_send_sms_subscr(receiver, sms); |
| 292 | } |
| 293 | |
| 294 | unsigned char next_rand_byte = 0; |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 295 | /* override, requires '-Wl,--wrap=osmo_get_rand_id' */ |
| 296 | int __real_osmo_get_rand_id(uint8_t *buf, size_t num); |
| 297 | int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 298 | { |
Max | 753c15d | 2017-12-21 14:50:44 +0100 | [diff] [blame] | 299 | size_t i; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 300 | for (i = 0; i < num; i++) |
| 301 | buf[i] = next_rand_byte++; |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | /* override, requires '-Wl,--wrap=gsm340_gen_scts' */ |
| 306 | void __real_gsm340_gen_scts(uint8_t *scts, time_t time); |
| 307 | void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time) |
| 308 | { |
Neels Hofmeyr | 05230ea | 2017-08-22 18:06:42 +0200 | [diff] [blame] | 309 | /* Write fixed time bytes for deterministic test results */ |
| 310 | osmo_hexparse("07101000000000", scts, 7); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | const char *paging_expecting_imsi = NULL; |
| 314 | uint32_t paging_expecting_tmsi; |
| 315 | bool paging_sent; |
| 316 | bool paging_stopped; |
| 317 | |
| 318 | void paging_expect_imsi(const char *imsi) |
| 319 | { |
| 320 | paging_expecting_imsi = imsi; |
| 321 | paging_expecting_tmsi = GSM_RESERVED_TMSI; |
| 322 | } |
| 323 | |
| 324 | void paging_expect_tmsi(uint32_t tmsi) |
| 325 | { |
| 326 | paging_expecting_tmsi = tmsi; |
| 327 | paging_expecting_imsi = NULL; |
| 328 | } |
| 329 | |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 330 | static int _paging_sent(enum ran_type via_ran, const char *imsi, uint32_t tmsi, uint32_t lac) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 331 | { |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 332 | log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u", |
| 333 | ran_type_name(via_ran), imsi, tmsi, lac); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 334 | OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI)); |
| 335 | if (paging_expecting_imsi) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 336 | VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d"); |
| 337 | if (paging_expecting_tmsi != GSM_RESERVED_TMSI) { |
| 338 | VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x"); |
| 339 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 340 | paging_sent = true; |
| 341 | paging_stopped = false; |
| 342 | return 1; |
| 343 | } |
| 344 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 345 | /* override, requires '-Wl,--wrap=ranap_iu_page_cs' */ |
| 346 | int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac); |
| 347 | int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 348 | { |
| 349 | return _paging_sent(RAN_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac); |
| 350 | } |
| 351 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 352 | /* override, requires '-Wl,--wrap=a_iface_tx_paging' */ |
| 353 | int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac); |
| 354 | int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 355 | { |
| 356 | return _paging_sent(RAN_GERAN_A, imsi, tmsi, lac); |
| 357 | } |
| 358 | |
| 359 | /* override, requires '-Wl,--wrap=msc_stop_paging' */ |
| 360 | void __real_msc_stop_paging(struct vlr_subscr *vsub); |
| 361 | void __wrap_msc_stop_paging(struct vlr_subscr *vsub) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 362 | { |
| 363 | paging_stopped = true; |
| 364 | } |
| 365 | |
| 366 | void clear_vlr() |
| 367 | { |
| 368 | struct vlr_subscr *vsub, *n; |
| 369 | llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) { |
| 370 | vlr_subscr_free(vsub); |
| 371 | } |
| 372 | |
| 373 | net->authentication_required = false; |
Harald Welte | 7b222aa | 2017-12-23 19:30:32 +0100 | [diff] [blame] | 374 | net->a5_encryption_mask = (1 << 0); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 375 | net->vlr->cfg.check_imei_rqd = false; |
| 376 | net->vlr->cfg.assign_tmsi = false; |
Neels Hofmeyr | 54a706c | 2017-07-18 15:39:27 +0200 | [diff] [blame] | 377 | net->vlr->cfg.retrieve_imeisv_early = false; |
| 378 | net->vlr->cfg.retrieve_imeisv_ciphered = false; |
Neels Hofmeyr | 7b1418e | 2017-10-29 02:12:16 +0100 | [diff] [blame] | 379 | net->vlr->cfg.auth_tuple_max_reuse_count = 0; |
| 380 | net->vlr->cfg.auth_reuse_old_sets_on_error = false; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 381 | |
| 382 | rx_from_ran = RAN_GERAN_A; |
| 383 | auth_request_sent = false; |
| 384 | auth_request_expect_rand = NULL; |
| 385 | auth_request_expect_autn = NULL; |
| 386 | |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 387 | cipher_mode_cmd_sent = false; |
| 388 | cipher_mode_cmd_sent_with_imeisv = false; |
| 389 | cipher_mode_expect_kc = NULL; |
| 390 | |
| 391 | security_mode_ctrl_sent = false; |
| 392 | security_mode_expect_ck = NULL; |
| 393 | security_mode_expect_ik = NULL; |
| 394 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 395 | next_rand_byte = 0; |
| 396 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 397 | iu_release_expected = false; |
| 398 | iu_release_sent = false; |
| 399 | bssap_clear_expected = false; |
| 400 | bssap_clear_sent = false; |
| 401 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 402 | osmo_gettimeofday_override = false; |
| 403 | } |
| 404 | |
| 405 | static struct log_info_cat test_categories[] = { |
| 406 | [DMSC] = { |
| 407 | .name = "DMSC", |
| 408 | .description = "Mobile Switching Center", |
| 409 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 410 | }, |
| 411 | [DRLL] = { |
| 412 | .name = "DRLL", |
| 413 | .description = "A-bis Radio Link Layer (RLL)", |
| 414 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 415 | }, |
| 416 | [DMM] = { |
| 417 | .name = "DMM", |
| 418 | .description = "Layer3 Mobility Management (MM)", |
| 419 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 420 | }, |
| 421 | [DRR] = { |
| 422 | .name = "DRR", |
| 423 | .description = "Layer3 Radio Resource (RR)", |
| 424 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 425 | }, |
| 426 | [DCC] = { |
| 427 | .name = "DCC", |
| 428 | .description = "Layer3 Call Control (CC)", |
Neels Hofmeyr | 12e17be | 2018-03-12 23:59:37 +0100 | [diff] [blame] | 429 | .enabled = 1, .loglevel = LOGL_DEBUG, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 430 | }, |
| 431 | [DMM] = { |
| 432 | .name = "DMM", |
| 433 | .description = "Layer3 Mobility Management (MM)", |
| 434 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 435 | }, |
| 436 | [DVLR] = { |
| 437 | .name = "DVLR", |
| 438 | .description = "Visitor Location Register", |
| 439 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 440 | }, |
| 441 | [DREF] = { |
| 442 | .name = "DREF", |
| 443 | .description = "Reference Counting", |
| 444 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 445 | }, |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 446 | [DPAG] = { |
| 447 | .name = "DPAG", |
| 448 | .description = "Paging Subsystem", |
| 449 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 450 | }, |
| 451 | [DIUCS] = { |
| 452 | .name = "DIUCS", |
| 453 | .description = "Iu-CS Protocol", |
| 454 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 455 | }, |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 456 | [DMNCC] = { |
| 457 | .name = "DMNCC", |
| 458 | .description = "MNCC API for Call Control application", |
| 459 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 460 | }, |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | static struct log_info info = { |
| 464 | .cat = test_categories, |
| 465 | .num_cat = ARRAY_SIZE(test_categories), |
| 466 | }; |
| 467 | |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 468 | int mncc_recv(struct gsm_network *net, struct msgb *msg) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 469 | { |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 470 | struct gsm_mncc *mncc = (void*)msg->data; |
| 471 | log("MSC --> MNCC: callref 0x%x: %s", mncc->callref, |
| 472 | get_mncc_name(mncc->msg_type)); |
| 473 | |
| 474 | OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type); |
| 475 | if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) { |
| 476 | log("Mismatch! Expected MNCC msg type: %s", |
| 477 | get_mncc_name(cc_to_mncc_tx_expected_msg_type)); |
| 478 | abort(); |
| 479 | } |
| 480 | |
| 481 | if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) { |
| 482 | log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'", |
| 483 | cc_to_mncc_tx_expected_imsi, |
| 484 | mncc->imsi); |
| 485 | abort(); |
| 486 | } |
| 487 | |
| 488 | cc_to_mncc_tx_confirmed = true; |
| 489 | cc_to_mncc_tx_got_callref = mncc->callref; |
| 490 | cc_to_mncc_tx_expected_imsi = NULL; |
| 491 | cc_to_mncc_tx_expected_msg_type = 0; |
| 492 | talloc_free(msg); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* override, requires '-Wl,--wrap=gsup_client_create' */ |
| 497 | struct gsup_client * |
| 498 | __real_gsup_client_create(const char *ip_addr, unsigned int tcp_port, |
| 499 | gsup_client_read_cb_t read_cb, |
| 500 | struct oap_client_config *oap_config); |
| 501 | struct gsup_client * |
| 502 | __wrap_gsup_client_create(const char *ip_addr, unsigned int tcp_port, |
| 503 | gsup_client_read_cb_t read_cb, |
| 504 | struct oap_client_config *oap_config) |
| 505 | { |
| 506 | struct gsup_client *gsupc; |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 507 | gsupc = talloc_zero(msc_vlr_tests_ctx, struct gsup_client); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 508 | OSMO_ASSERT(gsupc); |
| 509 | return gsupc; |
| 510 | } |
| 511 | |
| 512 | /* override, requires '-Wl,--wrap=gsup_client_send' */ |
| 513 | int __real_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg); |
| 514 | int __wrap_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg) |
| 515 | { |
| 516 | const char *is = osmo_hexdump_nospc(msg->data, msg->len); |
| 517 | fprintf(stderr, "GSUP --> HLR: %s: %s\n", |
| 518 | osmo_gsup_message_type_name(msg->data[0]), is); |
| 519 | |
| 520 | OSMO_ASSERT(gsup_tx_expected); |
| 521 | if (strcmp(gsup_tx_expected, is)) { |
| 522 | fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected); |
| 523 | abort(); |
| 524 | } |
| 525 | |
| 526 | talloc_free(msg); |
| 527 | gsup_tx_confirmed = true; |
| 528 | gsup_tx_expected = NULL; |
| 529 | return 0; |
| 530 | } |
| 531 | |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 532 | static int _validate_dtap(struct msgb *msg, enum ran_type to_ran) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 533 | { |
Neels Hofmeyr | 78ada64 | 2017-03-10 02:15:20 +0100 | [diff] [blame] | 534 | btw("DTAP --%s--> MS: %s: %s", |
Max | d8d1a9e | 2018-02-06 15:50:20 +0100 | [diff] [blame] | 535 | ran_type_name(to_ran), gh_type_name((void*)msg->data), |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 536 | osmo_hexdump_nospc(msg->data, msg->len)); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 537 | |
| 538 | OSMO_ASSERT(dtap_tx_expected); |
Harald Welte | c200785 | 2018-02-03 21:08:26 +0100 | [diff] [blame] | 539 | |
| 540 | /* Mask the sequence number out before comparing */ |
| 541 | msg->data[1] &= 0x3f; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 542 | if (msg->len != dtap_tx_expected->len |
| 543 | || memcmp(msg->data, dtap_tx_expected->data, msg->len)) { |
| 544 | fprintf(stderr, "Mismatch! Expected:\n%s\n", |
| 545 | osmo_hexdump_nospc(dtap_tx_expected->data, |
| 546 | dtap_tx_expected->len)); |
| 547 | abort(); |
| 548 | } |
| 549 | |
| 550 | btw("DTAP matches expected message"); |
| 551 | |
| 552 | talloc_free(msg); |
| 553 | dtap_tx_confirmed = true; |
| 554 | talloc_free(dtap_tx_expected); |
| 555 | dtap_tx_expected = NULL; |
| 556 | return 0; |
| 557 | } |
| 558 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 559 | /* override, requires '-Wl,--wrap=ranap_iu_tx' */ |
| 560 | int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi); |
| 561 | int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 562 | { |
| 563 | return _validate_dtap(msg, RAN_UTRAN_IU); |
| 564 | } |
| 565 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 566 | /* override, requires '-Wl,--wrap=ranap_iu_tx_release' */ |
| 567 | int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause); |
| 568 | int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 569 | { |
| 570 | btw("Iu Release --%s--> MS", ran_type_name(RAN_UTRAN_IU)); |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 571 | OSMO_ASSERT(iu_release_expected); |
| 572 | iu_release_expected = false; |
| 573 | iu_release_sent = true; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /* override, requires '-Wl,--wrap=iu_tx_common_id' */ |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 578 | int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi); |
| 579 | int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 580 | { |
| 581 | btw("Iu Common ID --%s--> MS (IMSI=%s)", ran_type_name(RAN_UTRAN_IU), imsi); |
| 582 | return 0; |
| 583 | } |
| 584 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 585 | /* override, requires '-Wl,--wrap=a_iface_tx_dtap' */ |
| 586 | int __real_a_iface_tx_dtap(struct msgb *msg); |
| 587 | int __wrap_a_iface_tx_dtap(struct msgb *msg) |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 588 | { |
| 589 | return _validate_dtap(msg, RAN_GERAN_A); |
| 590 | } |
| 591 | |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 592 | /* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */ |
| 593 | int __real_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn); |
| 594 | int __wrap_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn) |
| 595 | { |
| 596 | btw("BSSAP Clear --%s--> MS", ran_type_name(RAN_GERAN_A)); |
| 597 | OSMO_ASSERT(bssap_clear_expected); |
| 598 | bssap_clear_expected = false; |
| 599 | bssap_clear_sent = true; |
| 600 | return 0; |
| 601 | } |
| 602 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 603 | /* override, requires '-Wl,--wrap=msc_mgcp_call_assignment' */ |
| 604 | int __real_msc_mgcp_call_assignment(struct gsm_trans *trans); |
| 605 | int __wrap_msc_mgcp_call_assignment(struct gsm_trans *trans) |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 606 | { |
| 607 | log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x", |
| 608 | vlr_subscr_name(trans->vsub), trans->callref); |
| 609 | return 0; |
| 610 | } |
| 611 | |
Neels Hofmeyr | cbcf89c | 2018-03-13 17:52:07 +0100 | [diff] [blame] | 612 | struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL; |
| 613 | |
Philipp Maier | 621ba03 | 2017-11-07 17:19:25 +0100 | [diff] [blame] | 614 | /* override, requires '-Wl,--wrap=msc_mgcp_call_release' */ |
| 615 | void __real_msc_mgcp_call_release(struct gsm_trans *trans); |
| 616 | void __wrap_msc_mgcp_call_release(struct gsm_trans *trans) |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 617 | { |
| 618 | log("MS <--Call Release-- MSC: subscr=%s callref=0x%x", |
| 619 | vlr_subscr_name(trans->vsub), trans->callref); |
Neels Hofmeyr | cbcf89c | 2018-03-13 17:52:07 +0100 | [diff] [blame] | 620 | if (on_call_release_mncc_sends_to_cc_data) { |
| 621 | mncc_tx_to_cc(trans->net, on_call_release_mncc_sends_to_cc_data->msg_type, |
| 622 | on_call_release_mncc_sends_to_cc_data); |
| 623 | on_call_release_mncc_sends_to_cc_data = NULL; |
| 624 | } |
Neels Hofmeyr | a99b427 | 2017-11-21 17:13:23 +0100 | [diff] [blame] | 625 | } |
| 626 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 627 | static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi) |
| 628 | { |
| 629 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 630 | if (send_tmsi == GSM_RESERVED_TMSI) |
| 631 | btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub)); |
| 632 | else |
| 633 | btw("sending LU Accept for %s, with TMSI 0x%08x", |
| 634 | vlr_subscr_name(conn->vsub), send_tmsi); |
| 635 | lu_result_sent |= RES_ACCEPT; |
| 636 | return 0; |
| 637 | } |
| 638 | |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 639 | 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] | 640 | { |
| 641 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 642 | btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause); |
| 643 | lu_result_sent |= RES_REJECT; |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref) |
| 648 | { |
| 649 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 650 | btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub)); |
| 651 | cm_service_result_sent |= RES_ACCEPT; |
| 652 | return 0; |
| 653 | } |
| 654 | |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 655 | static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 656 | { |
| 657 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
Neels Hofmeyr | 1580959 | 2018-04-06 02:57:51 +0200 | [diff] [blame] | 658 | btw("sending CM Service Reject for %s, cause: %s", |
| 659 | vlr_subscr_name(conn->vsub), gsm48_reject_value_name(cause)); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 660 | cm_service_result_sent |= RES_REJECT; |
| 661 | return 0; |
| 662 | } |
| 663 | |
| 664 | static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at, |
| 665 | bool send_autn) |
| 666 | { |
| 667 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 668 | char *hex; |
| 669 | bool ok = true; |
| 670 | btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...", |
| 671 | send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub), |
| 672 | at->use_count, at->key_seq, at->vec.auth_types); |
| 673 | |
| 674 | hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand)); |
| 675 | btw("...rand=%s", hex); |
| 676 | if (!auth_request_expect_rand |
| 677 | || strcmp(hex, auth_request_expect_rand) != 0) { |
| 678 | ok = false; |
| 679 | log("FAILURE: expected rand=%s", |
| 680 | auth_request_expect_rand ? auth_request_expect_rand : "-"); |
| 681 | } |
| 682 | |
| 683 | if (send_autn) { |
| 684 | hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn)); |
| 685 | btw("...autn=%s", hex); |
| 686 | if (!auth_request_expect_autn |
| 687 | || strcmp(hex, auth_request_expect_autn) != 0) { |
| 688 | ok = false; |
| 689 | log("FAILURE: expected autn=%s", |
| 690 | auth_request_expect_autn ? auth_request_expect_autn : "-"); |
| 691 | } |
| 692 | } else if (auth_request_expect_autn) { |
| 693 | ok = false; |
| 694 | log("FAILURE: no AUTN sent, expected AUTN = %s", |
| 695 | auth_request_expect_autn); |
| 696 | } |
| 697 | |
| 698 | if (send_autn) |
| 699 | btw("...expecting res=%s", |
| 700 | osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len)); |
| 701 | else |
| 702 | btw("...expecting sres=%s", |
| 703 | osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres))); |
| 704 | |
| 705 | auth_request_sent = ok; |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static int fake_vlr_tx_auth_rej(void *msc_conn_ref) |
| 710 | { |
| 711 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
| 712 | btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub)); |
| 713 | return 0; |
| 714 | } |
| 715 | |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 716 | /* override, requires '-Wl,--wrap=a_iface_tx_cipher_mode' */ |
| 717 | int __real_a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn, |
| 718 | struct gsm0808_encrypt_info *ei, int include_imeisv); |
| 719 | int __wrap_a_iface_tx_cipher_mode(const struct gsm_subscriber_connection *conn, |
| 720 | struct gsm0808_encrypt_info *ei, int include_imeisv) |
| 721 | { |
| 722 | int i; |
| 723 | btw("sending Ciphering Mode Command for %s: include_imeisv=%d", |
| 724 | vlr_subscr_name(conn->vsub), include_imeisv); |
| 725 | for (i = 0; i < ei->perm_algo_len; i++) |
| 726 | btw("...perm algo: %u", ei->perm_algo[i]); |
| 727 | OSMO_ASSERT(ei->key_len <= sizeof(ei->key)); |
| 728 | btw("...key: %s", osmo_hexdump_nospc(ei->key, ei->key_len)); |
| 729 | cipher_mode_cmd_sent = true; |
| 730 | cipher_mode_cmd_sent_with_imeisv = include_imeisv; |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 731 | |
| 732 | if (!cipher_mode_expect_kc |
| 733 | || strcmp(cipher_mode_expect_kc, osmo_hexdump_nospc(ei->key, ei->key_len))) { |
| 734 | log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL"); |
| 735 | OSMO_ASSERT(false); |
| 736 | } |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /* override, requires '-Wl,--wrap=ranap_iu_tx_sec_mode_cmd' */ |
| 741 | int __real_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec, |
| 742 | int send_ck, int new_key); |
| 743 | int __wrap_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec, |
| 744 | int send_ck, int new_key) |
| 745 | { |
| 746 | btw("sending SecurityModeControl for UE ctx %u send_ck=%d new_key=%d", |
| 747 | uectx->conn_id, send_ck, new_key); |
| 748 | btw("...ik=%s", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik))); |
| 749 | if (send_ck) |
| 750 | btw("...ck=%s", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck))); |
Neels Hofmeyr | dbabfd3 | 2018-03-10 02:06:47 +0100 | [diff] [blame] | 751 | security_mode_ctrl_sent = true; |
| 752 | if (!security_mode_expect_ik |
| 753 | || strcmp(security_mode_expect_ik, osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)))) { |
| 754 | log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL"); |
| 755 | OSMO_ASSERT(false); |
| 756 | } |
| 757 | if (((!!send_ck) != (!!security_mode_expect_ck)) |
| 758 | || (security_mode_expect_ck |
| 759 | && strcmp(security_mode_expect_ck, osmo_hexdump_nospc(vec->ck, sizeof(vec->ck))))) { |
| 760 | log("FAILURE: expected ck=%s", security_mode_expect_ck ? : "NULL"); |
| 761 | OSMO_ASSERT(false); |
| 762 | } |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | extern int msc_vlr_set_ciph_mode(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv); |
| 767 | |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 768 | static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 769 | { |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 770 | int rc; |
| 771 | #ifndef BUILD_IU |
| 772 | /* If we built without support for IU, fake the IU part here. The root cause is that we don't |
| 773 | * have differing sets of expected outputs for --enable-iu and --disable-iu. */ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 774 | struct gsm_subscriber_connection *conn = msc_conn_ref; |
Neels Hofmeyr | da21a52 | 2018-03-02 01:50:09 +0100 | [diff] [blame] | 775 | if (conn->via_ran == RAN_UTRAN_IU) { |
| 776 | DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n", vlr_subscr_name(conn->vsub)); |
| 777 | rc = __wrap_ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &conn->vsub->last_tuple->vec, |
| 778 | 0, 1); |
| 779 | } else |
| 780 | #endif |
| 781 | rc = msc_vlr_set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv); |
| 782 | if (rc) |
| 783 | btw("ERROR sending ciphering mode command: rc=%d", rc); |
| 784 | return rc; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 785 | } |
| 786 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 787 | void ms_sends_security_mode_complete() |
| 788 | { |
| 789 | OSMO_ASSERT(g_conn); |
| 790 | OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU); |
| 791 | OSMO_ASSERT(g_conn->iu.ue_ctx); |
| 792 | msc_rx_sec_mode_compl(g_conn); |
| 793 | } |
| 794 | |
Neels Hofmeyr | 4068ab2 | 2018-04-01 20:55:54 +0200 | [diff] [blame] | 795 | void bss_sends_clear_complete() |
| 796 | { |
| 797 | btw("BSS sends BSSMAP Clear Complete"); |
| 798 | OSMO_ASSERT(g_conn); |
| 799 | OSMO_ASSERT(g_conn->via_ran == RAN_GERAN_A); |
| 800 | msc_subscr_conn_rx_bssmap_clear_complete(g_conn); |
| 801 | } |
| 802 | |
| 803 | void rnc_sends_release_complete() |
| 804 | { |
| 805 | btw("RNC sends Iu Release Complete"); |
| 806 | OSMO_ASSERT(g_conn); |
| 807 | OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU); |
| 808 | msc_subscr_conn_rx_iu_release_complete(g_conn); |
| 809 | } |
| 810 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 811 | const struct timeval fake_time_start_time = { 123, 456 }; |
| 812 | |
| 813 | void fake_time_start() |
| 814 | { |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 815 | struct timespec *clock_override; |
| 816 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 817 | osmo_gettimeofday_override_time = fake_time_start_time; |
| 818 | osmo_gettimeofday_override = true; |
Stefan Sperling | defc3c8 | 2018-05-15 14:48:04 +0200 | [diff] [blame] | 819 | clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC); |
| 820 | OSMO_ASSERT(clock_override); |
| 821 | clock_override->tv_sec = fake_time_start_time.tv_sec; |
| 822 | clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000; |
| 823 | osmo_clock_override_enable(CLOCK_MONOTONIC, true); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 824 | fake_time_passes(0, 0); |
| 825 | } |
| 826 | |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 827 | static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 828 | { |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 829 | /* Verifying that the msgb context is empty */ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 830 | talloc_report_full(msgb_ctx, stderr); |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 831 | /* Expecting these to stick around in msc_vlr_tests_ctx: |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 832 | * talloc_total_blocks(tall_bsc_ctx) == 12 |
| 833 | * full talloc report on 'msc_vlr_tests_ctx' (total 3636 bytes in 12 blocks) |
| 834 | * struct gsup_client contains 248 bytes in 1 blocks (ref 0) 0x563a489c05f0 |
| 835 | * struct gsm_network contains 2031 bytes in 4 blocks (ref 0) 0x563a489bfbb0 |
| 836 | * struct vlr_instance contains 168 bytes in 1 blocks (ref 0) 0x563a489c04e0 |
| 837 | * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x563a489c0460 |
| 838 | * ../../../src/libosmocore/src/rate_ctr.c:228 contains 1552 bytes in 1 blocks (ref 0) 0x563a489bfd40 |
| 839 | * logging contains 1357 bytes in 5 blocks (ref 0) 0x563a489bf440 |
| 840 | * struct log_target contains 228 bytes in 2 blocks (ref 0) 0x563a489bf9f0 |
| 841 | * struct log_category contains 68 bytes in 1 blocks (ref 0) 0x563a489bfb00 |
| 842 | * struct log_info contains 1128 bytes in 2 blocks (ref 0) 0x563a489bf4b0 |
| 843 | * struct log_info_cat contains 1088 bytes in 1 blocks (ref 0) 0x563a489bf540 |
| 844 | * msgb contains 0 bytes in 1 blocks (ref 0) 0x563a489bf3d0 |
| 845 | * (That's 12 counting the root ctx) |
| 846 | */ |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 847 | fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n", |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 848 | talloc_total_blocks(msc_vlr_tests_ctx)); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 849 | if (talloc_total_blocks(msc_vlr_tests_ctx) != 12) |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 850 | talloc_report_full(msc_vlr_tests_ctx, stderr); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 851 | fprintf(stderr, "\n"); |
| 852 | } |
| 853 | |
| 854 | static struct { |
| 855 | bool verbose; |
| 856 | int run_test_nr; |
| 857 | } cmdline_opts = { |
| 858 | .verbose = false, |
| 859 | .run_test_nr = -1, |
| 860 | }; |
| 861 | |
| 862 | static void print_help(const char *program) |
| 863 | { |
| 864 | printf("Usage:\n" |
| 865 | " %s [-v] [N [N...]]\n" |
| 866 | "Options:\n" |
| 867 | " -h --help show this text.\n" |
| 868 | " -v --verbose print source file and line numbers\n" |
| 869 | " N run only the Nth test (first test is N=1)\n", |
| 870 | program |
| 871 | ); |
| 872 | } |
| 873 | |
| 874 | static void handle_options(int argc, char **argv) |
| 875 | { |
| 876 | while (1) { |
| 877 | int option_index = 0, c; |
| 878 | static struct option long_options[] = { |
| 879 | {"help", 0, 0, 'h'}, |
| 880 | {"verbose", 1, 0, 'v'}, |
| 881 | {0, 0, 0, 0} |
| 882 | }; |
| 883 | |
| 884 | c = getopt_long(argc, argv, "hv", |
| 885 | long_options, &option_index); |
| 886 | if (c == -1) |
| 887 | break; |
| 888 | |
| 889 | switch (c) { |
| 890 | case 'h': |
| 891 | print_help(argv[0]); |
| 892 | exit(0); |
| 893 | case 'v': |
| 894 | cmdline_opts.verbose = true; |
| 895 | break; |
| 896 | default: |
| 897 | /* catch unknown options *as well as* missing arguments. */ |
| 898 | fprintf(stderr, "Error in command line options. Exiting.\n"); |
| 899 | exit(-1); |
| 900 | break; |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | void *msgb_ctx = NULL; |
| 906 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 907 | static void run_tests(int nr) |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 908 | { |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 909 | int test_nr; |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 910 | |
| 911 | check_talloc(msgb_ctx, msc_vlr_tests_ctx); |
| 912 | |
Max | 5e2e9bd | 2018-02-06 19:31:08 +0100 | [diff] [blame] | 913 | nr--; /* arg's first test is 1, in here it's 0 */ |
| 914 | for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) { |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 915 | if (nr >= 0 && test_nr != nr) |
| 916 | continue; |
| 917 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 918 | if (cmdline_opts.verbose) |
| 919 | fprintf(stderr, "(test nr %d)\n", test_nr + 1); |
| 920 | |
| 921 | msc_vlr_tests[test_nr](); |
| 922 | |
| 923 | if (cmdline_opts.verbose) |
| 924 | fprintf(stderr, "(test nr %d)\n", test_nr + 1); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 925 | |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 926 | check_talloc(msgb_ctx, msc_vlr_tests_ctx); |
Max | 29ce08a | 2018-02-06 18:46:57 +0100 | [diff] [blame] | 927 | } |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 928 | } |
| 929 | |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 930 | struct gsm_network *test_net(void *ctx) |
| 931 | { |
Neels Hofmeyr | 7f48420 | 2018-02-27 12:59:45 +0100 | [diff] [blame] | 932 | struct gsm_network *net = gsm_network_init(ctx, mncc_recv); |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 933 | |
| 934 | net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server"); |
| 935 | net->gsup_server_port = 0; |
| 936 | |
| 937 | OSMO_ASSERT(msc_vlr_alloc(net) == 0); |
| 938 | OSMO_ASSERT(msc_vlr_start(net) == 0); |
| 939 | OSMO_ASSERT(net->vlr); |
| 940 | OSMO_ASSERT(net->vlr->gsup_client); |
| 941 | |
| 942 | net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc; |
| 943 | net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej; |
| 944 | net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc; |
| 945 | net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej; |
| 946 | net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req; |
| 947 | net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej; |
| 948 | net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd; |
| 949 | |
| 950 | return net; |
| 951 | } |
| 952 | |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 953 | int main(int argc, char **argv) |
| 954 | { |
| 955 | handle_options(argc, argv); |
| 956 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 957 | msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx"); |
| 958 | msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0); |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 959 | osmo_init_logging2(msc_vlr_tests_ctx, &info); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 960 | |
| 961 | _log_lines = cmdline_opts.verbose; |
| 962 | |
| 963 | OSMO_ASSERT(osmo_stderr_target); |
| 964 | log_set_use_color(osmo_stderr_target, 0); |
| 965 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 966 | log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0); |
| 967 | log_set_print_category(osmo_stderr_target, 1); |
| 968 | |
Neels Hofmeyr | 3f5b780 | 2017-12-15 03:49:55 +0100 | [diff] [blame] | 969 | if (cmdline_opts.verbose) |
| 970 | log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG); |
| 971 | |
Neels Hofmeyr | c01e909 | 2018-03-22 15:56:49 +0100 | [diff] [blame] | 972 | net = test_net(msc_vlr_tests_ctx); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 973 | |
| 974 | osmo_fsm_log_addr(false); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 975 | |
Max | 5e60de6 | 2018-02-07 12:56:09 +0100 | [diff] [blame] | 976 | msc_subscr_conn_init(); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 977 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 978 | clear_vlr(); |
| 979 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 980 | if (optind >= argc) |
| 981 | run_tests(-1); |
| 982 | else { |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 983 | int arg; |
| 984 | long int nr; |
| 985 | for (arg = optind; arg < argc; arg++) { |
Neels Hofmeyr | 9c848b5 | 2017-11-22 01:59:36 +0100 | [diff] [blame] | 986 | errno = 0; |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 987 | nr = strtol(argv[arg], NULL, 10); |
| 988 | if (errno) { |
| 989 | fprintf(stderr, "Invalid argument: %s\n", |
| 990 | argv[arg]); |
| 991 | exit(1); |
| 992 | } |
| 993 | |
Neels Hofmeyr | dfdc61d | 2018-03-02 00:40:58 +0100 | [diff] [blame] | 994 | run_tests(nr); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
| 998 | printf("Done\n"); |
| 999 | |
Neels Hofmeyr | 08b3828 | 2018-03-30 23:04:04 +0200 | [diff] [blame] | 1000 | check_talloc(msgb_ctx, msc_vlr_tests_ctx); |
Neels Hofmeyr | 6a29d32 | 2017-01-25 15:04:16 +0100 | [diff] [blame] | 1001 | return 0; |
| 1002 | } |