blob: de4913a87fd462c7e15111e56a2637c1883aecf8 [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;
257 if (!msgb_eq_data_print(msg, dtap_tx_expected->data, dtap_tx_expected->len))
258 abort();
259
260 btw("DTAP matches expected message");
261
262 talloc_free(msg);
263 dtap_tx_confirmed = true;
264 talloc_free(dtap_tx_expected);
265 dtap_tx_expected = NULL;
266
267 return 0;
268}
269
270static void bssap_validate_clear_cmd()
271{
272 OSMO_ASSERT(bssap_clear_expected);
273 bssap_clear_expected = false;
274 bssap_clear_sent = true;
275}
276
277static void iucs_validate_clear_cmd()
278{
279 OSMO_ASSERT(iu_release_expected);
280 iu_release_expected = false;
281 iu_release_sent = true;
282}
283
284static int bssap_validate_cipher_mode_cmd(const struct ran_cipher_mode_command *cmd)
285{
286 int i;
287 const char *got_key;
288 cipher_mode_cmd_sent = true;
289 cipher_mode_cmd_sent_with_imeisv = cmd->geran.retrieve_imeisv;
290 btw("sending Ciphering Mode Command: retrieve_imeisv=%d", cipher_mode_cmd_sent_with_imeisv);
291 for (i = 0; i < 7; i++) {
292 if (!(cmd->geran.a5_encryption_mask & (1 << i)))
293 continue;
294 btw("...perm algo: A5/%d", i);
295 }
296 got_key = osmo_hexdump_nospc(cmd->vec->kc, sizeof(cmd->vec->kc));
297 btw("...key: %s", got_key);
298
299 if (!cipher_mode_expect_kc
300 || strcmp(cipher_mode_expect_kc, got_key)) {
301 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
302 OSMO_ASSERT(false);
303 }
304 return 0;
305}
306
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200307static void bssap_validate_assignment_cmd()
308{
309 OSMO_ASSERT(bssap_assignment_expected);
310 bssap_assignment_expected = false;
311 bssap_assignment_sent = true;
312}
313
314static void iucs_validate_assignment_cmd()
315{
316 OSMO_ASSERT(iu_rab_assignment_expected);
317 iu_rab_assignment_expected = false;
318 iu_rab_assignment_sent = true;
319}
320
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100321static int iucs_validate_security_mode_ctrl(const struct ran_cipher_mode_command *cmd)
322{
323 const char *got_ik;
324 got_ik = osmo_hexdump_nospc(cmd->vec->ik, sizeof(cmd->vec->ik));
325 btw("sending SecurityModeControl: ik=%s", got_ik);
326 security_mode_ctrl_sent = true;
327 if (!security_mode_expect_ik
328 || strcmp(security_mode_expect_ik, got_ik)) {
329 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
330 OSMO_ASSERT(false);
331 }
332 return 0;
333}
334
335struct msgb *dont_ran_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
336{
337 struct msc_role_common *c = caller_fi->priv;
338 enum osmo_rat_type ran_type = c->ran->type;
339 const char *ran_name = osmo_rat_type_name(ran_type);
340 LOG_RAN_ENC(caller_fi, DMSC, LOGL_INFO, "%s on %s\n", ran_msg_type_name(ran_enc_msg->msg_type),
341 ran_name);
342
343 switch (ran_enc_msg->msg_type) {
344 case RAN_MSG_DTAP:
345 _validate_dtap(ran_enc_msg->dtap, ran_type);
346 break;
347 case RAN_MSG_CLEAR_COMMAND:
348 switch (ran_type) {
349 case OSMO_RAT_GERAN_A:
350 bssap_validate_clear_cmd();
351 break;
352 case OSMO_RAT_UTRAN_IU:
353 iucs_validate_clear_cmd();
354 break;
355 default:
356 OSMO_ASSERT(false);
357 }
358 break;
359 case RAN_MSG_CIPHER_MODE_COMMAND:
360 switch (ran_type) {
361 case OSMO_RAT_GERAN_A:
362 bssap_validate_cipher_mode_cmd(&ran_enc_msg->cipher_mode_command);
363 break;
364 case OSMO_RAT_UTRAN_IU:
365 iucs_validate_security_mode_ctrl(&ran_enc_msg->cipher_mode_command);
366 break;
367 default:
368 OSMO_ASSERT(false);
369 }
370 break;
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200371 case RAN_MSG_ASSIGNMENT_COMMAND:
372 switch (ran_type) {
373 case OSMO_RAT_GERAN_A:
374 bssap_validate_assignment_cmd();
375 break;
376 case OSMO_RAT_UTRAN_IU:
377 iucs_validate_assignment_cmd();
378 break;
379 default:
380 OSMO_ASSERT(false);
381 }
382 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100383 default:
384 break;
385 }
386
387 /* We're testing MSC and VLR interaction, not message encoding.
388 * Return whatever. The test msc_i instance is a dummy and drops these.
389 * But it must be msg_free()-able.
390 */
391 return msgb_alloc(1, "unused dummy msg");
392}
393
394struct ran_infra test_ran_infra[] = {
395 [OSMO_RAT_GERAN_A] = {
396 .type = OSMO_RAT_GERAN_A,
397 .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_48006,
398 .log_subsys = DBSSAP,
399 .tdefs = msc_tdefs_geran,
400 .ran_encode = dont_ran_encode,
401 },
402 [OSMO_RAT_UTRAN_IU] = {
403 .type = OSMO_RAT_UTRAN_IU,
404 .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_25413,
405 .log_subsys = DIUCS,
406 .tdefs = msc_tdefs_utran,
407 .ran_encode = dont_ran_encode,
408 },
409};
410
411static int fake_msc_a_ran_dec(const struct ran_msg *ran_dec_msg)
412{
413 struct msc_a_ran_dec_data d = {
414 .from_role = MSC_ROLE_I,
415 };
416 return msc_a_ran_decode_cb(g_msub->role[MSC_ROLE_A], &d, ran_dec_msg);
417}
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100418
419void rx_from_ms(struct msgb *msg)
420{
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100421 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100422 struct ran_msg ran_dec_msg;
423 struct gsm0808_cell_id cell_id = {
424 .id_discr = CELL_IDENT_LAI_AND_LAC,
425 .id.lai_and_lac = {
426 .plmn = {
427 .mcc = 1,
428 .mnc = 2,
429 },
430 .lac = 23,
431 },
432 };
433 struct msc_a *msc_a;
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100434
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100435 log("MSC <--%s-- MS: %s", osmo_rat_type_name(rx_from_ran), gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100436
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100437 if (!conn_exists(g_msub))
438 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100439
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100440 if (!g_msub) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100441 log("new conn");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100442 g_msub = msub_alloc(net);
443 msc_a_alloc(g_msub, &test_ran_infra[rx_from_ran]);
444 dummy_msc_i_alloc(g_msub, &test_ran_infra[rx_from_ran]);
445
Harald Weltec2007852018-02-03 21:08:26 +0100446 reset_l3_seq_nr();
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100447 ran_dec_msg = (struct ran_msg){
448 .msg_type = RAN_MSG_COMPL_L3,
449 .compl_l3 = {
450 .cell_id = &cell_id,
451 .msg = msg,
452 },
453 };
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100454 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100455 ran_dec_msg = (struct ran_msg){
456 .msg_type = RAN_MSG_DTAP,
457 .dtap = msg,
458 };
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100459 }
460
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100461 msc_a = msub_msc_a(g_msub);
462 msc_a_get(msc_a, __func__);
463
464 patch_l3_seq_nr(msg);
465 fake_msc_a_ran_dec(&ran_dec_msg);
466
467 msc_a_put(msc_a, __func__);
468
469 if (!conn_exists(g_msub))
470 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100471}
472
473void ms_sends_msg(const char *hex)
474{
475 struct msgb *msg;
476
477 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
478 msg->l1h = msg->l2h = msg->l3h = msg->data;
479 rx_from_ms(msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200480 msgb_free(msg);
481}
482
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100483void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200484{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100485 struct ran_msg ran_dec = {
486 .msg_type = RAN_MSG_CLASSMARK_UPDATE,
487 .classmark_update = {
488 .classmark = classmark,
489 },
490 };
491 fake_msc_a_ran_dec(&ran_dec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100492}
493
Maxd8d1a9e2018-02-06 15:50:20 +0100494static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100495{
496 int rc;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100497 struct ran_msg ran_dec;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100498 struct msgb *msg;
499 struct gsm48_hdr *gh;
500
501 msg = msgb_alloc(1024, "ms_sends_msg_fake");
502 msg->l1h = msg->l2h = msg->l3h = msg->data;
503
504 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
505 gh->proto_discr = pdisc;
506 gh->msg_type = msg_type;
507 /* some amount of data, whatever */
508 msgb_put(msg, 123);
509
Harald Weltec2007852018-02-03 21:08:26 +0100510 patch_l3_seq_nr(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100511
512 ran_dec = (struct ran_msg){
513 .msg_type = RAN_MSG_DTAP,
514 .dtap = msg,
515 };
516 rc = fake_msc_a_ran_dec(&ran_dec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100517
518 talloc_free(msg);
519 return rc;
520}
521
Max68171902018-02-06 17:55:19 +0100522static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype)
523{
524 int rc = ms_sends_msg_fake(val, msgtype);
525 if (rc != -EACCES)
526 log("Unexpected return value %u != %u for %s/%s",
527 -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype));
528}
529
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100530void thwart_rx_non_initial_requests()
531{
532 log("requests shall be thwarted");
Max68171902018-02-06 17:55:19 +0100533
534 ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP);
535 ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */
536 ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1);
537 ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100538}
539
540void send_sms(struct vlr_subscr *receiver,
541 struct vlr_subscr *sender,
542 char *str)
543{
Harald Welte39b55482018-04-09 19:19:33 +0200544 struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str);
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700545 gsm411_send_sms(net, receiver, sms);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100546}
547
548unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100549/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
550int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
551int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100552{
Max753c15d2017-12-21 14:50:44 +0100553 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100554 for (i = 0; i < num; i++)
555 buf[i] = next_rand_byte++;
556 return 1;
557}
558
559/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
560void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
561void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
562{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200563 /* Write fixed time bytes for deterministic test results */
564 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100565}
566
567const char *paging_expecting_imsi = NULL;
568uint32_t paging_expecting_tmsi;
569bool paging_sent;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100570
571void paging_expect_imsi(const char *imsi)
572{
573 paging_expecting_imsi = imsi;
574 paging_expecting_tmsi = GSM_RESERVED_TMSI;
575}
576
577void paging_expect_tmsi(uint32_t tmsi)
578{
579 paging_expecting_tmsi = tmsi;
580 paging_expecting_imsi = NULL;
581}
582
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100583/* override, requires '-Wl,--wrap=ran_peers_down_paging' */
584int __real_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub,
585 enum paging_cause cause);
586int __wrap_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub,
587 enum paging_cause cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100588{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100589 log("paging request (%s) to %s on %s", paging_cause_name(cause), vlr_subscr_name(vsub),
590 osmo_rat_type_name(sri->ran->type));
591
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100592 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
593 if (paging_expecting_imsi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100594 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, vsub->imsi), == 0, "%d");
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200595 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100596 VERBOSE_ASSERT(paging_expecting_tmsi, == vsub->tmsi, "0x%08x");
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200597 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100598 paging_sent = true;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100599 return 1;
600}
601
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100602void clear_vlr()
603{
604 struct vlr_subscr *vsub, *n;
605 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
606 vlr_subscr_free(vsub);
607 }
608
609 net->authentication_required = false;
Harald Welte7b222aa2017-12-23 19:30:32 +0100610 net->a5_encryption_mask = (1 << 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100611 net->vlr->cfg.check_imei_rqd = false;
612 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200613 net->vlr->cfg.retrieve_imeisv_early = false;
614 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100615 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
616 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100617
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100618 rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100619 auth_request_sent = false;
620 auth_request_expect_rand = NULL;
621 auth_request_expect_autn = NULL;
622
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100623 cipher_mode_cmd_sent = false;
624 cipher_mode_cmd_sent_with_imeisv = false;
625 cipher_mode_expect_kc = NULL;
626
627 security_mode_ctrl_sent = false;
628 security_mode_expect_ck = NULL;
629 security_mode_expect_ik = NULL;
630
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100631 next_rand_byte = 0;
632
Philipp Maierfbf66102017-04-09 12:32:51 +0200633 iu_release_expected = false;
634 iu_release_sent = false;
635 bssap_clear_expected = false;
636 bssap_clear_sent = false;
637
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100638 osmo_gettimeofday_override = false;
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200639
640 expecting_crcx = -1;
641 got_crcx = false;
642
643 bssap_assignment_expected = false;
644 bssap_assignment_sent = false;
645 iu_rab_assignment_expected = false;
646 iu_rab_assignment_sent = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100647}
648
649static struct log_info_cat test_categories[] = {
650 [DMSC] = {
651 .name = "DMSC",
652 .description = "Mobile Switching Center",
653 .enabled = 1, .loglevel = LOGL_DEBUG,
654 },
655 [DRLL] = {
656 .name = "DRLL",
657 .description = "A-bis Radio Link Layer (RLL)",
658 .enabled = 1, .loglevel = LOGL_DEBUG,
659 },
660 [DMM] = {
661 .name = "DMM",
662 .description = "Layer3 Mobility Management (MM)",
663 .enabled = 1, .loglevel = LOGL_DEBUG,
664 },
665 [DRR] = {
666 .name = "DRR",
667 .description = "Layer3 Radio Resource (RR)",
668 .enabled = 1, .loglevel = LOGL_DEBUG,
669 },
670 [DCC] = {
671 .name = "DCC",
672 .description = "Layer3 Call Control (CC)",
Neels Hofmeyr12e17be2018-03-12 23:59:37 +0100673 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100674 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100675 [DVLR] = {
676 .name = "DVLR",
677 .description = "Visitor Location Register",
678 .enabled = 1, .loglevel = LOGL_DEBUG,
679 },
680 [DREF] = {
681 .name = "DREF",
682 .description = "Reference Counting",
683 .enabled = 1, .loglevel = LOGL_DEBUG,
684 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200685 [DPAG] = {
686 .name = "DPAG",
687 .description = "Paging Subsystem",
688 .enabled = 1, .loglevel = LOGL_DEBUG,
689 },
690 [DIUCS] = {
691 .name = "DIUCS",
692 .description = "Iu-CS Protocol",
693 .enabled = 1, .loglevel = LOGL_DEBUG,
694 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100695 [DMNCC] = {
696 .name = "DMNCC",
697 .description = "MNCC API for Call Control application",
698 .enabled = 1, .loglevel = LOGL_DEBUG,
699 },
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100700 [DBSSAP] = {
701 .name = "DBSSAP",
702 .description = "BSSAP Protocol (A Interface)",
703 .enabled = 1, .loglevel = LOGL_DEBUG,
704 },
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200705 [DSS] = {
706 .name = "DSS",
707 .description = "Supplementary Services",
708 .enabled = 1, .loglevel = LOGL_DEBUG,
709 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100710};
711
712static struct log_info info = {
713 .cat = test_categories,
714 .num_cat = ARRAY_SIZE(test_categories),
715};
716
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100717struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
718
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100719int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100720{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100721 struct gsm_mncc *mncc = (void*)msg->data;
722 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
723 get_mncc_name(mncc->msg_type));
724
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100725 if (mncc->msg_type == MNCC_REL_IND && on_call_release_mncc_sends_to_cc_data) {
726
727 log("MNCC: callref 0x%x: Call Release triggering %s", mncc->callref,
728 get_mncc_name(on_call_release_mncc_sends_to_cc_data->msg_type));
729
Neels Hofmeyr52558742019-05-09 01:23:09 +0200730 mncc_tx_to_cc(net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100731 on_call_release_mncc_sends_to_cc_data);
732
733 on_call_release_mncc_sends_to_cc_data = NULL;
734 return 0;
735 }
736
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100737 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
738 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
739 log("Mismatch! Expected MNCC msg type: %s",
740 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
741 abort();
742 }
743
744 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
745 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
746 cc_to_mncc_tx_expected_imsi,
747 mncc->imsi);
748 abort();
749 }
750
751 cc_to_mncc_tx_confirmed = true;
752 cc_to_mncc_tx_got_callref = mncc->callref;
753 cc_to_mncc_tx_expected_imsi = NULL;
754 cc_to_mncc_tx_expected_msg_type = 0;
755 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100756 return 0;
757}
758
Harald Welte1ea6baf2018-07-31 19:40:52 +0200759struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100760__real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
761 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
762 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200763struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100764__wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
765 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
766 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100767{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200768 struct osmo_gsup_client *gsupc;
769 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100770 OSMO_ASSERT(gsupc);
771 return gsupc;
772}
773
774/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200775int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
776int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100777{
Max34d306b2019-01-15 18:37:31 +0100778 uint8_t buf[512];
779 int len;
780
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100781 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
Max34d306b2019-01-15 18:37:31 +0100782 osmo_gsup_message_type_name(msg->data[0]), osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100783
784 OSMO_ASSERT(gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100785 OSMO_ASSERT(strlen(gsup_tx_expected) <= (sizeof(buf) * 2));
786
787 len = osmo_hexparse(gsup_tx_expected, buf, sizeof(buf));
788 if (len < 1)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100789 abort();
Max34d306b2019-01-15 18:37:31 +0100790
Neels Hofmeyrbe5f0332018-12-26 01:11:57 +0100791 /* Compare only the length expected. Extra data is fine, to not care about new GSUP IEs invented later. */
792 if (msg->len < len) {
793 fprintf(stderr, "ERROR: GSUP message too short, expected '%s'\n", gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100794 abort();
Neels Hofmeyrbe5f0332018-12-26 01:11:57 +0100795 }
796
797 if (memcmp(msg->data, buf, len)) {
798 fprintf(stderr, "ERROR: GSUP message mismatch, expected it to start with '%s'\n", gsup_tx_expected);
799 abort();
800 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100801
802 talloc_free(msg);
803 gsup_tx_confirmed = true;
804 gsup_tx_expected = NULL;
805 return 0;
806}
807
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200808struct rtp_stream fake_rtp[2] = {
809 {
810 .dir = RTP_TO_RAN,
811 .local = {
812 .ip = "10.23.42.1",
813 .port = 99,
814 },
815 .remote = {
816 .ip = "10.23.42.2",
817 .port = 100,
818 },
819 },
820 {
821 .dir = RTP_TO_CN,
822 .local = {
823 .ip = "10.23.42.1",
824 .port = 23,
825 },
826 .remote = {
827 .ip = "10.23.42.2",
828 .port = 42,
829 },
830 },
831};
832
833void expect_crcx(enum rtp_direction towards)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100834{
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200835 OSMO_ASSERT(expecting_crcx == -1);
836 expecting_crcx = towards;
837 got_crcx = false;
838}
839
840/* override, requires '-Wl,--wrap=call_leg_ensure_ci' */
841int __real_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
842 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known);
843int __wrap_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)
845{
846 if (!cl->rtp[dir]) {
847 log("MGW <--CRCX to %s-- MSC: callref=0x%x", rtp_direction_name(dir), call_id);
848
849 OSMO_ASSERT(expecting_crcx == dir);
850 expecting_crcx = -1;
851 got_crcx = true;
852
853 call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans);
854 if (codec_if_known)
855 rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
856 if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
857 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
858 }
859
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100860 return 0;
861}
862
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200863void crcx_ok(enum rtp_direction dir)
864{
865 struct msc_a *msc_a = msub_msc_a(g_msub);
866 struct call_leg *cl = msc_a->cc.call_leg;
867 OSMO_ASSERT(cl);
868 OSMO_ASSERT(cl->rtp[dir]);
869 osmo_sockaddr_str_from_str(&cl->rtp[dir]->local, "10.23.23.1", 23);
870 //osmo_sockaddr_str_from_str(&cl->rtp[dir].remote, "10.42.42.1", 42);
871 log("MGW --CRCX OK to %s--> MSC", rtp_direction_name(dir));
872 osmo_fsm_inst_dispatch(cl->fi, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, cl->rtp[dir]);
873}
874
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100875static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
876{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100877 struct msc_a *msc_a = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100878 if (send_tmsi == GSM_RESERVED_TMSI)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100879 btw("sending LU Accept for %s", msc_a->c.fi->id);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100880 else
881 btw("sending LU Accept for %s, with TMSI 0x%08x",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100882 msc_a->c.fi->id, send_tmsi);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100883 lu_result_sent |= RES_ACCEPT;
884 return 0;
885}
886
Neels Hofmeyr15809592018-04-06 02:57:51 +0200887static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100888{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100889 struct msc_a *msc_a = msc_conn_ref;
890 btw("sending LU Reject for %s, cause %u", msc_a->c.fi->id, cause);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100891 lu_result_sent |= RES_REJECT;
892 return 0;
893}
894
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100895static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
896 enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100897{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100898 struct msc_a *msc_a = msc_conn_ref;
899 btw("sending CM Service Reject (%s) for %s, cause: %s",
900 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 +0100901 cm_service_result_sent |= RES_REJECT;
902 return 0;
903}
904
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100905static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100906 bool send_autn)
907{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100908 struct msc_a *msc_a = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100909 char *hex;
910 bool ok = true;
911 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 +0100912 send_autn? "UMTS" : "GSM", msc_a->c.fi->id,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100913 at->use_count, at->key_seq, at->vec.auth_types);
914
915 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
916 btw("...rand=%s", hex);
917 if (!auth_request_expect_rand
918 || strcmp(hex, auth_request_expect_rand) != 0) {
919 ok = false;
920 log("FAILURE: expected rand=%s",
921 auth_request_expect_rand ? auth_request_expect_rand : "-");
922 }
923
924 if (send_autn) {
925 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
926 btw("...autn=%s", hex);
927 if (!auth_request_expect_autn
928 || strcmp(hex, auth_request_expect_autn) != 0) {
929 ok = false;
930 log("FAILURE: expected autn=%s",
931 auth_request_expect_autn ? auth_request_expect_autn : "-");
932 }
933 } else if (auth_request_expect_autn) {
934 ok = false;
935 log("FAILURE: no AUTN sent, expected AUTN = %s",
936 auth_request_expect_autn);
937 }
938
939 if (send_autn)
940 btw("...expecting res=%s",
941 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
942 else
943 btw("...expecting sres=%s",
944 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
945
946 auth_request_sent = ok;
947 return 0;
948}
949
950static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
951{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100952 struct msc_a *msc_a = msc_conn_ref;
953 btw("sending Auth Reject for %s", msc_a->c.fi->id);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100954 return 0;
955}
956
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100957void ms_sends_ciphering_mode_complete(const char *inner_ran_msg)
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100958{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100959 struct ran_msg ran_dec;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100960
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100961 msc_a_get(msub_msc_a(g_msub), __func__);
962
963 ran_dec = (struct ran_msg){
964 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
965 };
966 fake_msc_a_ran_dec(&ran_dec);
967
968 if (inner_ran_msg) {
969 struct msgb *msg = msgb_from_hex("cipher_mode_complete_ran", 1024, inner_ran_msg);
970 msg->l1h = msg->l2h = msg->l3h = msg->data;
971 ran_dec = (struct ran_msg){
972 .msg_type = RAN_MSG_DTAP,
973 .dtap = msg,
974 };
975 patch_l3_seq_nr(msg);
976 fake_msc_a_ran_dec(&ran_dec);
977 msgb_free(msg);
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100978 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100979
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100980 msc_a_put(msub_msc_a(g_msub), __func__);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100981
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100982 if (!conn_exists(g_msub))
983 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100984}
985
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200986void ms_sends_security_mode_complete()
987{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100988 struct ran_msg ran_dec;
989
990 ran_dec = (struct ran_msg){
991 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
992 };
993 fake_msc_a_ran_dec(&ran_dec);
994
995 if (!conn_exists(g_msub))
996 g_msub = NULL;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200997}
998
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200999void ms_sends_assignment_complete(enum mgcp_codecs assigned_codec)
1000{
1001 struct ran_msg ran_dec;
1002
1003 ran_dec = (struct ran_msg){
1004 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
1005 .assignment_complete = {
1006 .codec = assigned_codec,
1007 },
1008 };
1009 fake_msc_a_ran_dec(&ran_dec);
1010
1011 if (!conn_exists(g_msub))
1012 g_msub = NULL;
1013}
1014
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001015void ran_sends_clear_complete()
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001016{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001017 struct ran_msg ran_dec;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001018
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001019 ran_dec = (struct ran_msg){
1020 .msg_type = RAN_MSG_CLEAR_COMPLETE,
1021 };
1022 fake_msc_a_ran_dec(&ran_dec);
1023
1024 if (!conn_exists(g_msub))
1025 g_msub = NULL;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001026}
1027
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001028const struct timeval fake_time_start_time = { 123, 456 };
1029
1030void fake_time_start()
1031{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001032 struct timespec *clock_override;
1033
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001034 osmo_gettimeofday_override_time = fake_time_start_time;
1035 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001036 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
1037 OSMO_ASSERT(clock_override);
1038 clock_override->tv_sec = fake_time_start_time.tv_sec;
1039 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
1040 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001041 fake_time_passes(0, 0);
1042}
1043
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001044static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001045{
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001046 /* Verifying that the msgb context is empty */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001047 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001048 /* Expecting these to stick around in msc_vlr_tests_ctx:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001049 * full talloc report on 'msgb' (total 0 bytes in 1 blocks)
Alexander Couzensefa7b972019-04-27 23:45:37 +02001050 * talloc_total_blocks(tall_bsc_ctx) == 19
1051 * full talloc report on 'msc_vlr_tests_ctx' (total 6532 bytes in 19 blocks)
1052 * struct osmo_gsup_client contains 256 bytes in 1 blocks (ref 0) 0x56143306aa10
1053 * struct gsm_network contains 4775 bytes in 11 blocks (ref 0) 0x5614330697e0
1054 * struct mgcp_client contains 688 bytes in 1 blocks (ref 0) 0x56143306ad80
1055 * struct sccp_ran_inst contains 152 bytes in 1 blocks (ref 0) 0x56143306ac80
1056 * struct sccp_ran_inst contains 152 bytes in 1 blocks (ref 0) 0x56143306ab80
1057 * struct gsup_client_mux contains 152 bytes in 2 blocks (ref 0) 0x56143306a8a0
1058 * struct ipaccess_unit contains 64 bytes in 1 blocks (ref 0) 0x56143306a960
1059 * struct vlr_instance contains 248 bytes in 1 blocks (ref 0) 0x56143306a740
1060 * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x56143306a6c0
1061 * stat_item.c:96 contains 144 bytes in 2 blocks (ref 0) 0x56143306a550
1062 * stat_item.c:118 contains 96 bytes in 1 blocks (ref 0) 0x56143306a5f0
1063 * rate_ctr.c:234 contains 2352 bytes in 1 blocks (ref 0) 0x561433069bb0
1064 * logging contains 1501 bytes in 5 blocks (ref 0) 0x561433068fe0
1065 * struct log_target contains 244 bytes in 2 blocks (ref 0) 0x561433069610
1066 * struct log_category contains 76 bytes in 1 blocks (ref 0) 0x561433069720
1067 * struct log_info contains 1256 bytes in 2 blocks (ref 0) 0x561433069050
1068 * struct log_info_cat contains 1216 bytes in 1 blocks (ref 0) 0x5614330690e0
1069 * msgb contains 0 bytes in 1 blocks (ref 0) 0x561433068f70
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001070 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001071 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001072 talloc_total_blocks(msc_vlr_tests_ctx));
Alexander Couzensefa7b972019-04-27 23:45:37 +02001073 if (talloc_total_blocks(msc_vlr_tests_ctx) != 19)
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001074 talloc_report_full(msc_vlr_tests_ctx, stderr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001075 fprintf(stderr, "\n");
1076}
1077
1078static struct {
1079 bool verbose;
1080 int run_test_nr;
1081} cmdline_opts = {
1082 .verbose = false,
1083 .run_test_nr = -1,
1084};
1085
1086static void print_help(const char *program)
1087{
1088 printf("Usage:\n"
1089 " %s [-v] [N [N...]]\n"
1090 "Options:\n"
1091 " -h --help show this text.\n"
1092 " -v --verbose print source file and line numbers\n"
1093 " N run only the Nth test (first test is N=1)\n",
1094 program
1095 );
1096}
1097
1098static void handle_options(int argc, char **argv)
1099{
1100 while (1) {
1101 int option_index = 0, c;
1102 static struct option long_options[] = {
1103 {"help", 0, 0, 'h'},
1104 {"verbose", 1, 0, 'v'},
1105 {0, 0, 0, 0}
1106 };
1107
1108 c = getopt_long(argc, argv, "hv",
1109 long_options, &option_index);
1110 if (c == -1)
1111 break;
1112
1113 switch (c) {
1114 case 'h':
1115 print_help(argv[0]);
1116 exit(0);
1117 case 'v':
1118 cmdline_opts.verbose = true;
1119 break;
1120 default:
1121 /* catch unknown options *as well as* missing arguments. */
1122 fprintf(stderr, "Error in command line options. Exiting.\n");
1123 exit(-1);
1124 break;
1125 }
1126 }
1127}
1128
1129void *msgb_ctx = NULL;
1130
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001131static void run_tests(int nr)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001132{
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001133 int test_nr;
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001134
1135 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
1136
Max5e2e9bd2018-02-06 19:31:08 +01001137 nr--; /* arg's first test is 1, in here it's 0 */
1138 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001139 if (nr >= 0 && test_nr != nr)
1140 continue;
1141
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001142 if (cmdline_opts.verbose)
1143 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
1144
1145 msc_vlr_tests[test_nr]();
1146
1147 if (cmdline_opts.verbose)
1148 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001149
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001150 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Max29ce08a2018-02-06 18:46:57 +01001151 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001152}
1153
Max5e60de62018-02-07 12:56:09 +01001154struct gsm_network *test_net(void *ctx)
1155{
Neels Hofmeyr7f484202018-02-27 12:59:45 +01001156 struct gsm_network *net = gsm_network_init(ctx, mncc_recv);
Max5e60de62018-02-07 12:56:09 +01001157
1158 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
1159 net->gsup_server_port = 0;
1160
1161 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
Max5e60de62018-02-07 12:56:09 +01001162 OSMO_ASSERT(net->vlr);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001163 OSMO_ASSERT(msc_gsup_client_start(net) == 0);
1164 OSMO_ASSERT(net->gcm);
1165 OSMO_ASSERT(msc_vlr_start(net) == 0);
Max5e60de62018-02-07 12:56:09 +01001166
1167 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
1168 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001169 net->vlr->ops.tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc;
Max5e60de62018-02-07 12:56:09 +01001170 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
1171 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
1172 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001173 net->vlr->ops.set_ciph_mode = msc_a_vlr_set_cipher_mode;
1174
1175 /* Allocate fake SCCP Ran Instances */
1176 net->a.sri = talloc_zero(net, struct sccp_ran_inst);
1177 *net->a.sri = (struct sccp_ran_inst){
1178 .ran = &test_ran_infra[OSMO_RAT_GERAN_A],
1179 };
1180 INIT_LLIST_HEAD(&net->a.sri->ran_peers);
1181 INIT_LLIST_HEAD(&net->a.sri->ran_conns);
1182
1183 net->iu.sri = talloc_zero(net, struct sccp_ran_inst);
1184 *net->iu.sri = (struct sccp_ran_inst){
1185 .ran = &test_ran_infra[OSMO_RAT_UTRAN_IU],
1186 };
1187 INIT_LLIST_HEAD(&net->iu.sri->ran_peers);
1188 INIT_LLIST_HEAD(&net->iu.sri->ran_conns);
1189
1190 net->mgw.tdefs = g_mgw_tdefs;
1191 mgcp_client_conf_init(&net->mgw.conf);
1192 net->mgw.tdefs = g_mgw_tdefs;
1193 net->mgw.client = mgcp_client_init(net, &net->mgw.conf);
Max5e60de62018-02-07 12:56:09 +01001194
1195 return net;
1196}
1197
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001198int main(int argc, char **argv)
1199{
1200 handle_options(argc, argv);
1201
Neels Hofmeyre4f7e712019-03-24 21:07:33 +01001202 osmo_fsm_term_safely(true);
1203
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001204 msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx");
1205 msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0);
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001206 osmo_init_logging2(msc_vlr_tests_ctx, &info);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001207
1208 _log_lines = cmdline_opts.verbose;
1209
1210 OSMO_ASSERT(osmo_stderr_target);
1211 log_set_use_color(osmo_stderr_target, 0);
1212 log_set_print_timestamp(osmo_stderr_target, 0);
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001213 log_set_print_filename(osmo_stderr_target, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001214 log_set_print_category(osmo_stderr_target, 1);
Max48131522019-01-16 12:47:39 +01001215 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001216
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001217 if (cmdline_opts.verbose) {
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001218 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
1219 log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
1220 log_set_use_color(osmo_stderr_target, 1);
1221 log_set_print_level(osmo_stderr_target, 1);
1222 }
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +01001223
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001224 net = test_net(msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001225
1226 osmo_fsm_log_addr(false);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001227 osmo_fsm_log_timeouts(cmdline_opts.verbose);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001229 call_leg_init(net);
1230
1231 OSMO_ASSERT(osmo_fsm_register(&dummy_msc_i_fsm) == 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001232
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001233 clear_vlr();
1234
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001235 if (optind >= argc)
1236 run_tests(-1);
1237 else {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001238 int arg;
1239 long int nr;
1240 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +01001241 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001242 nr = strtol(argv[arg], NULL, 10);
1243 if (errno) {
1244 fprintf(stderr, "Invalid argument: %s\n",
1245 argv[arg]);
1246 exit(1);
1247 }
1248
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001249 run_tests(nr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001250 }
1251 }
1252
1253 printf("Done\n");
1254
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001255 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001256 return 0;
1257}