blob: 4a17789832f86b86490f2853f3f6f2d8b10f7b05 [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>
Harald Welte1ea6baf2018-07-31 19:40:52 +020033#include <osmocom/gsupclient/gsup_client.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 Hofmeyr3117b702018-09-13 03:23:07 +020038#include <osmocom/msc/a_iface_bssap.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020039
40#if BUILD_IU
Neels Hofmeyr90843962017-09-04 15:04:35 +020041#include <osmocom/msc/iucs_ranap.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020042#include <osmocom/ranap/iu_client.h>
43#else
Neels Hofmeyr90843962017-09-04 15:04:35 +020044#include <osmocom/msc/iu_dummy.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020045#endif
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010046
47#include "msc_vlr_tests.h"
48
Neels Hofmeyrc01e9092018-03-22 15:56:49 +010049void *msc_vlr_tests_ctx = NULL;
50
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010051bool _log_lines = false;
52
53struct gsm_network *net = NULL;
54
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010055const char *gsup_tx_expected = NULL;
56bool gsup_tx_confirmed;
57
58struct msgb *dtap_tx_expected = NULL;
59bool dtap_tx_confirmed;
60
61enum result_sent lu_result_sent;
62enum result_sent cm_service_result_sent;
63bool auth_request_sent;
64const char *auth_request_expect_rand;
65const char *auth_request_expect_autn;
66bool cipher_mode_cmd_sent;
67bool cipher_mode_cmd_sent_with_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +010068const char *cipher_mode_expect_kc;
69bool security_mode_ctrl_sent;
70const char *security_mode_expect_ck;
71const char *security_mode_expect_ik;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010072
Philipp Maierfbf66102017-04-09 12:32:51 +020073bool iu_release_expected = false;
74bool iu_release_sent = false;
75bool bssap_clear_expected = false;
76bool bssap_clear_sent = false;
77
Neels Hofmeyra99b4272017-11-21 17:13:23 +010078uint32_t cc_to_mncc_tx_expected_msg_type = 0;
79const char *cc_to_mncc_tx_expected_imsi = NULL;
80bool cc_to_mncc_tx_confirmed = false;
81uint32_t cc_to_mncc_tx_got_callref = 0;
82
Harald Weltec2007852018-02-03 21:08:26 +010083extern int gsm0407_pdisc_ctr_bin(uint8_t pdisc);
84
85/* static state variables for the L3 send sequence numbers */
86static uint8_t n_sd[4];
87
88/* patch a correct send sequence number into the given message */
89static void patch_l3_seq_nr(struct msgb *msg)
90{
91 struct gsm48_hdr *gh = msgb_l3(msg);
92 uint8_t pdisc = gsm48_hdr_pdisc(gh);
93 uint8_t *msg_type_oct = &msg->l3h[1];
94 int bin = gsm0407_pdisc_ctr_bin(pdisc);
95
96 if (bin >= 0 && bin < ARRAY_SIZE(n_sd)) {
97 /* patch in n_sd into the msg_type octet */
98 *msg_type_oct = (*msg_type_oct & 0x3f) | ((n_sd[bin] & 0x3) << 6);
99 //fprintf(stderr, "pdisc=0x%02x bin=%d, patched n_sd=%u\n\n", pdisc, bin, n_sd[bin] & 3);
100 /* increment N(SD) */
101 n_sd[bin] = (n_sd[bin] + 1) % 4;
102 } else {
103 //fprintf(stderr, "pdisc=0x%02x NO SEQ\n\n", pdisc);
104 }
105}
106
107/* reset L3 sequence numbers (e.g. new RR connection) */
108static void reset_l3_seq_nr()
109{
110 memset(n_sd, 0, sizeof(n_sd));
111}
112
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100113struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
114{
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200115 struct msgb *msg = msgb_alloc_headroom(size, 4, label);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100116 unsigned char *rc;
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200117 msg->l2h = msg->data;
118 rc = msgb_put(msg, osmo_hexparse(hex, msg->data, msgb_tailroom(msg)));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100119 OSMO_ASSERT(rc == msg->l2h);
120 return msg;
121}
122
Maxd8d1a9e2018-02-06 15:50:20 +0100123static const char *gh_type_name(struct gsm48_hdr *gh)
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100124{
125 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
126 gsm48_hdr_msg_type(gh));
127}
128
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100129void dtap_expect_tx(const char *hex)
130{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200131 /* Has the previously expected dtap been received? */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100132 OSMO_ASSERT(!dtap_tx_expected);
133 if (!hex)
134 return;
135 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
Harald Weltec2007852018-02-03 21:08:26 +0100136 /* Mask the sequence number out */
137 if (msgb_length(dtap_tx_expected) >= 2)
138 dtap_tx_expected->data[1] &= 0x3f;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100139 dtap_tx_confirmed = false;
140}
141
Harald Welte1ea6baf2018-07-31 19:40:52 +0200142int vlr_gsupc_read_cb(struct osmo_gsup_client *gsupc, struct msgb *msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100143
144void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
145{
146 int rc;
147 struct msgb *msg;
148 const char *label;
149
150 gsup_expect_tx(expect_tx_hex);
151
152 msg = msgb_from_hex("gsup", 1024, rx_hex);
153 label = osmo_gsup_message_type_name(msg->l2h[0]);
154 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
155 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
Neels Hofmeyr834f94a2017-09-28 03:07:16 +0200156 /* GSUP read cb takes ownership of msgb */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100157 rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg);
158 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
159 label, rc);
160 if (expect_tx_hex)
161 OSMO_ASSERT(gsup_tx_confirmed);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100162}
163
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100164bool conn_exists(const struct ran_conn *conn)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100165{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100166 struct ran_conn *c;
Maxba8a0072018-10-25 17:58:43 +0200167
168 if (!conn)
169 return false;
170
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100171 llist_for_each_entry(c, &net->ran_conns, entry) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100172 if (c == conn)
173 return true;
174 }
Maxba8a0072018-10-25 17:58:43 +0200175
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100176 return false;
177}
178
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700179/* Simplified version of the cm_service_request_concludes() */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100180void conn_conclude_cm_service_req(struct ran_conn *conn,
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100181 enum osmo_rat_type via_ran)
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700182{
183 btw("Concluding CM Service Request");
184
185 OSMO_ASSERT(conn);
186 OSMO_ASSERT(conn->received_cm_service_request);
187
188 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100189 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Vadim Yanitskiy27605852018-06-15 23:57:30 +0700190
191 ASSERT_RELEASE_CLEAR(via_ran);
192}
193
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100194enum osmo_rat_type rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100195
Harald Welte977b5482019-02-18 12:23:43 +0100196/* SCCP user stub to make a_iface_tx_bssap() happy during test case execution */
197struct osmo_sccp_user {
198 uint8_t foo;
199};
200static struct osmo_sccp_user g_scu;
201
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100202struct ran_conn *conn_new(void)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100203{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100204 struct ran_conn *conn;
205 conn = ran_conn_alloc(net, rx_from_ran, 23);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100206 if (conn->via_ran == OSMO_RAT_UTRAN_IU) {
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200207 struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx);
208 *ue_ctx = (struct ranap_ue_conn_ctx){
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200209 .conn_id = 42,
210 };
211 conn->iu.ue_ctx = ue_ctx;
Harald Welte977b5482019-02-18 12:23:43 +0100212 } else {
213 conn->a.scu = &g_scu;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200214 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100215 return conn;
216}
217
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100218struct ran_conn *g_conn = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100219
220void rx_from_ms(struct msgb *msg)
221{
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100222 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100223
224 log("MSC <--%s-- MS: %s",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100225 osmo_rat_type_name(rx_from_ran),
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100226 gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100227
Maxba8a0072018-10-25 17:58:43 +0200228 if (!conn_exists(g_conn))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100229 g_conn = NULL;
230
231 if (!g_conn) {
232 log("new conn");
233 g_conn = conn_new();
Harald Weltec2007852018-02-03 21:08:26 +0100234 reset_l3_seq_nr();
235 patch_l3_seq_nr(msg);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100236 ran_conn_compl_l3(g_conn, msg, 23);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100237 } else {
Harald Weltec2007852018-02-03 21:08:26 +0100238 patch_l3_seq_nr(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100239 if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR)
240 && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL))
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100241 ran_conn_cipher_mode_compl(g_conn, msg, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100242 else
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100243 ran_conn_dtap(g_conn, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100244 }
245
Maxba8a0072018-10-25 17:58:43 +0200246 if (!conn_exists(g_conn))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100247 g_conn = NULL;
248}
249
250void ms_sends_msg(const char *hex)
251{
252 struct msgb *msg;
253
254 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
255 msg->l1h = msg->l2h = msg->l3h = msg->data;
256 rx_from_ms(msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200257 msgb_free(msg);
258}
259
260void bss_sends_bssap_mgmt(const char *hex)
261{
262 struct msgb *msg;
263 struct bssmap_header *bh;
264 struct a_conn_info a_conn_info;
265
266 msg = msgb_from_hex("bss_sends_bssap_mgmt", 1024, hex);
267 msg->l3h = msg->data;
268
269 msg->l2h = msgb_push(msg, sizeof(*bh));
270 bh = (void*)msg->l2h;
271 bh->type = BSSAP_MSG_BSS_MANAGEMENT;
272 bh->length = msgb_l3len(msg);
273
Maxba8a0072018-10-25 17:58:43 +0200274 if (!conn_exists(g_conn))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200275 g_conn = NULL;
276
277 OSMO_ASSERT(g_conn);
278 a_conn_info.network = net;
279 a_conn_info.conn_id = g_conn->a.conn_id;
280
281 a_sccp_rx_dt((struct osmo_sccp_user*)0x1, &a_conn_info, msg);
282 msgb_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100283}
284
Maxd8d1a9e2018-02-06 15:50:20 +0100285static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100286{
287 int rc;
288 struct msgb *msg;
289 struct gsm48_hdr *gh;
290
291 msg = msgb_alloc(1024, "ms_sends_msg_fake");
292 msg->l1h = msg->l2h = msg->l3h = msg->data;
293
294 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
295 gh->proto_discr = pdisc;
296 gh->msg_type = msg_type;
297 /* some amount of data, whatever */
298 msgb_put(msg, 123);
299
Harald Weltec2007852018-02-03 21:08:26 +0100300 patch_l3_seq_nr(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100301 rc = gsm0408_dispatch(g_conn, msg);
302
303 talloc_free(msg);
304 return rc;
305}
306
Max68171902018-02-06 17:55:19 +0100307static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype)
308{
309 int rc = ms_sends_msg_fake(val, msgtype);
310 if (rc != -EACCES)
311 log("Unexpected return value %u != %u for %s/%s",
312 -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype));
313}
314
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100315void thwart_rx_non_initial_requests()
316{
317 log("requests shall be thwarted");
Max68171902018-02-06 17:55:19 +0100318
319 ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP);
320 ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */
321 ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1);
322 ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100323}
324
325void send_sms(struct vlr_subscr *receiver,
326 struct vlr_subscr *sender,
327 char *str)
328{
Harald Welte39b55482018-04-09 19:19:33 +0200329 struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str);
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700330 gsm411_send_sms(net, receiver, sms);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100331}
332
333unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100334/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
335int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
336int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100337{
Max753c15d2017-12-21 14:50:44 +0100338 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100339 for (i = 0; i < num; i++)
340 buf[i] = next_rand_byte++;
341 return 1;
342}
343
344/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
345void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
346void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
347{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200348 /* Write fixed time bytes for deterministic test results */
349 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100350}
351
352const char *paging_expecting_imsi = NULL;
353uint32_t paging_expecting_tmsi;
354bool paging_sent;
355bool paging_stopped;
356
357void paging_expect_imsi(const char *imsi)
358{
359 paging_expecting_imsi = imsi;
360 paging_expecting_tmsi = GSM_RESERVED_TMSI;
361}
362
363void paging_expect_tmsi(uint32_t tmsi)
364{
365 paging_expecting_tmsi = tmsi;
366 paging_expecting_imsi = NULL;
367}
368
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100369static int _paging_sent(enum osmo_rat_type via_ran, const char *imsi, uint32_t tmsi, uint32_t lac)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100370{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200371 log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100372 osmo_rat_type_name(via_ran), imsi, tmsi, lac);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100373 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
374 if (paging_expecting_imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200375 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d");
376 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
377 VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x");
378 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100379 paging_sent = true;
380 paging_stopped = false;
381 return 1;
382}
383
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200384/* override, requires '-Wl,--wrap=ranap_iu_page_cs' */
385int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
386int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200387{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100388 return _paging_sent(OSMO_RAT_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200389}
390
Philipp Maierfbf66102017-04-09 12:32:51 +0200391/* override, requires '-Wl,--wrap=a_iface_tx_paging' */
392int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac);
393int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200394{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100395 return _paging_sent(OSMO_RAT_GERAN_A, imsi, tmsi, lac);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200396}
397
398/* override, requires '-Wl,--wrap=msc_stop_paging' */
399void __real_msc_stop_paging(struct vlr_subscr *vsub);
400void __wrap_msc_stop_paging(struct vlr_subscr *vsub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100401{
402 paging_stopped = true;
403}
404
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200405
406/* override, requires '-Wl,--wrap=osmo_sccp_tx_data_msg' */
407int __real_osmo_sccp_tx_data_msg(struct osmo_sccp_user *scu, uint32_t conn_id,
408 struct msgb *msg);
409int __wrap_osmo_sccp_tx_data_msg(struct osmo_sccp_user *scu, uint32_t conn_id,
410 struct msgb *msg)
411{
412 const char *proto_str;
413 const char *msg_str = gsm0808_bssmap_name(msg->l3h[2]);
414 switch (*msg->l3h) {
415 case BSSAP_MSG_BSS_MANAGEMENT:
416 proto_str = "BSSAP-BSS-MANAGEMENT";
417 break;
418 case BSSAP_MSG_DTAP:
419 proto_str = "BSSAP-DTAP";
420 break;
421 default:
422 proto_str = "";
423 msg_str = "";
424 break;
425 }
426
427 log("BSC <--%s-- MSC: %s %s", proto_str, msg_str, msgb_hexdump(msg));
428 msgb_free(msg);
429 return 0;
430}
431
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100432void clear_vlr()
433{
434 struct vlr_subscr *vsub, *n;
435 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
436 vlr_subscr_free(vsub);
437 }
438
439 net->authentication_required = false;
Harald Welte7b222aa2017-12-23 19:30:32 +0100440 net->a5_encryption_mask = (1 << 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100441 net->vlr->cfg.check_imei_rqd = false;
442 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200443 net->vlr->cfg.retrieve_imeisv_early = false;
444 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100445 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
446 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100447
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100448 rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100449 auth_request_sent = false;
450 auth_request_expect_rand = NULL;
451 auth_request_expect_autn = NULL;
452
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100453 cipher_mode_cmd_sent = false;
454 cipher_mode_cmd_sent_with_imeisv = false;
455 cipher_mode_expect_kc = NULL;
456
457 security_mode_ctrl_sent = false;
458 security_mode_expect_ck = NULL;
459 security_mode_expect_ik = NULL;
460
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100461 next_rand_byte = 0;
462
Philipp Maierfbf66102017-04-09 12:32:51 +0200463 iu_release_expected = false;
464 iu_release_sent = false;
465 bssap_clear_expected = false;
466 bssap_clear_sent = false;
467
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100468 osmo_gettimeofday_override = false;
469}
470
471static struct log_info_cat test_categories[] = {
472 [DMSC] = {
473 .name = "DMSC",
474 .description = "Mobile Switching Center",
475 .enabled = 1, .loglevel = LOGL_DEBUG,
476 },
477 [DRLL] = {
478 .name = "DRLL",
479 .description = "A-bis Radio Link Layer (RLL)",
480 .enabled = 1, .loglevel = LOGL_DEBUG,
481 },
482 [DMM] = {
483 .name = "DMM",
484 .description = "Layer3 Mobility Management (MM)",
485 .enabled = 1, .loglevel = LOGL_DEBUG,
486 },
487 [DRR] = {
488 .name = "DRR",
489 .description = "Layer3 Radio Resource (RR)",
490 .enabled = 1, .loglevel = LOGL_DEBUG,
491 },
492 [DCC] = {
493 .name = "DCC",
494 .description = "Layer3 Call Control (CC)",
Neels Hofmeyr12e17be2018-03-12 23:59:37 +0100495 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100496 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100497 [DVLR] = {
498 .name = "DVLR",
499 .description = "Visitor Location Register",
500 .enabled = 1, .loglevel = LOGL_DEBUG,
501 },
502 [DREF] = {
503 .name = "DREF",
504 .description = "Reference Counting",
505 .enabled = 1, .loglevel = LOGL_DEBUG,
506 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200507 [DPAG] = {
508 .name = "DPAG",
509 .description = "Paging Subsystem",
510 .enabled = 1, .loglevel = LOGL_DEBUG,
511 },
512 [DIUCS] = {
513 .name = "DIUCS",
514 .description = "Iu-CS Protocol",
515 .enabled = 1, .loglevel = LOGL_DEBUG,
516 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100517 [DMNCC] = {
518 .name = "DMNCC",
519 .description = "MNCC API for Call Control application",
520 .enabled = 1, .loglevel = LOGL_DEBUG,
521 },
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100522 [DBSSAP] = {
523 .name = "DBSSAP",
524 .description = "BSSAP Protocol (A Interface)",
525 .enabled = 1, .loglevel = LOGL_DEBUG,
526 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100527};
528
529static struct log_info info = {
530 .cat = test_categories,
531 .num_cat = ARRAY_SIZE(test_categories),
532};
533
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100534int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100535{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100536 struct gsm_mncc *mncc = (void*)msg->data;
537 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
538 get_mncc_name(mncc->msg_type));
539
540 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
541 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
542 log("Mismatch! Expected MNCC msg type: %s",
543 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
544 abort();
545 }
546
547 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
548 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
549 cc_to_mncc_tx_expected_imsi,
550 mncc->imsi);
551 abort();
552 }
553
554 cc_to_mncc_tx_confirmed = true;
555 cc_to_mncc_tx_got_callref = mncc->callref;
556 cc_to_mncc_tx_expected_imsi = NULL;
557 cc_to_mncc_tx_expected_msg_type = 0;
558 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100559 return 0;
560}
561
Harald Welte1ea6baf2018-07-31 19:40:52 +0200562struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100563__real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
564 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
565 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200566struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100567__wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
568 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
569 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100570{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200571 struct osmo_gsup_client *gsupc;
572 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100573 OSMO_ASSERT(gsupc);
574 return gsupc;
575}
576
577/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200578int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
579int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100580{
Max34d306b2019-01-15 18:37:31 +0100581 uint8_t buf[512];
582 int len;
583
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100584 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
Max34d306b2019-01-15 18:37:31 +0100585 osmo_gsup_message_type_name(msg->data[0]), osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100586
587 OSMO_ASSERT(gsup_tx_expected);
Max34d306b2019-01-15 18:37:31 +0100588 OSMO_ASSERT(strlen(gsup_tx_expected) <= (sizeof(buf) * 2));
589
590 len = osmo_hexparse(gsup_tx_expected, buf, sizeof(buf));
591 if (len < 1)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100592 abort();
Max34d306b2019-01-15 18:37:31 +0100593
594 if (!msgb_eq_data_print(msg, buf, len))
595 abort();
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100596
597 talloc_free(msg);
598 gsup_tx_confirmed = true;
599 gsup_tx_expected = NULL;
600 return 0;
601}
602
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100603static int _validate_dtap(struct msgb *msg, enum osmo_rat_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100604{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100605 btw("DTAP --%s--> MS: %s: %s",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100606 osmo_rat_type_name(to_ran), gh_type_name((void*)msg->data),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200607 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100608
609 OSMO_ASSERT(dtap_tx_expected);
Harald Weltec2007852018-02-03 21:08:26 +0100610
611 /* Mask the sequence number out before comparing */
612 msg->data[1] &= 0x3f;
Max34d306b2019-01-15 18:37:31 +0100613 if (!msgb_eq_data_print(msg, dtap_tx_expected->data, dtap_tx_expected->len))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100614 abort();
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100615
616 btw("DTAP matches expected message");
617
618 talloc_free(msg);
619 dtap_tx_confirmed = true;
620 talloc_free(dtap_tx_expected);
621 dtap_tx_expected = NULL;
622 return 0;
623}
624
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200625/* override, requires '-Wl,--wrap=ranap_iu_tx' */
626int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
627int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200628{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100629 return _validate_dtap(msg, OSMO_RAT_UTRAN_IU);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200630}
631
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200632/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
633int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
634int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200635{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100636 btw("Iu Release --%s--> MS", osmo_rat_type_name(OSMO_RAT_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200637 OSMO_ASSERT(iu_release_expected);
638 iu_release_expected = false;
639 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200640 return 0;
641}
642
643/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200644int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
645int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200646{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100647 btw("Iu Common ID --%s--> MS (IMSI=%s)", osmo_rat_type_name(OSMO_RAT_UTRAN_IU), imsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200648 return 0;
649}
650
Philipp Maierfbf66102017-04-09 12:32:51 +0200651/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
652int __real_a_iface_tx_dtap(struct msgb *msg);
653int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200654{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100655 return _validate_dtap(msg, OSMO_RAT_GERAN_A);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200656}
657
Philipp Maierfbf66102017-04-09 12:32:51 +0200658/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100659int __real_a_iface_tx_clear_cmd(struct ran_conn *conn);
660int __wrap_a_iface_tx_clear_cmd(struct ran_conn *conn)
Philipp Maierfbf66102017-04-09 12:32:51 +0200661{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100662 btw("BSSAP Clear --%s--> MS", osmo_rat_type_name(OSMO_RAT_GERAN_A));
Philipp Maierfbf66102017-04-09 12:32:51 +0200663 OSMO_ASSERT(bssap_clear_expected);
664 bssap_clear_expected = false;
665 bssap_clear_sent = true;
666 return 0;
667}
668
Neels Hofmeyrb16259f2018-12-20 02:57:56 +0100669/* override, requires '-Wl,--wrap=msc_mgcp_try_call_assignment' */
670int __real_msc_mgcp_try_call_assignment(struct gsm_trans *trans);
671int __wrap_msc_mgcp_try_call_assignment(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100672{
673 log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x",
674 vlr_subscr_name(trans->vsub), trans->callref);
675 return 0;
676}
677
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100678struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
679
Philipp Maier621ba032017-11-07 17:19:25 +0100680/* override, requires '-Wl,--wrap=msc_mgcp_call_release' */
681void __real_msc_mgcp_call_release(struct gsm_trans *trans);
682void __wrap_msc_mgcp_call_release(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100683{
684 log("MS <--Call Release-- MSC: subscr=%s callref=0x%x",
685 vlr_subscr_name(trans->vsub), trans->callref);
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100686 if (on_call_release_mncc_sends_to_cc_data) {
687 mncc_tx_to_cc(trans->net, on_call_release_mncc_sends_to_cc_data->msg_type,
688 on_call_release_mncc_sends_to_cc_data);
689 on_call_release_mncc_sends_to_cc_data = NULL;
690 }
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100691}
692
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100693static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
694{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100695 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100696 if (send_tmsi == GSM_RESERVED_TMSI)
697 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
698 else
699 btw("sending LU Accept for %s, with TMSI 0x%08x",
700 vlr_subscr_name(conn->vsub), send_tmsi);
701 lu_result_sent |= RES_ACCEPT;
702 return 0;
703}
704
Neels Hofmeyr15809592018-04-06 02:57:51 +0200705static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100706{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100707 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100708 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
709 lu_result_sent |= RES_REJECT;
710 return 0;
711}
712
713static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
714{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100715 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100716 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
717 cm_service_result_sent |= RES_ACCEPT;
718 return 0;
719}
720
Neels Hofmeyr15809592018-04-06 02:57:51 +0200721static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100722{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100723 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200724 btw("sending CM Service Reject for %s, cause: %s",
725 vlr_subscr_name(conn->vsub), gsm48_reject_value_name(cause));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100726 cm_service_result_sent |= RES_REJECT;
727 return 0;
728}
729
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100730static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100731 bool send_autn)
732{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100733 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100734 char *hex;
735 bool ok = true;
736 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
737 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
738 at->use_count, at->key_seq, at->vec.auth_types);
739
740 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
741 btw("...rand=%s", hex);
742 if (!auth_request_expect_rand
743 || strcmp(hex, auth_request_expect_rand) != 0) {
744 ok = false;
745 log("FAILURE: expected rand=%s",
746 auth_request_expect_rand ? auth_request_expect_rand : "-");
747 }
748
749 if (send_autn) {
750 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
751 btw("...autn=%s", hex);
752 if (!auth_request_expect_autn
753 || strcmp(hex, auth_request_expect_autn) != 0) {
754 ok = false;
755 log("FAILURE: expected autn=%s",
756 auth_request_expect_autn ? auth_request_expect_autn : "-");
757 }
758 } else if (auth_request_expect_autn) {
759 ok = false;
760 log("FAILURE: no AUTN sent, expected AUTN = %s",
761 auth_request_expect_autn);
762 }
763
764 if (send_autn)
765 btw("...expecting res=%s",
766 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
767 else
768 btw("...expecting sres=%s",
769 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
770
771 auth_request_sent = ok;
772 return 0;
773}
774
775static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
776{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100777 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100778 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
779 return 0;
780}
781
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100782/* override, requires '-Wl,--wrap=a_iface_tx_cipher_mode' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100783int __real_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100784 struct gsm0808_encrypt_info *ei, int include_imeisv);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100785int __wrap_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100786 struct gsm0808_encrypt_info *ei, int include_imeisv)
787{
788 int i;
789 btw("sending Ciphering Mode Command for %s: include_imeisv=%d",
790 vlr_subscr_name(conn->vsub), include_imeisv);
791 for (i = 0; i < ei->perm_algo_len; i++)
Neels Hofmeyrd28ea6c2018-09-17 00:54:52 +0200792 btw("...perm algo: A5/%u", ei->perm_algo[i] - 1);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100793 OSMO_ASSERT(ei->key_len <= sizeof(ei->key));
794 btw("...key: %s", osmo_hexdump_nospc(ei->key, ei->key_len));
795 cipher_mode_cmd_sent = true;
796 cipher_mode_cmd_sent_with_imeisv = include_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100797
798 if (!cipher_mode_expect_kc
799 || strcmp(cipher_mode_expect_kc, osmo_hexdump_nospc(ei->key, ei->key_len))) {
800 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
801 OSMO_ASSERT(false);
802 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100803 return 0;
804}
805
806/* override, requires '-Wl,--wrap=ranap_iu_tx_sec_mode_cmd' */
807int __real_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
808 int send_ck, int new_key);
809int __wrap_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
810 int send_ck, int new_key)
811{
812 btw("sending SecurityModeControl for UE ctx %u send_ck=%d new_key=%d",
813 uectx->conn_id, send_ck, new_key);
814 btw("...ik=%s", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
815 if (send_ck)
816 btw("...ck=%s", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100817 security_mode_ctrl_sent = true;
818 if (!security_mode_expect_ik
819 || strcmp(security_mode_expect_ik, osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)))) {
820 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
821 OSMO_ASSERT(false);
822 }
823 if (((!!send_ck) != (!!security_mode_expect_ck))
824 || (security_mode_expect_ck
825 && strcmp(security_mode_expect_ck, osmo_hexdump_nospc(vec->ck, sizeof(vec->ck))))) {
826 log("FAILURE: expected ck=%s", security_mode_expect_ck ? : "NULL");
827 OSMO_ASSERT(false);
828 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100829 return 0;
830}
831
832extern int msc_vlr_set_ciph_mode(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv);
833
Harald Welte71c51df2017-12-23 18:51:48 +0100834static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100835{
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100836 int rc;
837#ifndef BUILD_IU
838 /* If we built without support for IU, fake the IU part here. The root cause is that we don't
839 * have differing sets of expected outputs for --enable-iu and --disable-iu. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100840 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100841 if (conn->via_ran == OSMO_RAT_UTRAN_IU) {
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100842 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n", vlr_subscr_name(conn->vsub));
843 rc = __wrap_ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &conn->vsub->last_tuple->vec,
844 0, 1);
845 } else
846#endif
847 rc = msc_vlr_set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
848 if (rc)
849 btw("ERROR sending ciphering mode command: rc=%d", rc);
850 return rc;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100851}
852
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200853void ms_sends_security_mode_complete()
854{
855 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100856 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200857 OSMO_ASSERT(g_conn->iu.ue_ctx);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100858 ran_conn_rx_sec_mode_compl(g_conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200859}
860
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200861void bss_sends_clear_complete()
862{
863 btw("BSS sends BSSMAP Clear Complete");
864 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100865 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_GERAN_A);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100866 ran_conn_rx_bssmap_clear_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200867}
868
869void rnc_sends_release_complete()
870{
871 btw("RNC sends Iu Release Complete");
872 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100873 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100874 ran_conn_rx_iu_release_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200875}
876
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100877const struct timeval fake_time_start_time = { 123, 456 };
878
879void fake_time_start()
880{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200881 struct timespec *clock_override;
882
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100883 osmo_gettimeofday_override_time = fake_time_start_time;
884 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200885 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
886 OSMO_ASSERT(clock_override);
887 clock_override->tv_sec = fake_time_start_time.tv_sec;
888 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
889 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100890 fake_time_passes(0, 0);
891}
892
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200893static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100894{
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200895 /* Verifying that the msgb context is empty */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100896 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100897 /* Expecting these to stick around in msc_vlr_tests_ctx:
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100898 * talloc_total_blocks(tall_bsc_ctx) == 13
899 * full talloc report on 'msc_vlr_tests_ctx' (total 4638 bytes in 13 blocks)
900 * struct osmo_gsup_client contains 256 bytes in 1 blocks (ref 0) 0x61300000dd20
901 * struct gsm_network contains 2983 bytes in 5 blocks (ref 0) 0x61400000fea0
902 * struct vlr_instance contains 320 bytes in 2 blocks (ref 0) 0x61300000dee0
903 * struct ipaccess_unit contains 64 bytes in 1 blocks (ref 0) 0x60e0000244c0
904 * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x60b00000af40
905 * rate_ctr.c:234 contains 2352 bytes in 1 blocks (ref 0) 0x61e00000f0e0
906 * logging contains 1399 bytes in 5 blocks (ref 0) 0x60b00000aff0
907 * struct log_target contains 238 bytes in 2 blocks (ref 0) 0x61200000bf20
908 * struct log_category contains 70 bytes in 1 blocks (ref 0) 0x60f00000efb0
909 * struct log_info contains 1160 bytes in 2 blocks (ref 0) 0x60d00000cfd0
910 * struct log_info_cat contains 1120 bytes in 1 blocks (ref 0) 0x61a00001f2e0
911 * msgb contains 0 bytes in 1 blocks (ref 0) 0x60800000bf80
912 * (That's 13 counting the root ctx)
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200913 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100914 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100915 talloc_total_blocks(msc_vlr_tests_ctx));
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100916 if (talloc_total_blocks(msc_vlr_tests_ctx) != 13)
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100917 talloc_report_full(msc_vlr_tests_ctx, stderr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100918 fprintf(stderr, "\n");
919}
920
921static struct {
922 bool verbose;
923 int run_test_nr;
924} cmdline_opts = {
925 .verbose = false,
926 .run_test_nr = -1,
927};
928
929static void print_help(const char *program)
930{
931 printf("Usage:\n"
932 " %s [-v] [N [N...]]\n"
933 "Options:\n"
934 " -h --help show this text.\n"
935 " -v --verbose print source file and line numbers\n"
936 " N run only the Nth test (first test is N=1)\n",
937 program
938 );
939}
940
941static void handle_options(int argc, char **argv)
942{
943 while (1) {
944 int option_index = 0, c;
945 static struct option long_options[] = {
946 {"help", 0, 0, 'h'},
947 {"verbose", 1, 0, 'v'},
948 {0, 0, 0, 0}
949 };
950
951 c = getopt_long(argc, argv, "hv",
952 long_options, &option_index);
953 if (c == -1)
954 break;
955
956 switch (c) {
957 case 'h':
958 print_help(argv[0]);
959 exit(0);
960 case 'v':
961 cmdline_opts.verbose = true;
962 break;
963 default:
964 /* catch unknown options *as well as* missing arguments. */
965 fprintf(stderr, "Error in command line options. Exiting.\n");
966 exit(-1);
967 break;
968 }
969 }
970}
971
972void *msgb_ctx = NULL;
973
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100974static void run_tests(int nr)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100975{
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100976 int test_nr;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200977
978 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
979
Max5e2e9bd2018-02-06 19:31:08 +0100980 nr--; /* arg's first test is 1, in here it's 0 */
981 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100982 if (nr >= 0 && test_nr != nr)
983 continue;
984
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100985 if (cmdline_opts.verbose)
986 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
987
988 msc_vlr_tests[test_nr]();
989
990 if (cmdline_opts.verbose)
991 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100992
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200993 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Max29ce08a2018-02-06 18:46:57 +0100994 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100995}
996
Max5e60de62018-02-07 12:56:09 +0100997struct gsm_network *test_net(void *ctx)
998{
Neels Hofmeyr7f484202018-02-27 12:59:45 +0100999 struct gsm_network *net = gsm_network_init(ctx, mncc_recv);
Max5e60de62018-02-07 12:56:09 +01001000
1001 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
1002 net->gsup_server_port = 0;
1003
1004 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
1005 OSMO_ASSERT(msc_vlr_start(net) == 0);
1006 OSMO_ASSERT(net->vlr);
1007 OSMO_ASSERT(net->vlr->gsup_client);
1008
1009 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
1010 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
1011 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
1012 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
1013 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
1014 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
1015 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
1016
1017 return net;
1018}
1019
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001020int main(int argc, char **argv)
1021{
1022 handle_options(argc, argv);
1023
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001024 msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx");
1025 msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0);
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001026 osmo_init_logging2(msc_vlr_tests_ctx, &info);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001027
1028 _log_lines = cmdline_opts.verbose;
1029
1030 OSMO_ASSERT(osmo_stderr_target);
1031 log_set_use_color(osmo_stderr_target, 0);
1032 log_set_print_timestamp(osmo_stderr_target, 0);
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001033 log_set_print_filename(osmo_stderr_target, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001034 log_set_print_category(osmo_stderr_target, 1);
Max48131522019-01-16 12:47:39 +01001035 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001036
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001037 if (cmdline_opts.verbose) {
Neels Hofmeyr39cb0dd2018-12-18 02:30:18 +01001038 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
1039 log_set_print_filename_pos(osmo_stderr_target, LOG_FILENAME_POS_LINE_END);
1040 log_set_use_color(osmo_stderr_target, 1);
1041 log_set_print_level(osmo_stderr_target, 1);
1042 }
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +01001043
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001044 net = test_net(msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001045
1046 osmo_fsm_log_addr(false);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001047
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001048 ran_conn_init();
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001049
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001050 clear_vlr();
1051
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001052 if (optind >= argc)
1053 run_tests(-1);
1054 else {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001055 int arg;
1056 long int nr;
1057 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +01001058 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001059 nr = strtol(argv[arg], NULL, 10);
1060 if (errno) {
1061 fprintf(stderr, "Invalid argument: %s\n",
1062 argv[arg]);
1063 exit(1);
1064 }
1065
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001066 run_tests(nr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001067 }
1068 }
1069
1070 printf("Done\n");
1071
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001072 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001073 return 0;
1074}