blob: 21af3ca8d797fb328005cf389c61cb51c7650dff [file] [log] [blame]
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001/* 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 Hofmeyrc4628a32018-12-07 14:47:34 +010033#include <osmocom/msc/gsup_client_mux.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020034#include <osmocom/msc/gsm_04_11.h>
35#include <osmocom/msc/debug.h>
Max43b01b02017-09-15 11:22:30 +020036#include <osmocom/msc/gsm_04_08.h>
Neels Hofmeyra99b4272017-11-21 17:13:23 +010037#include <osmocom/msc/transaction.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010038#include <osmocom/msc/ran_msg.h>
39#include <osmocom/msc/msc_a.h>
40#include <osmocom/msc/msc_i.h>
41#include <osmocom/msc/msc_t.h>
42#include <osmocom/msc/call_leg.h>
43#include <osmocom/msc/rtp_stream.h>
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010044
45#include "msc_vlr_tests.h"
46
Neels Hofmeyrc01e9092018-03-22 15:56:49 +010047void *msc_vlr_tests_ctx = NULL;
48
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010049bool _log_lines = false;
50
51struct gsm_network *net = NULL;
52
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010053const char *gsup_tx_expected = NULL;
54bool gsup_tx_confirmed;
55
56struct msgb *dtap_tx_expected = NULL;
57bool dtap_tx_confirmed;
58
59enum result_sent lu_result_sent;
60enum result_sent cm_service_result_sent;
61bool auth_request_sent;
62const char *auth_request_expect_rand;
63const char *auth_request_expect_autn;
64bool cipher_mode_cmd_sent;
65bool cipher_mode_cmd_sent_with_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +010066const char *cipher_mode_expect_kc;
67bool security_mode_ctrl_sent;
68const char *security_mode_expect_ck;
69const char *security_mode_expect_ik;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010070
Philipp Maierfbf66102017-04-09 12:32:51 +020071bool iu_release_expected = false;
72bool iu_release_sent = false;
73bool bssap_clear_expected = false;
74bool bssap_clear_sent = false;
75
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +020076bool bssap_assignment_expected = false;
77bool bssap_assignment_sent = false;
78bool iu_rab_assignment_expected = false;
79bool iu_rab_assignment_sent = false;
80
Neels Hofmeyra99b4272017-11-21 17:13:23 +010081uint32_t cc_to_mncc_tx_expected_msg_type = 0;
82const char *cc_to_mncc_tx_expected_imsi = NULL;
83bool cc_to_mncc_tx_confirmed = false;
84uint32_t cc_to_mncc_tx_got_callref = 0;
85
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +020086enum rtp_direction expecting_crcx = -1;
87bool got_crcx = false;
88
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010089extern int ran_dec_dtap_undup_pdisc_ctr_bin(uint8_t pdisc);
Harald Weltec2007852018-02-03 21:08:26 +010090
91/* static state variables for the L3 send sequence numbers */
92static uint8_t n_sd[4];
93
94/* patch a correct send sequence number into the given message */
95static void patch_l3_seq_nr(struct msgb *msg)
96{
97 struct gsm48_hdr *gh = msgb_l3(msg);
98 uint8_t pdisc = gsm48_hdr_pdisc(gh);
99 uint8_t *msg_type_oct = &msg->l3h[1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100100 int bin = ran_dec_dtap_undup_pdisc_ctr_bin(pdisc);
Harald Weltec2007852018-02-03 21:08:26 +0100101
102 if (bin >= 0 && bin < ARRAY_SIZE(n_sd)) {
103 /* patch in n_sd into the msg_type octet */
104 *msg_type_oct = (*msg_type_oct & 0x3f) | ((n_sd[bin] & 0x3) << 6);
105 //fprintf(stderr, "pdisc=0x%02x bin=%d, patched n_sd=%u\n\n", pdisc, bin, n_sd[bin] & 3);
106 /* increment N(SD) */
107 n_sd[bin] = (n_sd[bin] + 1) % 4;
108 } else {
109 //fprintf(stderr, "pdisc=0x%02x NO SEQ\n\n", pdisc);
110 }
111}
112
113/* reset L3 sequence numbers (e.g. new RR connection) */
114static void reset_l3_seq_nr()
115{
116 memset(n_sd, 0, sizeof(n_sd));
117}
118
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100119struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
120{
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200121 struct msgb *msg = msgb_alloc_headroom(size, 4, label);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100122 unsigned char *rc;
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200123 msg->l2h = msg->data;
124 rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg)));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100125 OSMO_ASSERT(rc == msg->l2h);
126 return msg;
127}
128
Maxd8d1a9e2018-02-06 15:50:20 +0100129static const char *gh_type_name(struct gsm48_hdr *gh)
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100130{
131 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
132 gsm48_hdr_msg_type(gh));
133}
134
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100135void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
136{
137 int rc;
138 struct msgb *msg;
139 const char *label;
140
141 gsup_expect_tx(expect_tx_hex);
142
143 msg = msgb_from_hex("gsup", 1024, rx_hex);
144 label = osmo_gsup_message_type_name(msg->l2h[0]);
145 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
146 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
Neels Hofmeyr834f94a2017-09-28 03:07:16 +0200147 /* GSUP read cb takes ownership of msgb */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100148 rc = gsup_client_mux_rx(net->gcm->gsup_client, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100149 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
150 label, rc);
151 if (expect_tx_hex)
152 OSMO_ASSERT(gsup_tx_confirmed);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100153}
154
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100155bool conn_exists(const struct msub *msub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100156{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100157 struct msub *i;
Maxba8a0072018-10-25 17:58:43 +0200158
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100159 if (!msub)
Maxba8a0072018-10-25 17:58:43 +0200160 return false;
161
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100162 llist_for_each_entry(i, &msub_list, entry) {
163 if (i == msub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100164 return true;
165 }
Maxba8a0072018-10-25 17:58:43 +0200166
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100167 btw("msub gone");
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100168 return false;
169}
170
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700171/* Simplified version of the cm_service_request_concludes() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100172void conn_conclude_cm_service_req(struct msub *msub, const char *cm_service_use)
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700173{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100174 int32_t count;
175 struct msc_a *msc_a = msub_msc_a(msub);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700176 btw("Concluding CM Service Request");
177
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100178 OSMO_ASSERT(conn_exists(msub));
179 count = osmo_use_count_by(&msc_a->use_count, cm_service_use);
180 OSMO_ASSERT(count > 0);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700181
Vadim Yanitskiyd14422a2019-07-09 00:37:49 +0700182 OSMO_ASSERT(osmo_use_count_get_put(&msc_a->use_count, cm_service_use, -count) == 0);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700183
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100184 ASSERT_RELEASE_CLEAR(msc_a->c.ran->type);
185}
186
187void dummy_msc_i_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
188{
189}
190
191static const struct osmo_fsm_state dummy_msc_i_states[] = {
192 {
193 .name = "0",
194 .in_event_mask = 0xffffffff,
195 .action = dummy_msc_i_action,
196 },
197};
198
199struct osmo_fsm dummy_msc_i_fsm = {
200 .name = "dummy_msc_i",
201 .states = dummy_msc_i_states,
202 .num_states = ARRAY_SIZE(dummy_msc_i_states),
203 .log_subsys = DMSC,
204 .event_names = msc_i_fsm_event_names,
205};
206
207struct msc_i *dummy_msc_i_alloc(struct msub *msub, struct ran_infra *ran)
208{
209 return msub_role_alloc(g_msub, MSC_ROLE_I, &dummy_msc_i_fsm, struct msc_i, ran);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700210}
211
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100212enum osmo_rat_type rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100213
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100214struct msub *g_msub = NULL;
Harald Welte977b5482019-02-18 12:23:43 +0100215
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100216void dtap_expect_tx(const char *hex)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100217{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100218 /* Has the previously expected dtap been received? */
219 OSMO_ASSERT(!dtap_tx_expected);
220 if (!hex)
221 return;
222 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
223 /* Mask the sequence number out */
224 if (msgb_length(dtap_tx_expected) >= 2)
225 dtap_tx_expected->data[1] &= 0x3f;
226 dtap_tx_confirmed = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100227}
228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100229static int _validate_dtap(struct msgb *msg, enum osmo_rat_type to_ran)
230{
231 struct gsm48_hdr *gh = (void*)msg->data;
232 uint8_t pdisc = gsm48_hdr_pdisc(gh);
233 uint8_t msgt = gsm48_hdr_msg_type(gh);
234
235 btw("DTAP --%s--> MS: %s: %s",
236 osmo_rat_type_name(to_ran), gh_type_name((void*)msg->data),
237 osmo_hexdump_nospc(msg->data, msg->len));
238
239 if (pdisc == GSM48_PDISC_MM
240 && msgt == GSM48_MT_MM_CM_SERV_ACC) {
241 cm_service_result_sent |= RES_ACCEPT;
242 talloc_free(msg);
243 return 0;
244 }
245
246 if (pdisc == GSM48_PDISC_MM
247 && msgt == GSM48_MT_MM_CM_SERV_REJ) {
248 cm_service_result_sent |= RES_REJECT;
249 talloc_free(msg);
250 return 0;
251 }
252
253 OSMO_ASSERT(dtap_tx_expected);
254
255 /* Mask the sequence number out before comparing */
256 msg->data[1] &= 0x3f;
Neels Hofmeyre0f906c2019-10-21 06:00:35 +0200257 if (!msgb_eq_data_print(msg, dtap_tx_expected->data, dtap_tx_expected->len)) {
258 btw("Expected %s", osmo_hexdump(dtap_tx_expected->data, dtap_tx_expected->len));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100259 abort();
Neels Hofmeyre0f906c2019-10-21 06:00:35 +0200260 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100261
262 btw("DTAP matches expected message");
263
264 talloc_free(msg);
265 dtap_tx_confirmed = true;
266 talloc_free(dtap_tx_expected);
267 dtap_tx_expected = NULL;
268
269 return 0;
270}
271
272static void bssap_validate_clear_cmd()
273{
274 OSMO_ASSERT(bssap_clear_expected);
275 bssap_clear_expected = false;
276 bssap_clear_sent = true;
277}
278
279static void iucs_validate_clear_cmd()
280{
281 OSMO_ASSERT(iu_release_expected);
282 iu_release_expected = false;
283 iu_release_sent = true;
284}
285
286static int bssap_validate_cipher_mode_cmd(const struct ran_cipher_mode_command *cmd)
287{
288 int i;
289 const char *got_key;
290 cipher_mode_cmd_sent = true;
291 cipher_mode_cmd_sent_with_imeisv = cmd->geran.retrieve_imeisv;
292 btw("sending Ciphering Mode Command: retrieve_imeisv=%d", cipher_mode_cmd_sent_with_imeisv);
293 for (i = 0; i < 7; i++) {
294 if (!(cmd->geran.a5_encryption_mask & (1 << i)))
295 continue;
296 btw("...perm algo: A5/%d", i);
297 }
298 got_key = osmo_hexdump_nospc(cmd->vec->kc, sizeof(cmd->vec->kc));
299 btw("...key: %s", got_key);
300
301 if (!cipher_mode_expect_kc
302 || strcmp(cipher_mode_expect_kc, got_key)) {
303 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
304 OSMO_ASSERT(false);
305 }
306 return 0;
307}
308
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200309static void bssap_validate_assignment_cmd()
310{
311 OSMO_ASSERT(bssap_assignment_expected);
312 bssap_assignment_expected = false;
313 bssap_assignment_sent = true;
314}
315
316static void iucs_validate_assignment_cmd()
317{
318 OSMO_ASSERT(iu_rab_assignment_expected);
319 iu_rab_assignment_expected = false;
320 iu_rab_assignment_sent = true;
321}
322
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100323static int iucs_validate_security_mode_ctrl(const struct ran_cipher_mode_command *cmd)
324{
325 const char *got_ik;
326 got_ik = osmo_hexdump_nospc(cmd->vec->ik, sizeof(cmd->vec->ik));
327 btw("sending SecurityModeControl: ik=%s", got_ik);
328 security_mode_ctrl_sent = true;
329 if (!security_mode_expect_ik
330 || strcmp(security_mode_expect_ik, got_ik)) {
331 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
332 OSMO_ASSERT(false);
333 }
334 return 0;
335}
336
337struct msgb *dont_ran_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
338{
339 struct msc_role_common *c = caller_fi->priv;
340 enum osmo_rat_type ran_type = c->ran->type;
341 const char *ran_name = osmo_rat_type_name(ran_type);
342 LOG_RAN_ENC(caller_fi, DMSC, LOGL_INFO, "%s on %s\n", ran_msg_type_name(ran_enc_msg->msg_type),
343 ran_name);
344
345 switch (ran_enc_msg->msg_type) {
346 case RAN_MSG_DTAP:
347 _validate_dtap(ran_enc_msg->dtap, ran_type);
348 break;
349 case RAN_MSG_CLEAR_COMMAND:
350 switch (ran_type) {
351 case OSMO_RAT_GERAN_A:
352 bssap_validate_clear_cmd();
353 break;
354 case OSMO_RAT_UTRAN_IU:
355 iucs_validate_clear_cmd();
356 break;
357 default:
358 OSMO_ASSERT(false);
359 }
360 break;
361 case RAN_MSG_CIPHER_MODE_COMMAND:
362 switch (ran_type) {
363 case OSMO_RAT_GERAN_A:
364 bssap_validate_cipher_mode_cmd(&ran_enc_msg->cipher_mode_command);
365 break;
366 case OSMO_RAT_UTRAN_IU:
367 iucs_validate_security_mode_ctrl(&ran_enc_msg->cipher_mode_command);
368 break;
369 default:
370 OSMO_ASSERT(false);
371 }
372 break;
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200373 case RAN_MSG_ASSIGNMENT_COMMAND:
374 switch (ran_type) {
375 case OSMO_RAT_GERAN_A:
376 bssap_validate_assignment_cmd();
377 break;
378 case OSMO_RAT_UTRAN_IU:
379 iucs_validate_assignment_cmd();
380 break;
381 default:
382 OSMO_ASSERT(false);
383 }
384 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100385 default:
386 break;
387 }
388
389 /* We're testing MSC and VLR interaction, not message encoding.
390 * Return whatever. The test msc_i instance is a dummy and drops these.
391 * But it must be msg_free()-able.
392 */
393 return msgb_alloc(1, "unused dummy msg");
394}
395
396struct ran_infra test_ran_infra[] = {
397 [OSMO_RAT_GERAN_A] = {
398 .type = OSMO_RAT_GERAN_A,
399 .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_48006,
400 .log_subsys = DBSSAP,
401 .tdefs = msc_tdefs_geran,
402 .ran_encode = dont_ran_encode,
403 },
404 [OSMO_RAT_UTRAN_IU] = {
405 .type = OSMO_RAT_UTRAN_IU,
406 .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_25413,
407 .log_subsys = DIUCS,
408 .tdefs = msc_tdefs_utran,
409 .ran_encode = dont_ran_encode,
410 },
411};
412
413static int fake_msc_a_ran_dec(const struct ran_msg *ran_dec_msg)
414{
415 struct msc_a_ran_dec_data d = {
416 .from_role = MSC_ROLE_I,
417 };
418 return msc_a_ran_decode_cb(g_msub->role[MSC_ROLE_A], &d, ran_dec_msg);
419}
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100420
421void rx_from_ms(struct msgb *msg)
422{
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100423 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100424 struct ran_msg ran_dec_msg;
425 struct gsm0808_cell_id cell_id = {
426 .id_discr = CELL_IDENT_LAI_AND_LAC,
427 .id.lai_and_lac = {
428 .plmn = {
429 .mcc = 1,
430 .mnc = 2,
431 },
432 .lac = 23,
433 },
434 };
435 struct msc_a *msc_a;
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100436
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100437 log("MSC <--%s-- MS: %s", osmo_rat_type_name(rx_from_ran), gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100438
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100439 if (!conn_exists(g_msub))
440 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100441
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100442 if (!g_msub) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100443 log("new conn");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100444 g_msub = msub_alloc(net);
445 msc_a_alloc(g_msub, &test_ran_infra[rx_from_ran]);
446 dummy_msc_i_alloc(g_msub, &test_ran_infra[rx_from_ran]);
447
Harald Weltec2007852018-02-03 21:08:26 +0100448 reset_l3_seq_nr();
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100449 ran_dec_msg = (struct ran_msg){
450 .msg_type = RAN_MSG_COMPL_L3,
451 .compl_l3 = {
452 .cell_id = &cell_id,
453 .msg = msg,
454 },
455 };
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100456 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100457 ran_dec_msg = (struct ran_msg){
458 .msg_type = RAN_MSG_DTAP,
459 .dtap = msg,
460 };
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100461 }
462
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100463 msc_a = msub_msc_a(g_msub);
464 msc_a_get(msc_a, __func__);
465
466 patch_l3_seq_nr(msg);
467 fake_msc_a_ran_dec(&ran_dec_msg);
468
469 msc_a_put(msc_a, __func__);
470
471 if (!conn_exists(g_msub))
472 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100473}
474
475void ms_sends_msg(const char *hex)
476{
477 struct msgb *msg;
478
479 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
480 msg->l1h = msg->l2h = msg->l3h = msg->data;
481 rx_from_ms(msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200482 msgb_free(msg);
483}
484
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100485void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200486{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100487 struct ran_msg ran_dec = {
488 .msg_type = RAN_MSG_CLASSMARK_UPDATE,
489 .classmark_update = {
490 .classmark = classmark,
491 },
492 };
493 fake_msc_a_ran_dec(&ran_dec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100494}
495
Maxd8d1a9e2018-02-06 15:50:20 +0100496static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100497{
498 int rc;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100499 struct ran_msg ran_dec;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100500 struct msgb *msg;
501 struct gsm48_hdr *gh;
502
503 msg = msgb_alloc(1024, "ms_sends_msg_fake");
504 msg->l1h = msg->l2h = msg->l3h = msg->data;
505
506 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
507 gh->proto_discr = pdisc;
508 gh->msg_type = msg_type;
509 /* some amount of data, whatever */
510 msgb_put(msg, 123);
511
Harald Weltec2007852018-02-03 21:08:26 +0100512 patch_l3_seq_nr(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100513
514 ran_dec = (struct ran_msg){
515 .msg_type = RAN_MSG_DTAP,
516 .dtap = msg,
517 };
518 rc = fake_msc_a_ran_dec(&ran_dec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100519
520 talloc_free(msg);
521 return rc;
522}
523
Max68171902018-02-06 17:55:19 +0100524static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype)
525{
526 int rc = ms_sends_msg_fake(val, msgtype);
527 if (rc != -EACCES)
528 log("Unexpected return value %u != %u for %s/%s",
529 -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype));
530}
531
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100532void thwart_rx_non_initial_requests()
533{
534 log("requests shall be thwarted");
Max68171902018-02-06 17:55:19 +0100535
536 ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP);
537 ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */
538 ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1);
539 ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100540}
541
542void send_sms(struct vlr_subscr *receiver,
543 struct vlr_subscr *sender,
544 char *str)
545{
Harald Welte39b55482018-04-09 19:19:33 +0200546 struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str);
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700547 gsm411_send_sms(net, receiver, sms);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100548}
549
550unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100551/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
552int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
553int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100554{
Max753c15d2017-12-21 14:50:44 +0100555 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100556 for (i = 0; i < num; i++)
557 buf[i] = next_rand_byte++;
558 return 1;
559}
560
561/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
562void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
563void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
564{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200565 /* Write fixed time bytes for deterministic test results */
566 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100567}
568
569const char *paging_expecting_imsi = NULL;
570uint32_t paging_expecting_tmsi;
571bool paging_sent;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100572
573void paging_expect_imsi(const char *imsi)
574{
575 paging_expecting_imsi = imsi;
576 paging_expecting_tmsi = GSM_RESERVED_TMSI;
577}
578
579void paging_expect_tmsi(uint32_t tmsi)
580{
581 paging_expecting_tmsi = tmsi;
582 paging_expecting_imsi = NULL;
583}
584
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100585/* override, requires '-Wl,--wrap=ran_peers_down_paging' */
586int __real_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub,
587 enum paging_cause cause);
588int __wrap_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub,
589 enum paging_cause cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100590{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100591 log("paging request (%s) to %s on %s", paging_cause_name(cause), vlr_subscr_name(vsub),
592 osmo_rat_type_name(sri->ran->type));
593
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100594 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
595 if (paging_expecting_imsi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100596 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, vsub->imsi), == 0, "%d");
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200597 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100598 VERBOSE_ASSERT(paging_expecting_tmsi, == vsub->tmsi, "0x%08x");
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200599 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100600 paging_sent = true;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100601 return 1;
602}
603
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100604void clear_vlr()
605{
606 struct vlr_subscr *vsub, *n;
607 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
608 vlr_subscr_free(vsub);
609 }
610
611 net->authentication_required = false;
Harald Welte7b222aa2017-12-23 19:30:32 +0100612 net->a5_encryption_mask = (1 << 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100613 net->vlr->cfg.check_imei_rqd = false;
614 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200615 net->vlr->cfg.retrieve_imeisv_early = false;
616 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100617 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
618 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100619
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100620 rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100621 auth_request_sent = false;
622 auth_request_expect_rand = NULL;
623 auth_request_expect_autn = NULL;
624
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100625 cipher_mode_cmd_sent = false;
626 cipher_mode_cmd_sent_with_imeisv = false;
627 cipher_mode_expect_kc = NULL;
628
629 security_mode_ctrl_sent = false;
630 security_mode_expect_ck = NULL;
631 security_mode_expect_ik = NULL;
632
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100633 next_rand_byte = 0;
634
Philipp Maierfbf66102017-04-09 12:32:51 +0200635 iu_release_expected = false;
636 iu_release_sent = false;
637 bssap_clear_expected = false;
638 bssap_clear_sent = false;
639
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100640 osmo_gettimeofday_override = false;
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200641
642 expecting_crcx = -1;
643 got_crcx = false;
644
645 bssap_assignment_expected = false;
646 bssap_assignment_sent = false;
647 iu_rab_assignment_expected = false;
648 iu_rab_assignment_sent = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100649}
650
651static struct log_info_cat test_categories[] = {
652 [DMSC] = {
653 .name = "DMSC",
654 .description = "Mobile Switching Center",
655 .enabled = 1, .loglevel = LOGL_DEBUG,
656 },
657 [DRLL] = {
658 .name = "DRLL",
659 .description = "A-bis Radio Link Layer (RLL)",
660 .enabled = 1, .loglevel = LOGL_DEBUG,
661 },
662 [DMM] = {
663 .name = "DMM",
664 .description = "Layer3 Mobility Management (MM)",
665 .enabled = 1, .loglevel = LOGL_DEBUG,
666 },
667 [DRR] = {
668 .name = "DRR",
669 .description = "Layer3 Radio Resource (RR)",
670 .enabled = 1, .loglevel = LOGL_DEBUG,
671 },
672 [DCC] = {
673 .name = "DCC",
674 .description = "Layer3 Call Control (CC)",
Neels Hofmeyr12e17be2018-03-12 23:59:37 +0100675 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100676 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100677 [DVLR] = {
678 .name = "DVLR",
679 .description = "Visitor Location Register",
680 .enabled = 1, .loglevel = LOGL_DEBUG,
681 },
682 [DREF] = {
683 .name = "DREF",
684 .description = "Reference Counting",
685 .enabled = 1, .loglevel = LOGL_DEBUG,
686 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200687 [DPAG] = {
688 .name = "DPAG",
689 .description = "Paging Subsystem",
690 .enabled = 1, .loglevel = LOGL_DEBUG,
691 },
692 [DIUCS] = {
693 .name = "DIUCS",
694 .description = "Iu-CS Protocol",
695 .enabled = 1, .loglevel = LOGL_DEBUG,
696 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100697 [DMNCC] = {
698 .name = "DMNCC",
699 .description = "MNCC API for Call Control application",
700 .enabled = 1, .loglevel = LOGL_DEBUG,
701 },
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100702 [DBSSAP] = {
703 .name = "DBSSAP",
704 .description = "BSSAP Protocol (A Interface)",
705 .enabled = 1, .loglevel = LOGL_DEBUG,
706 },
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200707 [DSS] = {
708 .name = "DSS",
709 .description = "Supplementary Services",
710 .enabled = 1, .loglevel = LOGL_DEBUG,
711 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100712};
713
714static struct log_info info = {
715 .cat = test_categories,
716 .num_cat = ARRAY_SIZE(test_categories),
717};
718
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100719struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
720
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100721int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100722{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100723 struct gsm_mncc *mncc = (void*)msg->data;
724 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
725 get_mncc_name(mncc->msg_type));
726
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100727 if (mncc->msg_type == MNCC_REL_IND && on_call_release_mncc_sends_to_cc_data) {
728
729 log("MNCC: callref 0x%x: Call Release triggering %s", mncc->callref,
730 get_mncc_name(on_call_release_mncc_sends_to_cc_data->msg_type));
731
Neels Hofmeyr52558742019-05-09 01:23:09 +0200732 mncc_tx_to_cc(net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100733 on_call_release_mncc_sends_to_cc_data);
734
735 on_call_release_mncc_sends_to_cc_data = NULL;
736 return 0;
737 }
738
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100739 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
740 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
741 log("Mismatch! Expected MNCC msg type: %s",
742 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
743 abort();
744 }
745
746 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
747 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
748 cc_to_mncc_tx_expected_imsi,
749 mncc->imsi);
750 abort();
751 }
752
753 cc_to_mncc_tx_confirmed = true;
754 cc_to_mncc_tx_got_callref = mncc->callref;
755 cc_to_mncc_tx_expected_imsi = NULL;
756 cc_to_mncc_tx_expected_msg_type = 0;
757 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100758 return 0;
759}
760
Harald Welte1ea6baf2018-07-31 19:40:52 +0200761struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100762__real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
763 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
764 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200765struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100766__wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
767 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
768 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100769{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200770 struct osmo_gsup_client *gsupc;
771 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100772 OSMO_ASSERT(gsupc);
773 return gsupc;
774}
775
776/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200777int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
778int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100779{
Max34d306b2019-01-15 18:37:31 +0100780 uint8_t buf[512];
781 int len;
782
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100783 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
Max34d306b2019-01-15 18:37:31 +0100784 osmo_gsup_message_type_name(msg->data[0]), osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100785
786 OSMO_ASSERT(gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100787 OSMO_ASSERT(strlen(gsup_tx_expected) <= (sizeof(buf) * 2));
788
789 len = osmo_hexparse(gsup_tx_expected, buf, sizeof(buf));
790 if (len < 1)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100791 abort();
Max34d306b2019-01-15 18:37:31 +0100792
Neels Hofmeyrbe5f0332018-12-26 01:11:57 +0100793 /* Compare only the length expected. Extra data is fine, to not care about new GSUP IEs invented later. */
794 if (msg->len < len) {
795 fprintf(stderr, "ERROR: GSUP message too short, expected '%s'\n", gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100796 abort();
Neels Hofmeyrbe5f0332018-12-26 01:11:57 +0100797 }
798
799 if (memcmp(msg->data, buf, len)) {
800 fprintf(stderr, "ERROR: GSUP message mismatch, expected it to start with '%s'\n", gsup_tx_expected);
801 abort();
802 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100803
804 talloc_free(msg);
805 gsup_tx_confirmed = true;
806 gsup_tx_expected = NULL;
807 return 0;
808}
809
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200810struct rtp_stream fake_rtp[2] = {
811 {
812 .dir = RTP_TO_RAN,
813 .local = {
814 .ip = "10.23.42.1",
815 .port = 99,
816 },
817 .remote = {
818 .ip = "10.23.42.2",
819 .port = 100,
820 },
821 },
822 {
823 .dir = RTP_TO_CN,
824 .local = {
825 .ip = "10.23.42.1",
826 .port = 23,
827 },
828 .remote = {
829 .ip = "10.23.42.2",
830 .port = 42,
831 },
832 },
833};
834
835void expect_crcx(enum rtp_direction towards)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100836{
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200837 OSMO_ASSERT(expecting_crcx == -1);
838 expecting_crcx = towards;
839 got_crcx = false;
840}
841
842/* override, requires '-Wl,--wrap=call_leg_ensure_ci' */
843int __real_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
844 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known);
845int __wrap_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
846 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known)
847{
848 if (!cl->rtp[dir]) {
849 log("MGW <--CRCX to %s-- MSC: callref=0x%x", rtp_direction_name(dir), call_id);
850
851 OSMO_ASSERT(expecting_crcx == dir);
852 expecting_crcx = -1;
853 got_crcx = true;
854
855 call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans);
856 if (codec_if_known)
857 rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
858 if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
859 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
860 }
861
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100862 return 0;
863}
864
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200865void crcx_ok(enum rtp_direction dir)
866{
867 struct msc_a *msc_a = msub_msc_a(g_msub);
868 struct call_leg *cl = msc_a->cc.call_leg;
869 OSMO_ASSERT(cl);
870 OSMO_ASSERT(cl->rtp[dir]);
871 osmo_sockaddr_str_from_str(&cl->rtp[dir]->local, "10.23.23.1", 23);
872 //osmo_sockaddr_str_from_str(&cl->rtp[dir].remote, "10.42.42.1", 42);
873 log("MGW --CRCX OK to %s--> MSC", rtp_direction_name(dir));
874 osmo_fsm_inst_dispatch(cl->fi, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, cl->rtp[dir]);
875}
876
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100877static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
878{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100879 struct msc_a *msc_a = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100880 if (send_tmsi == GSM_RESERVED_TMSI)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100881 btw("sending LU Accept for %s", msc_a->c.fi->id);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100882 else
883 btw("sending LU Accept for %s, with TMSI 0x%08x",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100884 msc_a->c.fi->id, send_tmsi);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100885 lu_result_sent |= RES_ACCEPT;
886 return 0;
887}
888
Neels Hofmeyr15809592018-04-06 02:57:51 +0200889static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100890{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100891 struct msc_a *msc_a = msc_conn_ref;
892 btw("sending LU Reject for %s, cause %u", msc_a->c.fi->id, cause);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100893 lu_result_sent |= RES_REJECT;
894 return 0;
895}
896
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100897static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
898 enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100899{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100900 struct msc_a *msc_a = msc_conn_ref;
901 btw("sending CM Service Reject (%s) for %s, cause: %s",
902 osmo_cm_service_type_name(cm_service_type), msc_a->c.fi->id, gsm48_reject_value_name(cause));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100903 cm_service_result_sent |= RES_REJECT;
904 return 0;
905}
906
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100907static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100908 bool send_autn)
909{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100910 struct msc_a *msc_a = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100911 char *hex;
912 bool ok = true;
913 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100914 send_autn? "UMTS" : "GSM", msc_a->c.fi->id,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100915 at->use_count, at->key_seq, at->vec.auth_types);
916
917 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
918 btw("...rand=%s", hex);
919 if (!auth_request_expect_rand
920 || strcmp(hex, auth_request_expect_rand) != 0) {
921 ok = false;
922 log("FAILURE: expected rand=%s",
923 auth_request_expect_rand ? auth_request_expect_rand : "-");
924 }
925
926 if (send_autn) {
927 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
928 btw("...autn=%s", hex);
929 if (!auth_request_expect_autn
930 || strcmp(hex, auth_request_expect_autn) != 0) {
931 ok = false;
932 log("FAILURE: expected autn=%s",
933 auth_request_expect_autn ? auth_request_expect_autn : "-");
934 }
935 } else if (auth_request_expect_autn) {
936 ok = false;
937 log("FAILURE: no AUTN sent, expected AUTN = %s",
938 auth_request_expect_autn);
939 }
940
941 if (send_autn)
942 btw("...expecting res=%s",
943 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
944 else
945 btw("...expecting sres=%s",
946 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
947
948 auth_request_sent = ok;
949 return 0;
950}
951
952static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
953{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100954 struct msc_a *msc_a = msc_conn_ref;
955 btw("sending Auth Reject for %s", msc_a->c.fi->id);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100956 return 0;
957}
958
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100959void ms_sends_ciphering_mode_complete(const char *inner_ran_msg)
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100960{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100961 struct ran_msg ran_dec;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100962
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100963 msc_a_get(msub_msc_a(g_msub), __func__);
964
965 ran_dec = (struct ran_msg){
966 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
967 };
968 fake_msc_a_ran_dec(&ran_dec);
969
970 if (inner_ran_msg) {
971 struct msgb *msg = msgb_from_hex("cipher_mode_complete_ran", 1024, inner_ran_msg);
972 msg->l1h = msg->l2h = msg->l3h = msg->data;
973 ran_dec = (struct ran_msg){
974 .msg_type = RAN_MSG_DTAP,
975 .dtap = msg,
976 };
977 patch_l3_seq_nr(msg);
978 fake_msc_a_ran_dec(&ran_dec);
979 msgb_free(msg);
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100980 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100981
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100982 msc_a_put(msub_msc_a(g_msub), __func__);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100983
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100984 if (!conn_exists(g_msub))
985 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100986}
987
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200988void ms_sends_security_mode_complete()
989{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100990 struct ran_msg ran_dec;
991
992 ran_dec = (struct ran_msg){
993 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
994 };
995 fake_msc_a_ran_dec(&ran_dec);
996
997 if (!conn_exists(g_msub))
998 g_msub = NULL;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200999}
1000
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +02001001void ms_sends_assignment_complete(enum mgcp_codecs assigned_codec)
1002{
1003 struct ran_msg ran_dec;
1004
1005 ran_dec = (struct ran_msg){
1006 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
1007 .assignment_complete = {
1008 .codec = assigned_codec,
1009 },
1010 };
1011 fake_msc_a_ran_dec(&ran_dec);
1012
1013 if (!conn_exists(g_msub))
1014 g_msub = NULL;
1015}
1016
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001017void ran_sends_clear_complete()
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001018{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001019 struct ran_msg ran_dec;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001020
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001021 ran_dec = (struct ran_msg){
1022 .msg_type = RAN_MSG_CLEAR_COMPLETE,
1023 };
1024 fake_msc_a_ran_dec(&ran_dec);
1025
1026 if (!conn_exists(g_msub))
1027 g_msub = NULL;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001028}
1029
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001030const struct timeval fake_time_start_time = { 123, 456 };
1031
1032void fake_time_start()
1033{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001034 struct timespec *clock_override;
1035
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001036 osmo_gettimeofday_override_time = fake_time_start_time;
1037 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001038 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
1039 OSMO_ASSERT(clock_override);
1040 clock_override->tv_sec = fake_time_start_time.tv_sec;
1041 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
1042 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001043 fake_time_passes(0, 0);
1044}
1045
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001046static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001047{
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001048 /* Verifying that the msgb context is empty */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001049 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001050 /* Expecting these to stick around in msc_vlr_tests_ctx:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001051 * full talloc report on 'msgb' (total 0 bytes in 1 blocks)
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001052 * talloc_total_blocks(tall_bsc_ctx) == 20
1053 * full talloc report on 'msc_vlr_tests_ctx' (total 6556 bytes in 20 blocks)
1054 * struct osmo_gsup_client contains 264 bytes in 1 blocks (ref 0) 0x613000000260
1055 * struct gsm_network contains 4791 bytes in 12 blocks (ref 0) 0x6190000000e0
1056 * struct mgcp_client contains 688 bytes in 1 blocks (ref 0) 0x6180000000e0
1057 * struct sccp_ran_inst contains 152 bytes in 1 blocks (ref 0) 0x611000000320
1058 * struct sccp_ran_inst contains 152 bytes in 1 blocks (ref 0) 0x6110000001e0
1059 * struct gsup_client_mux contains 152 bytes in 2 blocks (ref 0) 0x6100000001a0
1060 * struct ipaccess_unit contains 64 bytes in 1 blocks (ref 0) 0x60e000023180
1061 * struct vlr_instance contains 264 bytes in 2 blocks (ref 0) 0x6130000000a0
1062 * struct osmo_nri_ranges contains 16 bytes in 1 blocks (ref 0) 0x60b000000360
1063 * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x60b0000002b0
1064 * ../../../src/libosmocore/src/stat_item.c:96 contains 144 bytes in 2 blocks (ref 0) 0x60d000000170
1065 * ../../../src/libosmocore/src/stat_item.c:118 contains 96 bytes in 1 blocks (ref 0) 0x6100000000a0
1066 * ../../../src/libosmocore/src/rate_ctr.c:234 contains 2352 bytes in 1 blocks (ref 0) 0x61e0000000e0
1067 * logging contains 1501 bytes in 5 blocks (ref 0) 0x60b000000200
1068 * struct log_target contains 244 bytes in 2 blocks (ref 0) 0x6120000000a0
1069 * struct log_category contains 76 bytes in 1 blocks (ref 0) 0x60f0000000a0
1070 * struct log_info contains 1256 bytes in 2 blocks (ref 0) 0x60d0000000a0
1071 * struct log_info_cat contains 1216 bytes in 1 blocks (ref 0) 0x61a0000000e0
1072 * msgb contains 0 bytes in 1 blocks (ref 0) 0x608000000180
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001073 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001074 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001075 talloc_total_blocks(msc_vlr_tests_ctx));
Neels Hofmeyr9aac5c22020-05-27 00:04:26 +02001076 if (talloc_total_blocks(msc_vlr_tests_ctx) != 20)
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001077 talloc_report_full(msc_vlr_tests_ctx, stderr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001078 fprintf(stderr, "\n");
1079}
1080
1081static struct {
1082 bool verbose;
1083 int run_test_nr;
1084} cmdline_opts = {
1085 .verbose = false,
1086 .run_test_nr = -1,
1087};
1088
1089static void print_help(const char *program)
1090{
1091 printf("Usage:\n"
1092 " %s [-v] [N [N...]]\n"
1093 "Options:\n"
1094 " -h --help show this text.\n"
1095 " -v --verbose print source file and line numbers\n"
1096 " N run only the Nth test (first test is N=1)\n",
1097 program
1098 );
1099}
1100
1101static void handle_options(int argc, char **argv)
1102{
1103 while (1) {
1104 int option_index = 0, c;
1105 static struct option long_options[] = {
1106 {"help", 0, 0, 'h'},
1107 {"verbose", 1, 0, 'v'},
1108 {0, 0, 0, 0}
1109 };
1110
1111 c = getopt_long(argc, argv, "hv",
1112 long_options, &option_index);
1113 if (c == -1)
1114 break;
1115
1116 switch (c) {
1117 case 'h':
1118 print_help(argv[0]);
1119 exit(0);
1120 case 'v':
1121 cmdline_opts.verbose = true;
1122 break;
1123 default:
1124 /* catch unknown options *as well as* missing arguments. */
1125 fprintf(stderr, "Error in command line options. Exiting.\n");
1126 exit(-1);
1127 break;
1128 }
1129 }
1130}
1131
1132void *msgb_ctx = NULL;
1133
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001134static void run_tests(int nr)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001135{
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001136 int test_nr;
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001137
1138 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
1139
Max5e2e9bd2018-02-06 19:31:08 +01001140 nr--; /* arg's first test is 1, in here it's 0 */
1141 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001142 if (nr >= 0 && test_nr != nr)
1143 continue;
1144
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001145 if (cmdline_opts.verbose)
1146 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
1147
1148 msc_vlr_tests[test_nr]();
1149
1150 if (cmdline_opts.verbose)
1151 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001152
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001153 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Max29ce08a2018-02-06 18:46:57 +01001154 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001155}
1156
Max5e60de62018-02-07 12:56:09 +01001157struct gsm_network *test_net(void *ctx)
1158{
Neels Hofmeyr7f484202018-02-27 12:59:45 +01001159 struct gsm_network *net = gsm_network_init(ctx, mncc_recv);
Max5e60de62018-02-07 12:56:09 +01001160
1161 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
1162 net->gsup_server_port = 0;
1163
1164 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
Max5e60de62018-02-07 12:56:09 +01001165 OSMO_ASSERT(net->vlr);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001166 OSMO_ASSERT(msc_gsup_client_start(net) == 0);
1167 OSMO_ASSERT(net->gcm);
1168 OSMO_ASSERT(msc_vlr_start(net) == 0);
Max5e60de62018-02-07 12:56:09 +01001169
1170 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
1171 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001172 net->vlr->ops.tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc;
Max5e60de62018-02-07 12:56:09 +01001173 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
1174 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
1175 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001176 net->vlr->ops.set_ciph_mode = msc_a_vlr_set_cipher_mode;
1177
1178 /* Allocate fake SCCP Ran Instances */
1179 net->a.sri = talloc_zero(net, struct sccp_ran_inst);
1180 *net->a.sri = (struct sccp_ran_inst){
1181 .ran = &test_ran_infra[OSMO_RAT_GERAN_A],
1182 };
1183 INIT_LLIST_HEAD(&net->a.sri->ran_peers);
1184 INIT_LLIST_HEAD(&net->a.sri->ran_conns);
1185
1186 net->iu.sri = talloc_zero(net, struct sccp_ran_inst);
1187 *net->iu.sri = (struct sccp_ran_inst){
1188 .ran = &test_ran_infra[OSMO_RAT_UTRAN_IU],
1189 };
1190 INIT_LLIST_HEAD(&net->iu.sri->ran_peers);
1191 INIT_LLIST_HEAD(&net->iu.sri->ran_conns);
1192
1193 net->mgw.tdefs = g_mgw_tdefs;
1194 mgcp_client_conf_init(&net->mgw.conf);
1195 net->mgw.tdefs = g_mgw_tdefs;
1196 net->mgw.client = mgcp_client_init(net, &net->mgw.conf);
Max5e60de62018-02-07 12:56:09 +01001197
1198 return net;
1199}
1200
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001201int main(int argc, char **argv)
1202{
1203 handle_options(argc, argv);
1204
Neels Hofmeyre4f7e712019-03-24 21:07:33 +01001205 osmo_fsm_term_safely(true);
1206
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001207 msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx");
1208 msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0);
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001209 osmo_init_logging2(msc_vlr_tests_ctx, &info);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001210
1211 _log_lines = cmdline_opts.verbose;
1212
1213 OSMO_ASSERT(osmo_stderr_target);
1214 log_set_use_color(osmo_stderr_target, 0);
1215 log_set_print_timestamp(osmo_stderr_target, 0);
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001216 log_set_print_filename(osmo_stderr_target, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001217 log_set_print_category(osmo_stderr_target, 1);
Max48131522019-01-16 12:47:39 +01001218 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001219
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001220 if (cmdline_opts.verbose) {
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001221 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
1222 log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
1223 log_set_use_color(osmo_stderr_target, 1);
1224 log_set_print_level(osmo_stderr_target, 1);
1225 }
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +01001226
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001227 net = test_net(msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001228
1229 osmo_fsm_log_addr(false);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001230 osmo_fsm_log_timeouts(cmdline_opts.verbose);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001231
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001232 call_leg_init(net);
1233
1234 OSMO_ASSERT(osmo_fsm_register(&dummy_msc_i_fsm) == 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001235
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001236 clear_vlr();
1237
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001238 if (optind >= argc)
1239 run_tests(-1);
1240 else {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001241 int arg;
1242 long int nr;
1243 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +01001244 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001245 nr = strtol(argv[arg], NULL, 10);
1246 if (errno) {
1247 fprintf(stderr, "Invalid argument: %s\n",
1248 argv[arg]);
1249 exit(1);
1250 }
1251
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001252 run_tests(nr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001253 }
1254 }
1255
1256 printf("Done\n");
1257
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001258 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001259 return 0;
1260}