blob: 39d7e4235b782278cda4cff05ef1fd031f281ec0 [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
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100196struct ran_conn *conn_new(void)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100197{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100198 struct ran_conn *conn;
199 conn = ran_conn_alloc(net, rx_from_ran, 23);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100200 if (conn->via_ran == OSMO_RAT_UTRAN_IU) {
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200201 struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx);
202 *ue_ctx = (struct ranap_ue_conn_ctx){
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200203 .conn_id = 42,
204 };
205 conn->iu.ue_ctx = ue_ctx;
206 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100207 return conn;
208}
209
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100210struct ran_conn *g_conn = NULL;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100211
212void rx_from_ms(struct msgb *msg)
213{
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100214 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100215
216 log("MSC <--%s-- MS: %s",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100217 osmo_rat_type_name(rx_from_ran),
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100218 gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100219
Maxba8a0072018-10-25 17:58:43 +0200220 if (!conn_exists(g_conn))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100221 g_conn = NULL;
222
223 if (!g_conn) {
224 log("new conn");
225 g_conn = conn_new();
Harald Weltec2007852018-02-03 21:08:26 +0100226 reset_l3_seq_nr();
227 patch_l3_seq_nr(msg);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100228 ran_conn_compl_l3(g_conn, msg, 23);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100229 } else {
Harald Weltec2007852018-02-03 21:08:26 +0100230 patch_l3_seq_nr(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100231 if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR)
232 && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL))
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100233 ran_conn_cipher_mode_compl(g_conn, msg, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100234 else
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100235 ran_conn_dtap(g_conn, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100236 }
237
Maxba8a0072018-10-25 17:58:43 +0200238 if (!conn_exists(g_conn))
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100239 g_conn = NULL;
240}
241
242void ms_sends_msg(const char *hex)
243{
244 struct msgb *msg;
245
246 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
247 msg->l1h = msg->l2h = msg->l3h = msg->data;
248 rx_from_ms(msg);
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200249 msgb_free(msg);
250}
251
252void bss_sends_bssap_mgmt(const char *hex)
253{
254 struct msgb *msg;
255 struct bssmap_header *bh;
256 struct a_conn_info a_conn_info;
257
258 msg = msgb_from_hex("bss_sends_bssap_mgmt", 1024, hex);
259 msg->l3h = msg->data;
260
261 msg->l2h = msgb_push(msg, sizeof(*bh));
262 bh = (void*)msg->l2h;
263 bh->type = BSSAP_MSG_BSS_MANAGEMENT;
264 bh->length = msgb_l3len(msg);
265
Maxba8a0072018-10-25 17:58:43 +0200266 if (!conn_exists(g_conn))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200267 g_conn = NULL;
268
269 OSMO_ASSERT(g_conn);
270 a_conn_info.network = net;
271 a_conn_info.conn_id = g_conn->a.conn_id;
272
273 a_sccp_rx_dt((struct osmo_sccp_user*)0x1, &a_conn_info, msg);
274 msgb_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100275}
276
Maxd8d1a9e2018-02-06 15:50:20 +0100277static int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100278{
279 int rc;
280 struct msgb *msg;
281 struct gsm48_hdr *gh;
282
283 msg = msgb_alloc(1024, "ms_sends_msg_fake");
284 msg->l1h = msg->l2h = msg->l3h = msg->data;
285
286 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
287 gh->proto_discr = pdisc;
288 gh->msg_type = msg_type;
289 /* some amount of data, whatever */
290 msgb_put(msg, 123);
291
Harald Weltec2007852018-02-03 21:08:26 +0100292 patch_l3_seq_nr(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100293 rc = gsm0408_dispatch(g_conn, msg);
294
295 talloc_free(msg);
296 return rc;
297}
298
Max68171902018-02-06 17:55:19 +0100299static inline void ms_msg_log_err(uint8_t val, uint8_t msgtype)
300{
301 int rc = ms_sends_msg_fake(val, msgtype);
302 if (rc != -EACCES)
303 log("Unexpected return value %u != %u for %s/%s",
304 -rc, -EACCES, gsm48_pdisc_name(val), gsm48_cc_msg_name(msgtype));
305}
306
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100307void thwart_rx_non_initial_requests()
308{
309 log("requests shall be thwarted");
Max68171902018-02-06 17:55:19 +0100310
311 ms_msg_log_err(GSM48_PDISC_CC, GSM48_MT_CC_SETUP);
312 ms_msg_log_err(GSM48_PDISC_MM, 0x33); /* nonexistent */
313 ms_msg_log_err(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1);
314 ms_msg_log_err(GSM48_PDISC_SMS, GSM411_MT_CP_DATA);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100315}
316
317void send_sms(struct vlr_subscr *receiver,
318 struct vlr_subscr *sender,
319 char *str)
320{
Harald Welte39b55482018-04-09 19:19:33 +0200321 struct gsm_sms *sms = sms_from_text(receiver, sender->msisdn, 0, str);
Vadim Yanitskiy24e025e2018-11-22 15:42:39 +0700322 gsm411_send_sms(net, receiver, sms);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100323}
324
325unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100326/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
327int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
328int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100329{
Max753c15d2017-12-21 14:50:44 +0100330 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100331 for (i = 0; i < num; i++)
332 buf[i] = next_rand_byte++;
333 return 1;
334}
335
336/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
337void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
338void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
339{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200340 /* Write fixed time bytes for deterministic test results */
341 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100342}
343
344const char *paging_expecting_imsi = NULL;
345uint32_t paging_expecting_tmsi;
346bool paging_sent;
347bool paging_stopped;
348
349void paging_expect_imsi(const char *imsi)
350{
351 paging_expecting_imsi = imsi;
352 paging_expecting_tmsi = GSM_RESERVED_TMSI;
353}
354
355void paging_expect_tmsi(uint32_t tmsi)
356{
357 paging_expecting_tmsi = tmsi;
358 paging_expecting_imsi = NULL;
359}
360
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100361static 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 +0100362{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200363 log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100364 osmo_rat_type_name(via_ran), imsi, tmsi, lac);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100365 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
366 if (paging_expecting_imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200367 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d");
368 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
369 VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x");
370 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100371 paging_sent = true;
372 paging_stopped = false;
373 return 1;
374}
375
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200376/* override, requires '-Wl,--wrap=ranap_iu_page_cs' */
377int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
378int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200379{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100380 return _paging_sent(OSMO_RAT_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200381}
382
Philipp Maierfbf66102017-04-09 12:32:51 +0200383/* override, requires '-Wl,--wrap=a_iface_tx_paging' */
384int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac);
385int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200386{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100387 return _paging_sent(OSMO_RAT_GERAN_A, imsi, tmsi, lac);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200388}
389
390/* override, requires '-Wl,--wrap=msc_stop_paging' */
391void __real_msc_stop_paging(struct vlr_subscr *vsub);
392void __wrap_msc_stop_paging(struct vlr_subscr *vsub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100393{
394 paging_stopped = true;
395}
396
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200397
398/* override, requires '-Wl,--wrap=osmo_sccp_tx_data_msg' */
399int __real_osmo_sccp_tx_data_msg(struct osmo_sccp_user *scu, uint32_t conn_id,
400 struct msgb *msg);
401int __wrap_osmo_sccp_tx_data_msg(struct osmo_sccp_user *scu, uint32_t conn_id,
402 struct msgb *msg)
403{
404 const char *proto_str;
405 const char *msg_str = gsm0808_bssmap_name(msg->l3h[2]);
406 switch (*msg->l3h) {
407 case BSSAP_MSG_BSS_MANAGEMENT:
408 proto_str = "BSSAP-BSS-MANAGEMENT";
409 break;
410 case BSSAP_MSG_DTAP:
411 proto_str = "BSSAP-DTAP";
412 break;
413 default:
414 proto_str = "";
415 msg_str = "";
416 break;
417 }
418
419 log("BSC <--%s-- MSC: %s %s", proto_str, msg_str, msgb_hexdump(msg));
420 msgb_free(msg);
421 return 0;
422}
423
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100424void clear_vlr()
425{
426 struct vlr_subscr *vsub, *n;
427 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
428 vlr_subscr_free(vsub);
429 }
430
431 net->authentication_required = false;
Harald Welte7b222aa2017-12-23 19:30:32 +0100432 net->a5_encryption_mask = (1 << 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100433 net->vlr->cfg.check_imei_rqd = false;
434 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200435 net->vlr->cfg.retrieve_imeisv_early = false;
436 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100437 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
438 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100439
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100440 rx_from_ran = OSMO_RAT_GERAN_A;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100441 auth_request_sent = false;
442 auth_request_expect_rand = NULL;
443 auth_request_expect_autn = NULL;
444
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100445 cipher_mode_cmd_sent = false;
446 cipher_mode_cmd_sent_with_imeisv = false;
447 cipher_mode_expect_kc = NULL;
448
449 security_mode_ctrl_sent = false;
450 security_mode_expect_ck = NULL;
451 security_mode_expect_ik = NULL;
452
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100453 next_rand_byte = 0;
454
Philipp Maierfbf66102017-04-09 12:32:51 +0200455 iu_release_expected = false;
456 iu_release_sent = false;
457 bssap_clear_expected = false;
458 bssap_clear_sent = false;
459
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100460 osmo_gettimeofday_override = false;
461}
462
463static struct log_info_cat test_categories[] = {
464 [DMSC] = {
465 .name = "DMSC",
466 .description = "Mobile Switching Center",
467 .enabled = 1, .loglevel = LOGL_DEBUG,
468 },
469 [DRLL] = {
470 .name = "DRLL",
471 .description = "A-bis Radio Link Layer (RLL)",
472 .enabled = 1, .loglevel = LOGL_DEBUG,
473 },
474 [DMM] = {
475 .name = "DMM",
476 .description = "Layer3 Mobility Management (MM)",
477 .enabled = 1, .loglevel = LOGL_DEBUG,
478 },
479 [DRR] = {
480 .name = "DRR",
481 .description = "Layer3 Radio Resource (RR)",
482 .enabled = 1, .loglevel = LOGL_DEBUG,
483 },
484 [DCC] = {
485 .name = "DCC",
486 .description = "Layer3 Call Control (CC)",
Neels Hofmeyr12e17be2018-03-12 23:59:37 +0100487 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100488 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100489 [DVLR] = {
490 .name = "DVLR",
491 .description = "Visitor Location Register",
492 .enabled = 1, .loglevel = LOGL_DEBUG,
493 },
494 [DREF] = {
495 .name = "DREF",
496 .description = "Reference Counting",
497 .enabled = 1, .loglevel = LOGL_DEBUG,
498 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200499 [DPAG] = {
500 .name = "DPAG",
501 .description = "Paging Subsystem",
502 .enabled = 1, .loglevel = LOGL_DEBUG,
503 },
504 [DIUCS] = {
505 .name = "DIUCS",
506 .description = "Iu-CS Protocol",
507 .enabled = 1, .loglevel = LOGL_DEBUG,
508 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100509 [DMNCC] = {
510 .name = "DMNCC",
511 .description = "MNCC API for Call Control application",
512 .enabled = 1, .loglevel = LOGL_DEBUG,
513 },
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100514 [DBSSAP] = {
515 .name = "DBSSAP",
516 .description = "BSSAP Protocol (A Interface)",
517 .enabled = 1, .loglevel = LOGL_DEBUG,
518 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100519};
520
521static struct log_info info = {
522 .cat = test_categories,
523 .num_cat = ARRAY_SIZE(test_categories),
524};
525
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100526int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100527{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100528 struct gsm_mncc *mncc = (void*)msg->data;
529 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
530 get_mncc_name(mncc->msg_type));
531
532 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
533 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
534 log("Mismatch! Expected MNCC msg type: %s",
535 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
536 abort();
537 }
538
539 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
540 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
541 cc_to_mncc_tx_expected_imsi,
542 mncc->imsi);
543 abort();
544 }
545
546 cc_to_mncc_tx_confirmed = true;
547 cc_to_mncc_tx_got_callref = mncc->callref;
548 cc_to_mncc_tx_expected_imsi = NULL;
549 cc_to_mncc_tx_expected_msg_type = 0;
550 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100551 return 0;
552}
553
Harald Welte1ea6baf2018-07-31 19:40:52 +0200554struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100555__real_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
556 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
557 struct osmo_oap_client_config *oap_config);
Harald Welte1ea6baf2018-07-31 19:40:52 +0200558struct osmo_gsup_client *
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100559__wrap_osmo_gsup_client_create2(struct ipaccess_unit *ipa_dev, const char *ip_addr,
560 unsigned int tcp_port, osmo_gsup_client_read_cb_t read_cb,
561 struct osmo_oap_client_config *oap_config)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100562{
Harald Welte1ea6baf2018-07-31 19:40:52 +0200563 struct osmo_gsup_client *gsupc;
564 gsupc = talloc_zero(msc_vlr_tests_ctx, struct osmo_gsup_client);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100565 OSMO_ASSERT(gsupc);
566 return gsupc;
567}
568
569/* override, requires '-Wl,--wrap=gsup_client_send' */
Harald Welte1ea6baf2018-07-31 19:40:52 +0200570int __real_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg);
571int __wrap_osmo_gsup_client_send(struct osmo_gsup_client *gsupc, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100572{
573 const char *is = osmo_hexdump_nospc(msg->data, msg->len);
574 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
575 osmo_gsup_message_type_name(msg->data[0]), is);
576
577 OSMO_ASSERT(gsup_tx_expected);
578 if (strcmp(gsup_tx_expected, is)) {
579 fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected);
580 abort();
581 }
582
583 talloc_free(msg);
584 gsup_tx_confirmed = true;
585 gsup_tx_expected = NULL;
586 return 0;
587}
588
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100589static int _validate_dtap(struct msgb *msg, enum osmo_rat_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100590{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100591 btw("DTAP --%s--> MS: %s: %s",
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100592 osmo_rat_type_name(to_ran), gh_type_name((void*)msg->data),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200593 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100594
595 OSMO_ASSERT(dtap_tx_expected);
Harald Weltec2007852018-02-03 21:08:26 +0100596
597 /* Mask the sequence number out before comparing */
598 msg->data[1] &= 0x3f;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100599 if (msg->len != dtap_tx_expected->len
600 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
601 fprintf(stderr, "Mismatch! Expected:\n%s\n",
602 osmo_hexdump_nospc(dtap_tx_expected->data,
603 dtap_tx_expected->len));
604 abort();
605 }
606
607 btw("DTAP matches expected message");
608
609 talloc_free(msg);
610 dtap_tx_confirmed = true;
611 talloc_free(dtap_tx_expected);
612 dtap_tx_expected = NULL;
613 return 0;
614}
615
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200616/* override, requires '-Wl,--wrap=ranap_iu_tx' */
617int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
618int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200619{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100620 return _validate_dtap(msg, OSMO_RAT_UTRAN_IU);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200621}
622
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200623/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
624int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
625int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200626{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100627 btw("Iu Release --%s--> MS", osmo_rat_type_name(OSMO_RAT_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200628 OSMO_ASSERT(iu_release_expected);
629 iu_release_expected = false;
630 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200631 return 0;
632}
633
634/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200635int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
636int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200637{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100638 btw("Iu Common ID --%s--> MS (IMSI=%s)", osmo_rat_type_name(OSMO_RAT_UTRAN_IU), imsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200639 return 0;
640}
641
Philipp Maierfbf66102017-04-09 12:32:51 +0200642/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
643int __real_a_iface_tx_dtap(struct msgb *msg);
644int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200645{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100646 return _validate_dtap(msg, OSMO_RAT_GERAN_A);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200647}
648
Philipp Maierfbf66102017-04-09 12:32:51 +0200649/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100650int __real_a_iface_tx_clear_cmd(struct ran_conn *conn);
651int __wrap_a_iface_tx_clear_cmd(struct ran_conn *conn)
Philipp Maierfbf66102017-04-09 12:32:51 +0200652{
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100653 btw("BSSAP Clear --%s--> MS", osmo_rat_type_name(OSMO_RAT_GERAN_A));
Philipp Maierfbf66102017-04-09 12:32:51 +0200654 OSMO_ASSERT(bssap_clear_expected);
655 bssap_clear_expected = false;
656 bssap_clear_sent = true;
657 return 0;
658}
659
Neels Hofmeyrb16259f2018-12-20 02:57:56 +0100660/* override, requires '-Wl,--wrap=msc_mgcp_try_call_assignment' */
661int __real_msc_mgcp_try_call_assignment(struct gsm_trans *trans);
662int __wrap_msc_mgcp_try_call_assignment(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100663{
664 log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x",
665 vlr_subscr_name(trans->vsub), trans->callref);
666 return 0;
667}
668
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100669struct gsm_mncc *on_call_release_mncc_sends_to_cc_data = NULL;
670
Philipp Maier621ba032017-11-07 17:19:25 +0100671/* override, requires '-Wl,--wrap=msc_mgcp_call_release' */
672void __real_msc_mgcp_call_release(struct gsm_trans *trans);
673void __wrap_msc_mgcp_call_release(struct gsm_trans *trans)
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100674{
675 log("MS <--Call Release-- MSC: subscr=%s callref=0x%x",
676 vlr_subscr_name(trans->vsub), trans->callref);
Neels Hofmeyrcbcf89c2018-03-13 17:52:07 +0100677 if (on_call_release_mncc_sends_to_cc_data) {
678 mncc_tx_to_cc(trans->net, on_call_release_mncc_sends_to_cc_data->msg_type,
679 on_call_release_mncc_sends_to_cc_data);
680 on_call_release_mncc_sends_to_cc_data = NULL;
681 }
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100682}
683
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100684static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
685{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100686 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100687 if (send_tmsi == GSM_RESERVED_TMSI)
688 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
689 else
690 btw("sending LU Accept for %s, with TMSI 0x%08x",
691 vlr_subscr_name(conn->vsub), send_tmsi);
692 lu_result_sent |= RES_ACCEPT;
693 return 0;
694}
695
Neels Hofmeyr15809592018-04-06 02:57:51 +0200696static int fake_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100697{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100698 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100699 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
700 lu_result_sent |= RES_REJECT;
701 return 0;
702}
703
704static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
705{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100706 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100707 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
708 cm_service_result_sent |= RES_ACCEPT;
709 return 0;
710}
711
Neels Hofmeyr15809592018-04-06 02:57:51 +0200712static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100713{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100714 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200715 btw("sending CM Service Reject for %s, cause: %s",
716 vlr_subscr_name(conn->vsub), gsm48_reject_value_name(cause));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100717 cm_service_result_sent |= RES_REJECT;
718 return 0;
719}
720
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +0100721static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100722 bool send_autn)
723{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100724 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100725 char *hex;
726 bool ok = true;
727 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
728 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
729 at->use_count, at->key_seq, at->vec.auth_types);
730
731 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
732 btw("...rand=%s", hex);
733 if (!auth_request_expect_rand
734 || strcmp(hex, auth_request_expect_rand) != 0) {
735 ok = false;
736 log("FAILURE: expected rand=%s",
737 auth_request_expect_rand ? auth_request_expect_rand : "-");
738 }
739
740 if (send_autn) {
741 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
742 btw("...autn=%s", hex);
743 if (!auth_request_expect_autn
744 || strcmp(hex, auth_request_expect_autn) != 0) {
745 ok = false;
746 log("FAILURE: expected autn=%s",
747 auth_request_expect_autn ? auth_request_expect_autn : "-");
748 }
749 } else if (auth_request_expect_autn) {
750 ok = false;
751 log("FAILURE: no AUTN sent, expected AUTN = %s",
752 auth_request_expect_autn);
753 }
754
755 if (send_autn)
756 btw("...expecting res=%s",
757 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
758 else
759 btw("...expecting sres=%s",
760 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
761
762 auth_request_sent = ok;
763 return 0;
764}
765
766static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
767{
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100768 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100769 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
770 return 0;
771}
772
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100773/* override, requires '-Wl,--wrap=a_iface_tx_cipher_mode' */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100774int __real_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100775 struct gsm0808_encrypt_info *ei, int include_imeisv);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100776int __wrap_a_iface_tx_cipher_mode(const struct ran_conn *conn,
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100777 struct gsm0808_encrypt_info *ei, int include_imeisv)
778{
779 int i;
780 btw("sending Ciphering Mode Command for %s: include_imeisv=%d",
781 vlr_subscr_name(conn->vsub), include_imeisv);
782 for (i = 0; i < ei->perm_algo_len; i++)
Neels Hofmeyrd28ea6c2018-09-17 00:54:52 +0200783 btw("...perm algo: A5/%u", ei->perm_algo[i] - 1);
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100784 OSMO_ASSERT(ei->key_len <= sizeof(ei->key));
785 btw("...key: %s", osmo_hexdump_nospc(ei->key, ei->key_len));
786 cipher_mode_cmd_sent = true;
787 cipher_mode_cmd_sent_with_imeisv = include_imeisv;
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100788
789 if (!cipher_mode_expect_kc
790 || strcmp(cipher_mode_expect_kc, osmo_hexdump_nospc(ei->key, ei->key_len))) {
791 log("FAILURE: expected kc=%s", cipher_mode_expect_kc ? : "NULL");
792 OSMO_ASSERT(false);
793 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100794 return 0;
795}
796
797/* override, requires '-Wl,--wrap=ranap_iu_tx_sec_mode_cmd' */
798int __real_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
799 int send_ck, int new_key);
800int __wrap_ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
801 int send_ck, int new_key)
802{
803 btw("sending SecurityModeControl for UE ctx %u send_ck=%d new_key=%d",
804 uectx->conn_id, send_ck, new_key);
805 btw("...ik=%s", osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)));
806 if (send_ck)
807 btw("...ck=%s", osmo_hexdump_nospc(vec->ck, sizeof(vec->ck)));
Neels Hofmeyrdbabfd32018-03-10 02:06:47 +0100808 security_mode_ctrl_sent = true;
809 if (!security_mode_expect_ik
810 || strcmp(security_mode_expect_ik, osmo_hexdump_nospc(vec->ik, sizeof(vec->ik)))) {
811 log("FAILURE: expected ik=%s", security_mode_expect_ik ? : "NULL");
812 OSMO_ASSERT(false);
813 }
814 if (((!!send_ck) != (!!security_mode_expect_ck))
815 || (security_mode_expect_ck
816 && strcmp(security_mode_expect_ck, osmo_hexdump_nospc(vec->ck, sizeof(vec->ck))))) {
817 log("FAILURE: expected ck=%s", security_mode_expect_ck ? : "NULL");
818 OSMO_ASSERT(false);
819 }
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100820 return 0;
821}
822
823extern int msc_vlr_set_ciph_mode(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv);
824
Harald Welte71c51df2017-12-23 18:51:48 +0100825static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100826{
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100827 int rc;
828#ifndef BUILD_IU
829 /* If we built without support for IU, fake the IU part here. The root cause is that we don't
830 * have differing sets of expected outputs for --enable-iu and --disable-iu. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100831 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100832 if (conn->via_ran == OSMO_RAT_UTRAN_IU) {
Neels Hofmeyrda21a522018-03-02 01:50:09 +0100833 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n", vlr_subscr_name(conn->vsub));
834 rc = __wrap_ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &conn->vsub->last_tuple->vec,
835 0, 1);
836 } else
837#endif
838 rc = msc_vlr_set_ciph_mode(msc_conn_ref, umts_aka, retrieve_imeisv);
839 if (rc)
840 btw("ERROR sending ciphering mode command: rc=%d", rc);
841 return rc;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100842}
843
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200844void ms_sends_security_mode_complete()
845{
846 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100847 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200848 OSMO_ASSERT(g_conn->iu.ue_ctx);
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100849 ran_conn_rx_sec_mode_compl(g_conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200850}
851
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200852void bss_sends_clear_complete()
853{
854 btw("BSS sends BSSMAP Clear Complete");
855 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100856 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_GERAN_A);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100857 ran_conn_rx_bssmap_clear_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200858}
859
860void rnc_sends_release_complete()
861{
862 btw("RNC sends Iu Release Complete");
863 OSMO_ASSERT(g_conn);
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100864 OSMO_ASSERT(g_conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100865 ran_conn_rx_iu_release_complete(g_conn);
Neels Hofmeyr4068ab22018-04-01 20:55:54 +0200866}
867
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100868const struct timeval fake_time_start_time = { 123, 456 };
869
870void fake_time_start()
871{
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200872 struct timespec *clock_override;
873
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100874 osmo_gettimeofday_override_time = fake_time_start_time;
875 osmo_gettimeofday_override = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200876 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
877 OSMO_ASSERT(clock_override);
878 clock_override->tv_sec = fake_time_start_time.tv_sec;
879 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
880 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100881 fake_time_passes(0, 0);
882}
883
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200884static void check_talloc(void *msgb_ctx, void *msc_vlr_tests_ctx)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100885{
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200886 /* Verifying that the msgb context is empty */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100887 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100888 /* Expecting these to stick around in msc_vlr_tests_ctx:
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100889 * talloc_total_blocks(tall_bsc_ctx) == 13
890 * full talloc report on 'msc_vlr_tests_ctx' (total 4638 bytes in 13 blocks)
891 * struct osmo_gsup_client contains 256 bytes in 1 blocks (ref 0) 0x61300000dd20
892 * struct gsm_network contains 2983 bytes in 5 blocks (ref 0) 0x61400000fea0
893 * struct vlr_instance contains 320 bytes in 2 blocks (ref 0) 0x61300000dee0
894 * struct ipaccess_unit contains 64 bytes in 1 blocks (ref 0) 0x60e0000244c0
895 * no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x60b00000af40
896 * rate_ctr.c:234 contains 2352 bytes in 1 blocks (ref 0) 0x61e00000f0e0
897 * logging contains 1399 bytes in 5 blocks (ref 0) 0x60b00000aff0
898 * struct log_target contains 238 bytes in 2 blocks (ref 0) 0x61200000bf20
899 * struct log_category contains 70 bytes in 1 blocks (ref 0) 0x60f00000efb0
900 * struct log_info contains 1160 bytes in 2 blocks (ref 0) 0x60d00000cfd0
901 * struct log_info_cat contains 1120 bytes in 1 blocks (ref 0) 0x61a00001f2e0
902 * msgb contains 0 bytes in 1 blocks (ref 0) 0x60800000bf80
903 * (That's 13 counting the root ctx)
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200904 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100905 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100906 talloc_total_blocks(msc_vlr_tests_ctx));
Stefan Sperlingafa030d2018-12-06 12:06:59 +0100907 if (talloc_total_blocks(msc_vlr_tests_ctx) != 13)
Neels Hofmeyrc01e9092018-03-22 15:56:49 +0100908 talloc_report_full(msc_vlr_tests_ctx, stderr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100909 fprintf(stderr, "\n");
910}
911
912static struct {
913 bool verbose;
914 int run_test_nr;
915} cmdline_opts = {
916 .verbose = false,
917 .run_test_nr = -1,
918};
919
920static void print_help(const char *program)
921{
922 printf("Usage:\n"
923 " %s [-v] [N [N...]]\n"
924 "Options:\n"
925 " -h --help show this text.\n"
926 " -v --verbose print source file and line numbers\n"
927 " N run only the Nth test (first test is N=1)\n",
928 program
929 );
930}
931
932static void handle_options(int argc, char **argv)
933{
934 while (1) {
935 int option_index = 0, c;
936 static struct option long_options[] = {
937 {"help", 0, 0, 'h'},
938 {"verbose", 1, 0, 'v'},
939 {0, 0, 0, 0}
940 };
941
942 c = getopt_long(argc, argv, "hv",
943 long_options, &option_index);
944 if (c == -1)
945 break;
946
947 switch (c) {
948 case 'h':
949 print_help(argv[0]);
950 exit(0);
951 case 'v':
952 cmdline_opts.verbose = true;
953 break;
954 default:
955 /* catch unknown options *as well as* missing arguments. */
956 fprintf(stderr, "Error in command line options. Exiting.\n");
957 exit(-1);
958 break;
959 }
960 }
961}
962
963void *msgb_ctx = NULL;
964
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100965static void run_tests(int nr)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100966{
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100967 int test_nr;
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200968
969 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
970
Max5e2e9bd2018-02-06 19:31:08 +0100971 nr--; /* arg's first test is 1, in here it's 0 */
972 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr++) {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100973 if (nr >= 0 && test_nr != nr)
974 continue;
975
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +0100976 if (cmdline_opts.verbose)
977 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
978
979 msc_vlr_tests[test_nr]();
980
981 if (cmdline_opts.verbose)
982 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100983
Neels Hofmeyr08b38282018-03-30 23:04:04 +0200984 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Max29ce08a2018-02-06 18:46:57 +0100985 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100986}
987
Max5e60de62018-02-07 12:56:09 +0100988struct gsm_network *test_net(void *ctx)
989{
Neels Hofmeyr7f484202018-02-27 12:59:45 +0100990 struct gsm_network *net = gsm_network_init(ctx, mncc_recv);
Max5e60de62018-02-07 12:56:09 +0100991
992 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
993 net->gsup_server_port = 0;
994
995 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
996 OSMO_ASSERT(msc_vlr_start(net) == 0);
997 OSMO_ASSERT(net->vlr);
998 OSMO_ASSERT(net->vlr->gsup_client);
999
1000 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
1001 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
1002 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
1003 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
1004 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
1005 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
1006 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
1007
1008 return net;
1009}
1010
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001011int main(int argc, char **argv)
1012{
1013 handle_options(argc, argv);
1014
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001015 msc_vlr_tests_ctx = talloc_named_const(NULL, 0, "msc_vlr_tests_ctx");
1016 msgb_ctx = msgb_talloc_ctx_init(msc_vlr_tests_ctx, 0);
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001017 osmo_init_logging2(msc_vlr_tests_ctx, &info);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001018
1019 _log_lines = cmdline_opts.verbose;
1020
1021 OSMO_ASSERT(osmo_stderr_target);
1022 log_set_use_color(osmo_stderr_target, 0);
1023 log_set_print_timestamp(osmo_stderr_target, 0);
1024 log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0);
1025 log_set_print_category(osmo_stderr_target, 1);
1026
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +01001027 if (cmdline_opts.verbose)
1028 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
1029
Neels Hofmeyrc01e9092018-03-22 15:56:49 +01001030 net = test_net(msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001031
1032 osmo_fsm_log_addr(false);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001033
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001034 ran_conn_init();
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001035
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001036 clear_vlr();
1037
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001038 if (optind >= argc)
1039 run_tests(-1);
1040 else {
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001041 int arg;
1042 long int nr;
1043 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +01001044 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001045 nr = strtol(argv[arg], NULL, 10);
1046 if (errno) {
1047 fprintf(stderr, "Invalid argument: %s\n",
1048 argv[arg]);
1049 exit(1);
1050 }
1051
Neels Hofmeyrdfdc61d2018-03-02 00:40:58 +01001052 run_tests(nr);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001053 }
1054 }
1055
1056 printf("Done\n");
1057
Neels Hofmeyr08b38282018-03-30 23:04:04 +02001058 check_talloc(msgb_ctx, msc_vlr_tests_ctx);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001059 return 0;
1060}