blob: b0c60edad0cf5529d7792a799cfee5597bb440a2 [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;
Neels Hofmeyrdbb3c722021-02-07 23:53:07 +010048void *msgb_ctx = NULL;
Neels Hofmeyrc01e9092018-03-22 15:56:49 +010049
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010050bool _log_lines = false;
51
52struct gsm_network *net = NULL;
53
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010054const char *gsup_tx_expected = NULL;
55bool gsup_tx_confirmed;
56
57struct msgb *dtap_tx_expected = NULL;
58bool dtap_tx_confirmed;
59
60enum result_sent lu_result_sent;
61enum result_sent cm_service_result_sent;
62bool auth_request_sent;
63const char *auth_request_expect_rand;
64const char *auth_request_expect_autn;
65bool cipher_mode_cmd_sent;
66bool cipher_mode_cmd_sent_with_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +010067const char *cipher_mode_expect_kc;
68bool security_mode_ctrl_sent;
69const char *security_mode_expect_ck;
70const char *security_mode_expect_ik;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010071
Philipp Maierfbf66102017-04-09 12:32:51 +020072bool iu_release_expected = false;
73bool iu_release_sent = false;
74bool bssap_clear_expected = false;
75bool bssap_clear_sent = false;
76
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +020077bool bssap_assignment_expected = false;
78bool bssap_assignment_sent = false;
79bool iu_rab_assignment_expected = false;
80bool iu_rab_assignment_sent = false;
81
Neels Hofmeyra99b4272017-11-21 17:13:23 +010082uint32_t cc_to_mncc_tx_expected_msg_type = 0;
83const char *cc_to_mncc_tx_expected_imsi = NULL;
84bool cc_to_mncc_tx_confirmed = false;
85uint32_t cc_to_mncc_tx_got_callref = 0;
86
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +020087enum rtp_direction expecting_crcx = -1;
88bool got_crcx = false;
89
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010090extern int ran_dec_dtap_undup_pdisc_ctr_bin(uint8_t pdisc);
Harald Weltec2007852018-02-03 21:08:26 +010091
92/* static state variables for the L3 send sequence numbers */
93static uint8_t n_sd[4];
94
95/* patch a correct send sequence number into the given message */
96static void patch_l3_seq_nr(struct msgb *msg)
97{
98 struct gsm48_hdr *gh = msgb_l3(msg);
99 uint8_t pdisc = gsm48_hdr_pdisc(gh);
100 uint8_t *msg_type_oct = &msg->l3h[1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100101 int bin = ran_dec_dtap_undup_pdisc_ctr_bin(pdisc);
Harald Weltec2007852018-02-03 21:08:26 +0100102
103 if (bin >= 0 && bin < ARRAY_SIZE(n_sd)) {
104 /* patch in n_sd into the msg_type octet */
105 *msg_type_oct = (*msg_type_oct & 0x3f) | ((n_sd[bin] & 0x3) << 6);
106 //fprintf(stderr, "pdisc=0x%02x bin=%d, patched n_sd=%u\n\n", pdisc, bin, n_sd[bin] & 3);
107 /* increment N(SD) */
108 n_sd[bin] = (n_sd[bin] + 1) % 4;
109 } else {
110 //fprintf(stderr, "pdisc=0x%02x NO SEQ\n\n", pdisc);
111 }
112}
113
114/* reset L3 sequence numbers (e.g. new RR connection) */
115static void reset_l3_seq_nr()
116{
117 memset(n_sd, 0, sizeof(n_sd));
118}
119
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100120struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
121{
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200122 struct msgb *msg = msgb_alloc_headroom(size, 4, label);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100123 unsigned char *rc;
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200124 msg->l2h = msg->data;
125 rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg)));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100126 OSMO_ASSERT(rc == msg->l2h);
127 return msg;
128}
129
Maxd8d1a9e2018-02-06 15:50:20 +0100130static const char *gh_type_name(struct gsm48_hdr *gh)
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100131{
132 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
133 gsm48_hdr_msg_type(gh));
134}
135
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100136void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
137{
138 int rc;
139 struct msgb *msg;
140 const char *label;
141
142 gsup_expect_tx(expect_tx_hex);
143
144 msg = msgb_from_hex("gsup", 1024, rx_hex);
145 label = osmo_gsup_message_type_name(msg->l2h[0]);
146 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
147 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
Neels Hofmeyr834f94a2017-09-28 03:07:16 +0200148 /* GSUP read cb takes ownership of msgb */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100149 rc = gsup_client_mux_rx(net->gcm->gsup_client, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100150 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
151 label, rc);
152 if (expect_tx_hex)
153 OSMO_ASSERT(gsup_tx_confirmed);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100154}
155
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100156bool conn_exists(const struct msub *msub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100157{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100158 struct msub *i;
Maxba8a0072018-10-25 17:58:43 +0200159
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100160 if (!msub)
Maxba8a0072018-10-25 17:58:43 +0200161 return false;
162
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100163 llist_for_each_entry(i, &msub_list, entry) {
164 if (i == msub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100165 return true;
166 }
Maxba8a0072018-10-25 17:58:43 +0200167
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100168 btw("msub gone");
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100169 return false;
170}
171
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700172/* Simplified version of the cm_service_request_concludes() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100173void conn_conclude_cm_service_req(struct msub *msub, const char *cm_service_use)
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700174{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100175 int32_t count;
176 struct msc_a *msc_a = msub_msc_a(msub);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700177 btw("Concluding CM Service Request");
178
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100179 OSMO_ASSERT(conn_exists(msub));
180 count = osmo_use_count_by(&msc_a->use_count, cm_service_use);
181 OSMO_ASSERT(count > 0);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700182
Vadim Yanitskiyd14422a2019-07-09 00:37:49 +0700183 OSMO_ASSERT(osmo_use_count_get_put(&msc_a->use_count, cm_service_use, -count) == 0);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700184
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100185 ASSERT_RELEASE_CLEAR(msc_a->c.ran->type);
186}
187
188void dummy_msc_i_action(struct osmo_fsm_inst *fi, uint32_t event, void *data)
189{
190}
191
192static const struct osmo_fsm_state dummy_msc_i_states[] = {
193 {
194 .name = "0",
195 .in_event_mask = 0xffffffff,
196 .action = dummy_msc_i_action,
197 },
198};
199
200struct osmo_fsm dummy_msc_i_fsm = {
201 .name = "dummy_msc_i",
202 .states = dummy_msc_i_states,
203 .num_states = ARRAY_SIZE(dummy_msc_i_states),
204 .log_subsys = DMSC,
205 .event_names = msc_i_fsm_event_names,
206};
207
208struct msc_i *dummy_msc_i_alloc(struct msub *msub, struct ran_infra *ran)
209{
210 return msub_role_alloc(g_msub, MSC_ROLE_I, &dummy_msc_i_fsm, struct msc_i, ran);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700211}
212
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100213enum osmo_rat_type rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100214
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100215struct msub *g_msub = NULL;
Harald Welte977b5482019-02-18 12:23:43 +0100216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100217void dtap_expect_tx(const char *hex)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100218{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100219 /* Has the previously expected dtap been received? */
220 OSMO_ASSERT(!dtap_tx_expected);
221 if (!hex)
222 return;
223 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
224 /* Mask the sequence number out */
225 if (msgb_length(dtap_tx_expected) >= 2)
226 dtap_tx_expected->data[1] &= 0x3f;
227 dtap_tx_confirmed = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100228}
229
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100230static int _validate_dtap(struct msgb *msg, enum osmo_rat_type to_ran)
231{
232 struct gsm48_hdr *gh = (void*)msg->data;
233 uint8_t pdisc = gsm48_hdr_pdisc(gh);
234 uint8_t msgt = gsm48_hdr_msg_type(gh);
235
236 btw("DTAP --%s--> MS: %s: %s",
237 osmo_rat_type_name(to_ran), gh_type_name((void*)msg->data),
238 osmo_hexdump_nospc(msg->data, msg->len));
239
240 if (pdisc == GSM48_PDISC_MM
241 && msgt == GSM48_MT_MM_CM_SERV_ACC) {
242 cm_service_result_sent |= RES_ACCEPT;
243 talloc_free(msg);
244 return 0;
245 }
246
247 if (pdisc == GSM48_PDISC_MM
248 && msgt == GSM48_MT_MM_CM_SERV_REJ) {
249 cm_service_result_sent |= RES_REJECT;
250 talloc_free(msg);
251 return 0;
252 }
253
254 OSMO_ASSERT(dtap_tx_expected);
255
256 /* Mask the sequence number out before comparing */
257 msg->data[1] &= 0x3f;
Neels Hofmeyre0f906c2019-10-21 06:00:35 +0200258 if (!msgb_eq_data_print(msg, dtap_tx_expected->data, dtap_tx_expected->len)) {
259 btw("Expected %s", osmo_hexdump(dtap_tx_expected->data, dtap_tx_expected->len));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100260 abort();
Neels Hofmeyre0f906c2019-10-21 06:00:35 +0200261 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100262
263 btw("DTAP matches expected message");
264
265 talloc_free(msg);
266 dtap_tx_confirmed = true;
267 talloc_free(dtap_tx_expected);
268 dtap_tx_expected = NULL;
269
270 return 0;
271}
272
273static void bssap_validate_clear_cmd()
274{
275 OSMO_ASSERT(bssap_clear_expected);
276 bssap_clear_expected = false;
277 bssap_clear_sent = true;
278}
279
280static void iucs_validate_clear_cmd()
281{
282 OSMO_ASSERT(iu_release_expected);
283 iu_release_expected = false;
284 iu_release_sent = true;
285}
286
287static int bssap_validate_cipher_mode_cmd(const struct ran_cipher_mode_command *cmd)
288{
289 int i;
290 const char *got_key;
291 cipher_mode_cmd_sent = true;
292 cipher_mode_cmd_sent_with_imeisv = cmd->geran.retrieve_imeisv;
293 btw("sending Ciphering Mode Command: retrieve_imeisv=%d", cipher_mode_cmd_sent_with_imeisv);
294 for (i = 0; i < 7; i++) {
295 if (!(cmd->geran.a5_encryption_mask & (1 << i)))
296 continue;
297 btw("...perm algo: A5/%d", i);
298 }
299 got_key = osmo_hexdump_nospc(cmd->vec->kc, sizeof(cmd->vec->kc));
300 btw("...key: %s", got_key);
301
302 if (!cipher_mode_expect_kc
303 || strcmp(cipher_mode_expect_kc, got_key)) {
304 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
305 OSMO_ASSERT(false);
306 }
307 return 0;
308}
309
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200310static void bssap_validate_assignment_cmd()
311{
312 OSMO_ASSERT(bssap_assignment_expected);
313 bssap_assignment_expected = false;
314 bssap_assignment_sent = true;
315}
316
317static void iucs_validate_assignment_cmd()
318{
319 OSMO_ASSERT(iu_rab_assignment_expected);
320 iu_rab_assignment_expected = false;
321 iu_rab_assignment_sent = true;
322}
323
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100324static int iucs_validate_security_mode_ctrl(const struct ran_cipher_mode_command *cmd)
325{
326 const char *got_ik;
327 got_ik = osmo_hexdump_nospc(cmd->vec->ik, sizeof(cmd->vec->ik));
328 btw("sending SecurityModeControl: ik=%s", got_ik);
329 security_mode_ctrl_sent = true;
330 if (!security_mode_expect_ik
331 || strcmp(security_mode_expect_ik, got_ik)) {
332 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
333 OSMO_ASSERT(false);
334 }
335 return 0;
336}
337
338struct msgb *dont_ran_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
339{
340 struct msc_role_common *c = caller_fi->priv;
341 enum osmo_rat_type ran_type = c->ran->type;
342 const char *ran_name = osmo_rat_type_name(ran_type);
343 LOG_RAN_ENC(caller_fi, DMSC, LOGL_INFO, "%s on %s\n", ran_msg_type_name(ran_enc_msg->msg_type),
344 ran_name);
345
346 switch (ran_enc_msg->msg_type) {
347 case RAN_MSG_DTAP:
348 _validate_dtap(ran_enc_msg->dtap, ran_type);
349 break;
350 case RAN_MSG_CLEAR_COMMAND:
351 switch (ran_type) {
352 case OSMO_RAT_GERAN_A:
353 bssap_validate_clear_cmd();
354 break;
355 case OSMO_RAT_UTRAN_IU:
356 iucs_validate_clear_cmd();
357 break;
358 default:
359 OSMO_ASSERT(false);
360 }
361 break;
362 case RAN_MSG_CIPHER_MODE_COMMAND:
363 switch (ran_type) {
364 case OSMO_RAT_GERAN_A:
365 bssap_validate_cipher_mode_cmd(&ran_enc_msg->cipher_mode_command);
366 break;
367 case OSMO_RAT_UTRAN_IU:
368 iucs_validate_security_mode_ctrl(&ran_enc_msg->cipher_mode_command);
369 break;
370 default:
371 OSMO_ASSERT(false);
372 }
373 break;
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200374 case RAN_MSG_ASSIGNMENT_COMMAND:
375 switch (ran_type) {
376 case OSMO_RAT_GERAN_A:
377 bssap_validate_assignment_cmd();
378 break;
379 case OSMO_RAT_UTRAN_IU:
380 iucs_validate_assignment_cmd();
381 break;
382 default:
383 OSMO_ASSERT(false);
384 }
385 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100386 default:
387 break;
388 }
389
390 /* We're testing MSC and VLR interaction, not message encoding.
391 * Return whatever. The test msc_i instance is a dummy and drops these.
392 * But it must be msg_free()-able.
393 */
394 return msgb_alloc(1, "unused dummy msg");
395}
396
397struct ran_infra test_ran_infra[] = {
398 [OSMO_RAT_GERAN_A] = {
399 .type = OSMO_RAT_GERAN_A,
400 .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_48006,
401 .log_subsys = DBSSAP,
402 .tdefs = msc_tdefs_geran,
403 .ran_encode = dont_ran_encode,
404 },
405 [OSMO_RAT_UTRAN_IU] = {
406 .type = OSMO_RAT_UTRAN_IU,
407 .an_proto = OSMO_GSUP_ACCESS_NETWORK_PROTOCOL_TS3G_25413,
408 .log_subsys = DIUCS,
409 .tdefs = msc_tdefs_utran,
410 .ran_encode = dont_ran_encode,
Neels Hofmeyr1dc39612023-03-03 16:43:19 +0100411 .force_mgw_codecs_to_ran = {
412 .count = 1,
413 .codec = {
414 {
415 .payload_type = 96,
416 .subtype_name = "VND.3GPP.IUFP",
417 .rate = 16000,
418 },
419 },
420 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100421 },
422};
423
424static int fake_msc_a_ran_dec(const struct ran_msg *ran_dec_msg)
425{
426 struct msc_a_ran_dec_data d = {
427 .from_role = MSC_ROLE_I,
428 };
429 return msc_a_ran_decode_cb(g_msub->role[MSC_ROLE_A], &d, ran_dec_msg);
430}
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100431
432void rx_from_ms(struct msgb *msg)
433{
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100434 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100435 struct ran_msg ran_dec_msg;
436 struct gsm0808_cell_id cell_id = {
437 .id_discr = CELL_IDENT_LAI_AND_LAC,
438 .id.lai_and_lac = {
439 .plmn = {
440 .mcc = 1,
441 .mnc = 2,
442 },
443 .lac = 23,
444 },
445 };
446 struct msc_a *msc_a;
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100447
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100448 log("MSC <--%s-- MS: %s", osmo_rat_type_name(rx_from_ran), gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100449
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100450 if (!conn_exists(g_msub))
451 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100452
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100453 if (!g_msub) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100454 log("new conn");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100455 g_msub = msub_alloc(net);
456 msc_a_alloc(g_msub, &test_ran_infra[rx_from_ran]);
457 dummy_msc_i_alloc(g_msub, &test_ran_infra[rx_from_ran]);
458
Harald Weltec2007852018-02-03 21:08:26 +0100459 reset_l3_seq_nr();
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100460 ran_dec_msg = (struct ran_msg){
461 .msg_type = RAN_MSG_COMPL_L3,
462 .compl_l3 = {
463 .cell_id = &cell_id,
464 .msg = msg,
465 },
466 };
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100467 } else {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100468 ran_dec_msg = (struct ran_msg){
469 .msg_type = RAN_MSG_DTAP,
470 .dtap = msg,
471 };
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100472 }
473
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100474 msc_a = msub_msc_a(g_msub);
475 msc_a_get(msc_a, __func__);
476
477 patch_l3_seq_nr(msg);
478 fake_msc_a_ran_dec(&ran_dec_msg);
479
480 msc_a_put(msc_a, __func__);
481
482 if (!conn_exists(g_msub))
483 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100484}
485
486void ms_sends_msg(const char *hex)
487{
488 struct msgb *msg;
489
490 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
491 msg->l1h = msg->l2h = msg->l3h = msg->data;
492 rx_from_ms(msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200493 msgb_free(msg);
494}
495
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100496void ms_sends_classmark_update(const struct osmo_gsm48_classmark *classmark)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200497{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100498 struct ran_msg ran_dec = {
499 .msg_type = RAN_MSG_CLASSMARK_UPDATE,
500 .classmark_update = {
501 .classmark = classmark,
502 },
503 };
504 fake_msc_a_ran_dec(&ran_dec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100505}
506
Maxd8d1a9e2018-02-06 15:50:20 +0100507static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100508{
509 int rc;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100510 struct ran_msg ran_dec;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100511 struct msgb *msg;
512 struct gsm48_hdr *gh;
513
514 msg = msgb_alloc(1024, "ms_sends_msg_fake");
515 msg->l1h = msg->l2h = msg->l3h = msg->data;
516
517 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
518 gh->proto_discr = pdisc;
519 gh->msg_type = msg_type;
520 /* some amount of data, whatever */
521 msgb_put(msg, 123);
522
Harald Weltec2007852018-02-03 21:08:26 +0100523 patch_l3_seq_nr(msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100524
525 ran_dec = (struct ran_msg){
526 .msg_type = RAN_MSG_DTAP,
527 .dtap = msg,
528 };
529 rc = fake_msc_a_ran_dec(&ran_dec);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100530
531 talloc_free(msg);
532 return rc;
533}
534
Max68171902018-02-06 17:55:19 +0100535static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype)
536{
537 int rc = ms_sends_msg_fake(val, msgtype);
538 if (rc != -EACCES)
539 log("Unexpected return value %u != %u for %s/%s",
540 -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype));
541}
542
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100543void thwart_rx_non_initial_requests()
544{
545 log("requests shall be thwarted");
Max68171902018-02-06 17:55:19 +0100546
547 ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP);
548 ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */
549 ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1);
550 ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100551}
552
553void send_sms(struct vlr_subscr *receiver,
554 struct vlr_subscr *sender,
555 char *str)
556{
Harald Welte39b55482018-04-09 19:19:33 +0200557 struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str);
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700558 gsm411_send_sms(net, receiver, sms);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100559}
560
561unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100562/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
563int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
564int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100565{
Max753c15d2017-12-21 14:50:44 +0100566 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100567 for (i = 0; i < num; i++)
568 buf[i] = next_rand_byte++;
569 return 1;
570}
571
572/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
573void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
574void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
575{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200576 /* Write fixed time bytes for deterministic test results */
577 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100578}
579
580const char *paging_expecting_imsi = NULL;
581uint32_t paging_expecting_tmsi;
582bool paging_sent;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100583
584void paging_expect_imsi(const char *imsi)
585{
586 paging_expecting_imsi = imsi;
587 paging_expecting_tmsi = GSM_RESERVED_TMSI;
588}
589
590void paging_expect_tmsi(uint32_t tmsi)
591{
592 paging_expecting_tmsi = tmsi;
593 paging_expecting_imsi = NULL;
594}
595
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100596/* override, requires '-Wl,--wrap=ran_peers_down_paging' */
597int __real_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub,
598 enum paging_cause cause);
599int __wrap_ran_peers_down_paging(struct sccp_ran_inst *sri, enum CELL_IDENT page_where, struct vlr_subscr *vsub,
600 enum paging_cause cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100601{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100602 log("paging request (%s) to %s on %s", paging_cause_name(cause), vlr_subscr_name(vsub),
603 osmo_rat_type_name(sri->ran->type));
604
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100605 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
606 if (paging_expecting_imsi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100607 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, vsub->imsi), == 0, "%d");
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200608 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100609 VERBOSE_ASSERT(paging_expecting_tmsi, == vsub->tmsi, "0x%08x");
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200610 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100611 paging_sent = true;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100612 return 1;
613}
614
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100615void clear_vlr()
616{
617 struct vlr_subscr *vsub, *n;
618 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
619 vlr_subscr_free(vsub);
620 }
621
622 net->authentication_required = false;
Harald Welte7b222aa2017-12-23 19:30:32 +0100623 net->a5_encryption_mask = (1 << 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100624 net->vlr->cfg.check_imei_rqd = false;
625 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200626 net->vlr->cfg.retrieve_imeisv_early = false;
627 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100628 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
629 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100630
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100631 rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100632 auth_request_sent = false;
633 auth_request_expect_rand = NULL;
634 auth_request_expect_autn = NULL;
635
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100636 cipher_mode_cmd_sent = false;
637 cipher_mode_cmd_sent_with_imeisv = false;
638 cipher_mode_expect_kc = NULL;
639
640 security_mode_ctrl_sent = false;
641 security_mode_expect_ck = NULL;
642 security_mode_expect_ik = NULL;
643
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100644 next_rand_byte = 0;
645
Philipp Maierfbf66102017-04-09 12:32:51 +0200646 iu_release_expected = false;
647 iu_release_sent = false;
648 bssap_clear_expected = false;
649 bssap_clear_sent = false;
650
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100651 osmo_gettimeofday_override = false;
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200652
653 expecting_crcx = -1;
654 got_crcx = false;
655
656 bssap_assignment_expected = false;
657 bssap_assignment_sent = false;
658 iu_rab_assignment_expected = false;
659 iu_rab_assignment_sent = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100660}
661
662static struct log_info_cat test_categories[] = {
663 [DMSC] = {
664 .name = "DMSC",
665 .description = "Mobile Switching Center",
666 .enabled = 1, .loglevel = LOGL_DEBUG,
667 },
668 [DRLL] = {
669 .name = "DRLL",
670 .description = "A-bis Radio Link Layer (RLL)",
671 .enabled = 1, .loglevel = LOGL_DEBUG,
672 },
673 [DMM] = {
674 .name = "DMM",
675 .description = "Layer3 Mobility Management (MM)",
676 .enabled = 1, .loglevel = LOGL_DEBUG,
677 },
678 [DRR] = {
679 .name = "DRR",
680 .description = "Layer3 Radio Resource (RR)",
681 .enabled = 1, .loglevel = LOGL_DEBUG,
682 },
683 [DCC] = {
684 .name = "DCC",
685 .description = "Layer3 Call Control (CC)",
Neels Hofmeyr12e17be2018-03-12 23:59:37 +0100686 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100687 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100688 [DVLR] = {
689 .name = "DVLR",
690 .description = "Visitor Location Register",
691 .enabled = 1, .loglevel = LOGL_DEBUG,
692 },
693 [DREF] = {
694 .name = "DREF",
695 .description = "Reference Counting",
696 .enabled = 1, .loglevel = LOGL_DEBUG,
697 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200698 [DPAG] = {
699 .name = "DPAG",
700 .description = "Paging Subsystem",
701 .enabled = 1, .loglevel = LOGL_DEBUG,
702 },
703 [DIUCS] = {
704 .name = "DIUCS",
705 .description = "Iu-CS Protocol",
706 .enabled = 1, .loglevel = LOGL_DEBUG,
707 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100708 [DMNCC] = {
709 .name = "DMNCC",
710 .description = "MNCC API for Call Control application",
711 .enabled = 1, .loglevel = LOGL_DEBUG,
712 },
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100713 [DBSSAP] = {
714 .name = "DBSSAP",
715 .description = "BSSAP Protocol (A Interface)",
716 .enabled = 1, .loglevel = LOGL_DEBUG,
717 },
Neels Hofmeyr979b0572019-05-09 15:24:49 +0200718 [DSS] = {
719 .name = "DSS",
720 .description = "Supplementary Services",
721 .enabled = 1, .loglevel = LOGL_DEBUG,
722 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100723};
724
725static struct log_info info = {
726 .cat = test_categories,
727 .num_cat = ARRAY_SIZE(test_categories),
728};
729
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100730struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
731
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100732int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100733{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100734 struct gsm_mncc *mncc = (void*)msg->data;
735 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
736 get_mncc_name(mncc->msg_type));
737
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100738 if (mncc->msg_type == MNCC_REL_IND && on_call_release_mncc_sends_to_cc_data) {
739
740 log("MNCC: callref 0x%x: Call Release triggering %s", mncc->callref,
741 get_mncc_name(on_call_release_mncc_sends_to_cc_data->msg_type));
742
Neels Hofmeyr52558742019-05-09 01:23:09 +0200743 mncc_tx_to_cc(net,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100744 on_call_release_mncc_sends_to_cc_data);
745
746 on_call_release_mncc_sends_to_cc_data = NULL;
747 return 0;
748 }
749
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100750 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
751 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
752 log("Mismatch! Expected MNCC msg type: %s",
753 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
754 abort();
755 }
756
757 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
758 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
759 cc_to_mncc_tx_expected_imsi,
760 mncc->imsi);
761 abort();
762 }
763
764 cc_to_mncc_tx_confirmed = true;
765 cc_to_mncc_tx_got_callref = mncc->callref;
766 cc_to_mncc_tx_expected_imsi = NULL;
767 cc_to_mncc_tx_expected_msg_type = 0;
768 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100769 return 0;
770}
771
Harald Welte1ea6baf2018-07-31 19:40:52 +0200772struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100773__real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
774 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
775 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200776struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100777__wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
778 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
779 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100780{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200781 struct osmo_gsup_client *gsupc;
782 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100783 OSMO_ASSERT(gsupc);
784 return gsupc;
785}
786
787/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200788int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
789int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100790{
Max34d306b2019-01-15 18:37:31 +0100791 uint8_t buf[512];
792 int len;
793
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100794 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
Max34d306b2019-01-15 18:37:31 +0100795 osmo_gsup_message_type_name(msg->data[0]), osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100796
797 OSMO_ASSERT(gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100798 OSMO_ASSERT(strlen(gsup_tx_expected) <= (sizeof(buf) * 2));
799
800 len = osmo_hexparse(gsup_tx_expected, buf, sizeof(buf));
801 if (len < 1)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100802 abort();
Max34d306b2019-01-15 18:37:31 +0100803
Neels Hofmeyrbe5f0332018-12-26 01:11:57 +0100804 /* Compare only the length expected. Extra data is fine, to not care about new GSUP IEs invented later. */
805 if (msg->len < len) {
806 fprintf(stderr, "ERROR: GSUP message too short, expected '%s'\n", gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100807 abort();
Neels Hofmeyrbe5f0332018-12-26 01:11:57 +0100808 }
809
810 if (memcmp(msg->data, buf, len)) {
811 fprintf(stderr, "ERROR: GSUP message mismatch, expected it to start with '%s'\n", gsup_tx_expected);
812 abort();
813 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100814
815 talloc_free(msg);
816 gsup_tx_confirmed = true;
817 gsup_tx_expected = NULL;
818 return 0;
819}
820
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200821struct rtp_stream fake_rtp[2] = {
822 {
823 .dir = RTP_TO_RAN,
824 .local = {
825 .ip = "10.23.42.1",
826 .port = 99,
827 },
828 .remote = {
829 .ip = "10.23.42.2",
830 .port = 100,
831 },
832 },
833 {
834 .dir = RTP_TO_CN,
835 .local = {
836 .ip = "10.23.42.1",
837 .port = 23,
838 },
839 .remote = {
840 .ip = "10.23.42.2",
841 .port = 42,
842 },
843 },
844};
845
846void expect_crcx(enum rtp_direction towards)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100847{
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200848 OSMO_ASSERT(expecting_crcx == -1);
849 expecting_crcx = towards;
850 got_crcx = false;
851}
852
853/* override, requires '-Wl,--wrap=call_leg_ensure_ci' */
854int __real_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
855 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known);
856int __wrap_call_leg_ensure_ci(struct call_leg *cl, enum rtp_direction dir, uint32_t call_id, struct gsm_trans *for_trans,
857 const enum mgcp_codecs *codec_if_known, const struct osmo_sockaddr_str *remote_addr_if_known)
858{
859 if (!cl->rtp[dir]) {
860 log("MGW <--CRCX to %s-- MSC: callref=0x%x", rtp_direction_name(dir), call_id);
861
862 OSMO_ASSERT(expecting_crcx == dir);
863 expecting_crcx = -1;
864 got_crcx = true;
865
866 call_leg_ensure_rtp_alloc(cl, dir, call_id, for_trans);
867 if (codec_if_known)
868 rtp_stream_set_codec(cl->rtp[dir], *codec_if_known);
869 if (remote_addr_if_known && osmo_sockaddr_str_is_nonzero(remote_addr_if_known))
870 rtp_stream_set_remote_addr(cl->rtp[dir], remote_addr_if_known);
871 }
872
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100873 return 0;
874}
875
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +0200876void crcx_ok(enum rtp_direction dir)
877{
878 struct msc_a *msc_a = msub_msc_a(g_msub);
879 struct call_leg *cl = msc_a->cc.call_leg;
880 OSMO_ASSERT(cl);
881 OSMO_ASSERT(cl->rtp[dir]);
882 osmo_sockaddr_str_from_str(&cl->rtp[dir]->local, "10.23.23.1", 23);
883 //osmo_sockaddr_str_from_str(&cl->rtp[dir].remote, "10.42.42.1", 42);
884 log("MGW --CRCX OK to %s--> MSC", rtp_direction_name(dir));
885 osmo_fsm_inst_dispatch(cl->fi, CALL_LEG_EV_RTP_STREAM_ADDR_AVAILABLE, cl->rtp[dir]);
886}
887
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100888static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
889{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100890 struct msc_a *msc_a = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100891 if (send_tmsi == GSM_RESERVED_TMSI)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100892 btw("sending LU Accept for %s", msc_a->c.fi->id);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100893 else
894 btw("sending LU Accept for %s, with TMSI 0x%08x",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100895 msc_a->c.fi->id, send_tmsi);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100896 lu_result_sent |= RES_ACCEPT;
897 return 0;
898}
899
Neels Hofmeyr15809592018-04-06 02:57:51 +0200900static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100901{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100902 struct msc_a *msc_a = msc_conn_ref;
903 btw("sending LU Reject for %s, cause %u", msc_a->c.fi->id, cause);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100904 lu_result_sent |= RES_REJECT;
905 return 0;
906}
907
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100908static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum osmo_cm_service_type cm_service_type,
909 enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100910{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100911 struct msc_a *msc_a = msc_conn_ref;
912 btw("sending CM Service Reject (%s) for %s, cause: %s",
913 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 +0100914 cm_service_result_sent |= RES_REJECT;
915 return 0;
916}
917
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100918static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100919 bool send_autn)
920{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100921 struct msc_a *msc_a = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100922 char *hex;
923 bool ok = true;
924 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 +0100925 send_autn? "UMTS" : "GSM", msc_a->c.fi->id,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100926 at->use_count, at->key_seq, at->vec.auth_types);
927
928 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
929 btw("...rand=%s", hex);
930 if (!auth_request_expect_rand
931 || strcmp(hex, auth_request_expect_rand) != 0) {
932 ok = false;
933 log("FAILURE: expected rand=%s",
934 auth_request_expect_rand ? auth_request_expect_rand : "-");
935 }
936
937 if (send_autn) {
938 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
939 btw("...autn=%s", hex);
940 if (!auth_request_expect_autn
941 || strcmp(hex, auth_request_expect_autn) != 0) {
942 ok = false;
943 log("FAILURE: expected autn=%s",
944 auth_request_expect_autn ? auth_request_expect_autn : "-");
945 }
946 } else if (auth_request_expect_autn) {
947 ok = false;
948 log("FAILURE: no AUTN sent, expected AUTN = %s",
949 auth_request_expect_autn);
950 }
951
952 if (send_autn)
953 btw("...expecting res=%s",
954 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
955 else
956 btw("...expecting sres=%s",
957 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
958
959 auth_request_sent = ok;
960 return 0;
961}
962
963static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
964{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100965 struct msc_a *msc_a = msc_conn_ref;
966 btw("sending Auth Reject for %s", msc_a->c.fi->id);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100967 return 0;
968}
969
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100970void ms_sends_ciphering_mode_complete(const char *inner_ran_msg)
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100971{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100972 struct ran_msg ran_dec;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100973
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100974 msc_a_get(msub_msc_a(g_msub), __func__);
975
976 ran_dec = (struct ran_msg){
977 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
978 };
979 fake_msc_a_ran_dec(&ran_dec);
980
981 if (inner_ran_msg) {
982 struct msgb *msg = msgb_from_hex("cipher_mode_complete_ran", 1024, inner_ran_msg);
983 msg->l1h = msg->l2h = msg->l3h = msg->data;
984 ran_dec = (struct ran_msg){
985 .msg_type = RAN_MSG_DTAP,
986 .dtap = msg,
987 };
988 patch_l3_seq_nr(msg);
989 fake_msc_a_ran_dec(&ran_dec);
990 msgb_free(msg);
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100991 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100992
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100993 msc_a_put(msub_msc_a(g_msub), __func__);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100994
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100995 if (!conn_exists(g_msub))
996 g_msub = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100997}
998
Alexander Couzens2aaff752021-10-19 17:09:11 +0200999void ms_sends_security_mode_complete(uint8_t utran_encryption)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001000{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001001 struct ran_msg ran_dec;
1002
1003 ran_dec = (struct ran_msg){
1004 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
Alexander Couzens2aaff752021-10-19 17:09:11 +02001005 .cipher_mode_complete.utran_encryption = utran_encryption,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001006 };
1007 fake_msc_a_ran_dec(&ran_dec);
1008
1009 if (!conn_exists(g_msub))
1010 g_msub = NULL;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001011}
1012
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +02001013void ms_sends_assignment_complete(enum mgcp_codecs assigned_codec)
1014{
1015 struct ran_msg ran_dec;
1016
1017 ran_dec = (struct ran_msg){
1018 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
1019 .assignment_complete = {
Pau Espin Pedrol9de384a2022-01-07 16:27:04 +01001020 .codec_present = true,
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +02001021 .codec = assigned_codec,
1022 },
1023 };
Neels Hofmeyr8159c952022-08-06 16:23:31 +02001024 osmo_sockaddr_str_from_str(&ran_dec.assignment_complete.remote_rtp, "1.2.3.4", 1234);
Neels Hofmeyr3f391dd2019-08-29 03:53:11 +02001025 fake_msc_a_ran_dec(&ran_dec);
1026
1027 if (!conn_exists(g_msub))
1028 g_msub = NULL;
1029}
1030
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001031void ran_sends_clear_complete()
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001032{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001033 struct ran_msg ran_dec;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001034
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001035 ran_dec = (struct ran_msg){
1036 .msg_type = RAN_MSG_CLEAR_COMPLETE,
1037 };
1038 fake_msc_a_ran_dec(&ran_dec);
1039
1040 if (!conn_exists(g_msub))
1041 g_msub = NULL;
Neels Hofmeyr4068ab22018-04-01 20:55:54 +02001042}
1043
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001044const struct timeval fake_time_start_time = { 123, 456 };
1045
1046void fake_time_start()
1047{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001048 struct timespec *clock_override;
1049
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001050 osmo_gettimeofday_override_time = fake_time_start_time;
1051 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +02001052 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
1053 OSMO_ASSERT(clock_override);
1054 clock_override->tv_sec = fake_time_start_time.tv_sec;
1055 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
1056 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001057 fake_time_passes(0, 0);
1058}
1059
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001060static struct {
1061 bool verbose;
1062 int run_test_nr;
1063} cmdline_opts = {
1064 .verbose = false,
1065 .run_test_nr = -1,
1066};
1067
1068static void print_help(const char *program)
1069{
1070 printf("Usage:\n"
1071 " %s [-v] [N [N...]]\n"
1072 "Options:\n"
1073 " -h --help show this text.\n"
1074 " -v --verbose print source file and line numbers\n"
1075 " N run only the Nth test (first test is N=1)\n",
1076 program
1077 );
1078}
1079
1080static void handle_options(int argc, char **argv)
1081{
1082 while (1) {
1083 int option_index = 0, c;
1084 static struct option long_options[] = {
1085 {"help", 0, 0, 'h'},
1086 {"verbose", 1, 0, 'v'},
1087 {0, 0, 0, 0}
1088 };
1089
1090 c = getopt_long(argc, argv, "hv",
1091 long_options, &option_index);
1092 if (c == -1)
1093 break;
1094
1095 switch (c) {
1096 case 'h':
1097 print_help(argv[0]);
1098 exit(0);
1099 case 'v':
1100 cmdline_opts.verbose = true;
1101 break;
1102 default:
1103 /* catch unknown options *as well as* missing arguments. */
1104 fprintf(stderr, "Error in command line options. Exiting.\n");
1105 exit(-1);
1106 break;
1107 }
1108 }
1109}
1110
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001111static void run_tests(int nr)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001112{
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001113 int test_nr;
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001114
Max5e2e9bd2018-02-06 19:31:08 +01001115 nr--; /* arg's first test is 1, in here it's 0 */
1116 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) {
Neels Hofmeyrdbb3c722021-02-07 23:53:07 +01001117 size_t talloc_blocks_before_test;
1118
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001119 if (nr >= 0 && test_nr != nr)
1120 continue;
1121
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001122 if (cmdline_opts.verbose)
1123 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
1124
Neels Hofmeyrdbb3c722021-02-07 23:53:07 +01001125 talloc_blocks_before_test = talloc_total_blocks(msc_vlr_tests_ctx);
1126
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001127 msc_vlr_tests[test_nr]();
1128
Neels Hofmeyrdbb3c722021-02-07 23:53:07 +01001129 if (talloc_total_blocks(msc_vlr_tests_ctx) != talloc_blocks_before_test) {
1130 fprintf(stderr, "ERROR: talloc leak: %zu blocks\n",
1131 talloc_total_blocks(msc_vlr_tests_ctx) - talloc_blocks_before_test);
1132 talloc_report_full(msc_vlr_tests_ctx, stderr);
1133 fprintf(stderr, "\n");
1134 }
1135
1136 if (talloc_total_blocks(msgb_ctx) > 1) {
1137 fprintf(stderr, "ERROR: msgb leak:\n");
1138 talloc_report_full(msgb_ctx, stderr);
1139 fprintf(stderr, "\n");
1140 }
1141
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001142 if (cmdline_opts.verbose)
1143 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
Max29ce08a2018-02-06 18:46:57 +01001144 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001145}
1146
Max5e60de62018-02-07 12:56:09 +01001147struct gsm_network *test_net(void *ctx)
1148{
Neels Hofmeyr7f484202018-02-27 12:59:45 +01001149 struct gsm_network *net = gsm_network_init(ctx, mncc_recv);
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +02001150 struct mgcp_client *client;
Max5e60de62018-02-07 12:56:09 +01001151
1152 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
1153 net->gsup_server_port = 0;
1154
1155 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
Max5e60de62018-02-07 12:56:09 +01001156 OSMO_ASSERT(net->vlr);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001157 OSMO_ASSERT(msc_gsup_client_start(net) == 0);
1158 OSMO_ASSERT(net->gcm);
1159 OSMO_ASSERT(msc_vlr_start(net) == 0);
Max5e60de62018-02-07 12:56:09 +01001160
1161 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
1162 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001163 net->vlr->ops.tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc;
Max5e60de62018-02-07 12:56:09 +01001164 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
1165 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
1166 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001167 net->vlr->ops.set_ciph_mode = msc_a_vlr_set_cipher_mode;
1168
1169 /* Allocate fake SCCP Ran Instances */
1170 net->a.sri = talloc_zero(net, struct sccp_ran_inst);
1171 *net->a.sri = (struct sccp_ran_inst){
1172 .ran = &test_ran_infra[OSMO_RAT_GERAN_A],
1173 };
1174 INIT_LLIST_HEAD(&net->a.sri->ran_peers);
1175 INIT_LLIST_HEAD(&net->a.sri->ran_conns);
1176
1177 net->iu.sri = talloc_zero(net, struct sccp_ran_inst);
1178 *net->iu.sri = (struct sccp_ran_inst){
1179 .ran = &test_ran_infra[OSMO_RAT_UTRAN_IU],
1180 };
1181 INIT_LLIST_HEAD(&net->iu.sri->ran_peers);
1182 INIT_LLIST_HEAD(&net->iu.sri->ran_conns);
1183
1184 net->mgw.tdefs = g_mgw_tdefs;
1185 mgcp_client_conf_init(&net->mgw.conf);
1186 net->mgw.tdefs = g_mgw_tdefs;
Pau Espin Pedrolb44cf2d2022-10-17 18:09:15 +02001187 net->mgw.mgw_pool = mgcp_client_pool_alloc(net);
1188 client = mgcp_client_init(net, &net->mgw.conf);
1189 mgcp_client_pool_register_single(net->mgw.mgw_pool, client);
Max5e60de62018-02-07 12:56:09 +01001190 return net;
1191}
1192
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001193int main(int argc, char **argv)
1194{
1195 handle_options(argc, argv);
1196
Neels Hofmeyre4f7e712019-03-24 21:07:33 +01001197 osmo_fsm_term_safely(true);
1198
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001199 msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx");
1200 msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0);
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001201 osmo_init_logging2(msc_vlr_tests_ctx, &info);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001202
1203 _log_lines = cmdline_opts.verbose;
1204
1205 OSMO_ASSERT(osmo_stderr_target);
1206 log_set_use_color(osmo_stderr_target, 0);
1207 log_set_print_timestamp(osmo_stderr_target, 0);
Pau Espin Pedrolcad22fd2021-02-19 13:37:00 +01001208 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001209 log_set_print_category(osmo_stderr_target, 1);
Pau Espin Pedrol8d02d992021-02-19 13:36:47 +01001210 log_set_print_category_hex(osmo_stderr_target, 0);
Max48131522019-01-16 12:47:39 +01001211 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
Philipp Maierce1f9732021-09-14 11:18:46 +02001212 log_set_category_filter(osmo_stderr_target, DLMGCP, 0, LOGL_NOTICE);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001213
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001214 if (cmdline_opts.verbose) {
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001215 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
1216 log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
1217 log_set_use_color(osmo_stderr_target, 1);
1218 log_set_print_level(osmo_stderr_target, 1);
1219 }
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +01001220
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001221 net = test_net(msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001222
1223 osmo_fsm_log_addr(false);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001224 osmo_fsm_log_timeouts(cmdline_opts.verbose);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001225
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001226 call_leg_init(net);
1227
1228 OSMO_ASSERT(osmo_fsm_register(&dummy_msc_i_fsm) == 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001229
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001230 clear_vlr();
1231
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001232 if (optind >= argc)
1233 run_tests(-1);
1234 else {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001235 int arg;
1236 long int nr;
1237 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +01001238 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001239 nr = strtol(argv[arg], NULL, 10);
1240 if (errno) {
1241 fprintf(stderr, "Invalid argument: %s\n",
1242 argv[arg]);
1243 exit(1);
1244 }
1245
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001246 run_tests(nr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001247 }
1248 }
1249
1250 printf("Done\n");
1251
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001252 return 0;
1253}