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