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