blob: b8553b64b10d4d5b9024bb25f0d1affcae3c2159 [file] [log] [blame]
Neels Hofmeyr6a29d322017-01-25 15:04:16 +01001/* Osmocom MSC+VLR end-to-end tests */
2
3/* (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 *
5 * All Rights Reserved
6 *
7 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <getopt.h>
25#include <stdlib.h>
26
27#include <osmocom/core/logging.h>
28#include <osmocom/core/utils.h>
29#include <osmocom/core/msgb.h>
30#include <osmocom/core/application.h>
31#include <osmocom/gsm/protocol/gsm_04_11.h>
32#include <osmocom/gsm/gsup.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020033#include <osmocom/msc/gsup_client.h>
34#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 Hofmeyr00e82d62017-07-05 15:19:52 +020038
39#if BUILD_IU
Neels Hofmeyr90843962017-09-04 15:04:35 +020040#include <osmocom/msc/iucs_ranap.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020041#include <osmocom/ranap/iu_client.h>
42#else
Neels Hofmeyr90843962017-09-04 15:04:35 +020043#include <osmocom/msc/iu_dummy.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020044#endif
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010045
46#include "msc_vlr_tests.h"
47
48bool _log_lines = false;
49
50struct gsm_network *net = NULL;
51
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010052const char *gsup_tx_expected = NULL;
53bool gsup_tx_confirmed;
54
55struct msgb *dtap_tx_expected = NULL;
56bool dtap_tx_confirmed;
57
58enum result_sent lu_result_sent;
59enum result_sent cm_service_result_sent;
60bool auth_request_sent;
61const char *auth_request_expect_rand;
62const char *auth_request_expect_autn;
63bool cipher_mode_cmd_sent;
64bool cipher_mode_cmd_sent_with_imeisv;
65
Philipp Maierfbf66102017-04-09 12:32:51 +020066bool iu_release_expected = false;
67bool iu_release_sent = false;
68bool bssap_clear_expected = false;
69bool bssap_clear_sent = false;
70
Neels Hofmeyra99b4272017-11-21 17:13:23 +010071uint32_t cc_to_mncc_tx_expected_msg_type = 0;
72const char *cc_to_mncc_tx_expected_imsi = NULL;
73bool cc_to_mncc_tx_confirmed = false;
74uint32_t cc_to_mncc_tx_got_callref = 0;
75
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010076struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
77{
78 struct msgb *msg = msgb_alloc(size, label);
79 unsigned char *rc;
80 msg->l2h = msg->head;
81 rc = msgb_put(msg, osmo_hexparse(hex, msg->head, msgb_tailroom(msg)));
82 OSMO_ASSERT(rc == msg->l2h);
83 return msg;
84}
85
Neels Hofmeyr78ada642017-03-10 02:15:20 +010086const char *gh_type_name(struct gsm48_hdr *gh)
87{
88 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
89 gsm48_hdr_msg_type(gh));
90}
91
92const char *msg_type_name(struct msgb *msg)
93{
94 return gh_type_name((void*)msg->data);
95}
96
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010097void dtap_expect_tx(const char *hex)
98{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020099 /* Has the previously expected dtap been received? */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100100 OSMO_ASSERT(!dtap_tx_expected);
101 if (!hex)
102 return;
103 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
104 dtap_tx_confirmed = false;
105}
106
107void dtap_expect_tx_ussd(char *ussd_text)
108{
109 uint8_t ussd_enc[128];
110 int len;
111 /* header */
112 char ussd_msg_hex[128] = "8b2a1c27a225020100302002013b301b04010f0416";
113
114 log("expecting USSD:\n %s", ussd_text);
115 /* append encoded USSD text */
116 gsm_7bit_encode_n_ussd(ussd_enc, sizeof(ussd_enc), ussd_text,
117 &len);
118 strncat(ussd_msg_hex, osmo_hexdump_nospc(ussd_enc, len),
119 sizeof(ussd_msg_hex) - strlen(ussd_msg_hex));
120 dtap_expect_tx(ussd_msg_hex);
121}
122
123int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg);
124
125void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
126{
127 int rc;
128 struct msgb *msg;
129 const char *label;
130
131 gsup_expect_tx(expect_tx_hex);
132
133 msg = msgb_from_hex("gsup", 1024, rx_hex);
134 label = osmo_gsup_message_type_name(msg->l2h[0]);
135 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
136 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
Neels Hofmeyr834f94a2017-09-28 03:07:16 +0200137 /* GSUP read cb takes ownership of msgb */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100138 rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg);
139 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
140 label, rc);
141 if (expect_tx_hex)
142 OSMO_ASSERT(gsup_tx_confirmed);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100143}
144
145bool conn_exists(struct gsm_subscriber_connection *conn)
146{
147 struct gsm_subscriber_connection *c;
148 llist_for_each_entry(c, &net->subscr_conns, entry) {
149 if (c == conn)
150 return true;
151 }
152 return false;
153}
154
155enum ran_type rx_from_ran = RAN_GERAN_A;
156
157struct gsm_subscriber_connection *conn_new(void)
158{
159 struct gsm_subscriber_connection *conn;
160 conn = msc_subscr_con_allocate(net);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100161 conn->via_ran = rx_from_ran;
Neels Hofmeyr8a656eb2017-11-22 02:55:52 +0100162 conn->lac = 23;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200163 if (conn->via_ran == RAN_UTRAN_IU) {
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200164 struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx);
165 *ue_ctx = (struct ranap_ue_conn_ctx){
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200166 .conn_id = 42,
167 };
168 conn->iu.ue_ctx = ue_ctx;
169 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100170 return conn;
171}
172
173struct gsm_subscriber_connection *g_conn = NULL;
174
175void rx_from_ms(struct msgb *msg)
176{
177 int rc;
178
179 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100180
181 log("MSC <--%s-- MS: %s",
182 ran_type_name(rx_from_ran),
183 gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100184
185 if (g_conn && !conn_exists(g_conn))
186 g_conn = NULL;
187
188 if (!g_conn) {
189 log("new conn");
190 g_conn = conn_new();
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200191 rc = msc_compl_l3(g_conn, msg, 23);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100192 if (rc == BSC_API_CONN_POL_REJECT) {
193 msc_subscr_con_free(g_conn);
194 g_conn = NULL;
195 }
196 } else {
197 if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR)
198 && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL))
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200199 msc_cipher_mode_compl(g_conn, msg, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100200 else
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200201 msc_dtap(g_conn, 23, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100202 }
203
204 if (g_conn && !conn_exists(g_conn))
205 g_conn = NULL;
206}
207
208void ms_sends_msg(const char *hex)
209{
210 struct msgb *msg;
211
212 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
213 msg->l1h = msg->l2h = msg->l3h = msg->data;
214 rx_from_ms(msg);
215 talloc_free(msg);
216}
217
218int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
219{
220 int rc;
221 struct msgb *msg;
222 struct gsm48_hdr *gh;
223
224 msg = msgb_alloc(1024, "ms_sends_msg_fake");
225 msg->l1h = msg->l2h = msg->l3h = msg->data;
226
227 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
228 gh->proto_discr = pdisc;
229 gh->msg_type = msg_type;
230 /* some amount of data, whatever */
231 msgb_put(msg, 123);
232
233 rc = gsm0408_dispatch(g_conn, msg);
234
235 talloc_free(msg);
236 return rc;
237}
238
239void thwart_rx_non_initial_requests()
240{
241 log("requests shall be thwarted");
242 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_CC, GSM48_MT_CC_SETUP) == -EACCES);
243 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_MM, 0x33 /* nonexistent */) == -EACCES);
244 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1) == -EACCES);
245 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_SMS, GSM411_MT_CP_DATA) == -EACCES);
246}
247
248void send_sms(struct vlr_subscr *receiver,
249 struct vlr_subscr *sender,
250 char *str)
251{
252 struct gsm_sms *sms = sms_from_text(receiver, sender, 0, str);
253 gsm411_send_sms_subscr(receiver, sms);
254}
255
256unsigned char next_rand_byte = 0;
Max753c15d2017-12-21 14:50:44 +0100257/* override, requires '-Wl,--wrap=osmo_get_rand_id' */
258int __real_osmo_get_rand_id(uint8_t *buf, size_t num);
259int __wrap_osmo_get_rand_id(uint8_t *buf, size_t num)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100260{
Max753c15d2017-12-21 14:50:44 +0100261 size_t i;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100262 for (i = 0; i < num; i++)
263 buf[i] = next_rand_byte++;
264 return 1;
265}
266
267/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
268void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
269void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
270{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200271 /* Write fixed time bytes for deterministic test results */
272 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100273}
274
275const char *paging_expecting_imsi = NULL;
276uint32_t paging_expecting_tmsi;
277bool paging_sent;
278bool paging_stopped;
279
280void paging_expect_imsi(const char *imsi)
281{
282 paging_expecting_imsi = imsi;
283 paging_expecting_tmsi = GSM_RESERVED_TMSI;
284}
285
286void paging_expect_tmsi(uint32_t tmsi)
287{
288 paging_expecting_tmsi = tmsi;
289 paging_expecting_imsi = NULL;
290}
291
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200292int _paging_sent(enum ran_type via_ran, const char *imsi, uint32_t tmsi, uint32_t lac)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100293{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200294 log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u",
295 ran_type_name(via_ran), imsi, tmsi, lac);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100296 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
297 if (paging_expecting_imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200298 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d");
299 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
300 VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x");
301 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100302 paging_sent = true;
303 paging_stopped = false;
304 return 1;
305}
306
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200307/* override, requires '-Wl,--wrap=ranap_iu_page_cs' */
308int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
309int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200310{
311 return _paging_sent(RAN_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
312}
313
Philipp Maierfbf66102017-04-09 12:32:51 +0200314/* override, requires '-Wl,--wrap=a_iface_tx_paging' */
315int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac);
316int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200317{
318 return _paging_sent(RAN_GERAN_A, imsi, tmsi, lac);
319}
320
321/* override, requires '-Wl,--wrap=msc_stop_paging' */
322void __real_msc_stop_paging(struct vlr_subscr *vsub);
323void __wrap_msc_stop_paging(struct vlr_subscr *vsub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100324{
325 paging_stopped = true;
326}
327
328void clear_vlr()
329{
330 struct vlr_subscr *vsub, *n;
331 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
332 vlr_subscr_free(vsub);
333 }
334
335 net->authentication_required = false;
336 net->a5_encryption = VLR_CIPH_NONE;
337 net->vlr->cfg.check_imei_rqd = false;
338 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200339 net->vlr->cfg.retrieve_imeisv_early = false;
340 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100341 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
342 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100343
344 rx_from_ran = RAN_GERAN_A;
345 auth_request_sent = false;
346 auth_request_expect_rand = NULL;
347 auth_request_expect_autn = NULL;
348
349 next_rand_byte = 0;
350
Philipp Maierfbf66102017-04-09 12:32:51 +0200351 iu_release_expected = false;
352 iu_release_sent = false;
353 bssap_clear_expected = false;
354 bssap_clear_sent = false;
355
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100356 osmo_gettimeofday_override = false;
357}
358
359static struct log_info_cat test_categories[] = {
360 [DMSC] = {
361 .name = "DMSC",
362 .description = "Mobile Switching Center",
363 .enabled = 1, .loglevel = LOGL_DEBUG,
364 },
365 [DRLL] = {
366 .name = "DRLL",
367 .description = "A-bis Radio Link Layer (RLL)",
368 .enabled = 1, .loglevel = LOGL_DEBUG,
369 },
370 [DMM] = {
371 .name = "DMM",
372 .description = "Layer3 Mobility Management (MM)",
373 .enabled = 1, .loglevel = LOGL_DEBUG,
374 },
375 [DRR] = {
376 .name = "DRR",
377 .description = "Layer3 Radio Resource (RR)",
378 .enabled = 1, .loglevel = LOGL_DEBUG,
379 },
380 [DCC] = {
381 .name = "DCC",
382 .description = "Layer3 Call Control (CC)",
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100383 .enabled = 1, .loglevel = LOGL_INFO,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100384 },
385 [DMM] = {
386 .name = "DMM",
387 .description = "Layer3 Mobility Management (MM)",
388 .enabled = 1, .loglevel = LOGL_DEBUG,
389 },
390 [DVLR] = {
391 .name = "DVLR",
392 .description = "Visitor Location Register",
393 .enabled = 1, .loglevel = LOGL_DEBUG,
394 },
395 [DREF] = {
396 .name = "DREF",
397 .description = "Reference Counting",
398 .enabled = 1, .loglevel = LOGL_DEBUG,
399 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200400 [DPAG] = {
401 .name = "DPAG",
402 .description = "Paging Subsystem",
403 .enabled = 1, .loglevel = LOGL_DEBUG,
404 },
405 [DIUCS] = {
406 .name = "DIUCS",
407 .description = "Iu-CS Protocol",
408 .enabled = 1, .loglevel = LOGL_DEBUG,
409 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100410 [DMNCC] = {
411 .name = "DMNCC",
412 .description = "MNCC API for Call Control application",
413 .enabled = 1, .loglevel = LOGL_DEBUG,
414 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100415};
416
417static struct log_info info = {
418 .cat = test_categories,
419 .num_cat = ARRAY_SIZE(test_categories),
420};
421
422extern void *tall_bsc_ctx;
423
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100424int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100425{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100426 struct gsm_mncc *mncc = (void*)msg->data;
427 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
428 get_mncc_name(mncc->msg_type));
429
430 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
431 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
432 log("Mismatch! Expected MNCC msg type: %s",
433 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
434 abort();
435 }
436
437 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
438 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
439 cc_to_mncc_tx_expected_imsi,
440 mncc->imsi);
441 abort();
442 }
443
444 cc_to_mncc_tx_confirmed = true;
445 cc_to_mncc_tx_got_callref = mncc->callref;
446 cc_to_mncc_tx_expected_imsi = NULL;
447 cc_to_mncc_tx_expected_msg_type = 0;
448 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100449 return 0;
450}
451
452/* override, requires '-Wl,--wrap=gsup_client_create' */
453struct gsup_client *
454__real_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
455 gsup_client_read_cb_t read_cb,
456 struct oap_client_config *oap_config);
457struct gsup_client *
458__wrap_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
459 gsup_client_read_cb_t read_cb,
460 struct oap_client_config *oap_config)
461{
462 struct gsup_client *gsupc;
463 gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client);
464 OSMO_ASSERT(gsupc);
465 return gsupc;
466}
467
468/* override, requires '-Wl,--wrap=gsup_client_send' */
469int __real_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg);
470int __wrap_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg)
471{
472 const char *is = osmo_hexdump_nospc(msg->data, msg->len);
473 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
474 osmo_gsup_message_type_name(msg->data[0]), is);
475
476 OSMO_ASSERT(gsup_tx_expected);
477 if (strcmp(gsup_tx_expected, is)) {
478 fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected);
479 abort();
480 }
481
482 talloc_free(msg);
483 gsup_tx_confirmed = true;
484 gsup_tx_expected = NULL;
485 return 0;
486}
487
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200488int _validate_dtap(struct msgb *msg, enum ran_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100489{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100490 btw("DTAP --%s--> MS: %s: %s",
491 ran_type_name(to_ran), msg_type_name(msg),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200492 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100493
494 OSMO_ASSERT(dtap_tx_expected);
495 if (msg->len != dtap_tx_expected->len
496 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
497 fprintf(stderr, "Mismatch! Expected:\n%s\n",
498 osmo_hexdump_nospc(dtap_tx_expected->data,
499 dtap_tx_expected->len));
500 abort();
501 }
502
503 btw("DTAP matches expected message");
504
505 talloc_free(msg);
506 dtap_tx_confirmed = true;
507 talloc_free(dtap_tx_expected);
508 dtap_tx_expected = NULL;
509 return 0;
510}
511
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200512/* override, requires '-Wl,--wrap=ranap_iu_tx' */
513int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
514int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200515{
516 return _validate_dtap(msg, RAN_UTRAN_IU);
517}
518
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200519/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
520int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
521int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200522{
523 btw("Iu Release --%s--> MS", ran_type_name(RAN_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200524 OSMO_ASSERT(iu_release_expected);
525 iu_release_expected = false;
526 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200527 return 0;
528}
529
530/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200531int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
532int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200533{
534 btw("Iu Common ID --%s--> MS (IMSI=%s)", ran_type_name(RAN_UTRAN_IU), imsi);
535 return 0;
536}
537
Philipp Maierfbf66102017-04-09 12:32:51 +0200538/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
539int __real_a_iface_tx_dtap(struct msgb *msg);
540int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200541{
542 return _validate_dtap(msg, RAN_GERAN_A);
543}
544
Philipp Maierfbf66102017-04-09 12:32:51 +0200545/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
546int __real_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn);
547int __wrap_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
548{
549 btw("BSSAP Clear --%s--> MS", ran_type_name(RAN_GERAN_A));
550 OSMO_ASSERT(bssap_clear_expected);
551 bssap_clear_expected = false;
552 bssap_clear_sent = true;
553 return 0;
554}
555
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100556/* override, requires '-Wl,--wrap=msc_call_assignment' */
557int __real_msc_call_assignment(struct gsm_trans *trans);
558int __wrap_msc_call_assignment(struct gsm_trans *trans)
559{
560 log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x",
561 vlr_subscr_name(trans->vsub), trans->callref);
562 return 0;
563}
564
565/* override, requires '-Wl,--wrap=msc_call_release' */
566void __real_msc_call_release(struct gsm_trans *trans);
567void __wrap_msc_call_release(struct gsm_trans *trans)
568{
569 log("MS <--Call Release-- MSC: subscr=%s callref=0x%x",
570 vlr_subscr_name(trans->vsub), trans->callref);
571}
572
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100573static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
574{
575 struct gsm_subscriber_connection *conn = msc_conn_ref;
576 if (send_tmsi == GSM_RESERVED_TMSI)
577 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
578 else
579 btw("sending LU Accept for %s, with TMSI 0x%08x",
580 vlr_subscr_name(conn->vsub), send_tmsi);
581 lu_result_sent |= RES_ACCEPT;
582 return 0;
583}
584
585static int fake_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
586{
587 struct gsm_subscriber_connection *conn = msc_conn_ref;
588 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
589 lu_result_sent |= RES_REJECT;
590 return 0;
591}
592
593static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
594{
595 struct gsm_subscriber_connection *conn = msc_conn_ref;
596 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
597 cm_service_result_sent |= RES_ACCEPT;
598 return 0;
599}
600
601static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref,
602 enum vlr_proc_arq_result result)
603{
604 struct gsm_subscriber_connection *conn = msc_conn_ref;
605 btw("sending CM Service Reject for %s, result %s",
606 vlr_subscr_name(conn->vsub),
607 vlr_proc_arq_result_name(result));
608 cm_service_result_sent |= RES_REJECT;
609 return 0;
610}
611
612static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
613 bool send_autn)
614{
615 struct gsm_subscriber_connection *conn = msc_conn_ref;
616 char *hex;
617 bool ok = true;
618 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
619 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
620 at->use_count, at->key_seq, at->vec.auth_types);
621
622 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
623 btw("...rand=%s", hex);
624 if (!auth_request_expect_rand
625 || strcmp(hex, auth_request_expect_rand) != 0) {
626 ok = false;
627 log("FAILURE: expected rand=%s",
628 auth_request_expect_rand ? auth_request_expect_rand : "-");
629 }
630
631 if (send_autn) {
632 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
633 btw("...autn=%s", hex);
634 if (!auth_request_expect_autn
635 || strcmp(hex, auth_request_expect_autn) != 0) {
636 ok = false;
637 log("FAILURE: expected autn=%s",
638 auth_request_expect_autn ? auth_request_expect_autn : "-");
639 }
640 } else if (auth_request_expect_autn) {
641 ok = false;
642 log("FAILURE: no AUTN sent, expected AUTN = %s",
643 auth_request_expect_autn);
644 }
645
646 if (send_autn)
647 btw("...expecting res=%s",
648 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
649 else
650 btw("...expecting sres=%s",
651 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
652
653 auth_request_sent = ok;
654 return 0;
655}
656
657static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
658{
659 struct gsm_subscriber_connection *conn = msc_conn_ref;
660 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
661 return 0;
662}
663
Harald Welte71c51df2017-12-23 18:51:48 +0100664static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100665{
666 /* FIXME: we actually would like to see the message bytes checked here,
667 * not possible while msc_vlr_set_ciph_mode() calls
668 * gsm0808_cipher_mode() directly. When the MSCSPLIT is ready, check
669 * the tx bytes in the sense of dtap_expect_tx() above. */
670 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200671 switch (conn->via_ran) {
672 case RAN_GERAN_A:
673 btw("sending Ciphering Mode Command for %s: cipher=%s kc=%s"
674 " retrieve_imeisv=%d",
675 vlr_subscr_name(conn->vsub),
676 vlr_ciph_name(conn->network->a5_encryption),
677 osmo_hexdump_nospc(conn->vsub->last_tuple->vec.kc, 8),
678 retrieve_imeisv);
679 break;
680 case RAN_UTRAN_IU:
681 btw("sending SecurityModeControl for %s",
682 vlr_subscr_name(conn->vsub));
683 break;
684 default:
685 btw("UNKNOWN RAN TYPE %d", conn->via_ran);
686 OSMO_ASSERT(false);
687 return -1;
688 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100689 cipher_mode_cmd_sent = true;
690 cipher_mode_cmd_sent_with_imeisv = retrieve_imeisv;
691 return 0;
692}
693
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200694void ms_sends_security_mode_complete()
695{
696 OSMO_ASSERT(g_conn);
697 OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU);
698 OSMO_ASSERT(g_conn->iu.ue_ctx);
699 msc_rx_sec_mode_compl(g_conn);
700}
701
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100702const struct timeval fake_time_start_time = { 123, 456 };
703
704void fake_time_start()
705{
706 osmo_gettimeofday_override_time = fake_time_start_time;
707 osmo_gettimeofday_override = true;
708 fake_time_passes(0, 0);
709}
710
711void check_talloc(void *msgb_ctx, void *tall_bsc_ctx, int expected_blocks)
712{
713 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyr2c46e042017-11-18 23:29:24 +0100714 /* Expecting these to stick around in tall_bsc_ctx:
715full talloc report on 'msgb' (total 0 bytes in 1 blocks)
Harald Weltea3ab1de2018-01-24 15:17:06 +0100716talloc_total_blocks(tall_bsc_ctx) == 8
717full talloc report on 'subscr_conn_test_ctx' (total 2642 bytes in 8 blocks)
Neels Hofmeyr2c46e042017-11-18 23:29:24 +0100718 struct gsup_client contains 248 bytes in 1 blocks (ref 0) 0x61300000dee0
719 struct gsm_network contains 2410 bytes in 6 blocks (ref 0) 0x61700000fce0
720 struct vlr_instance contains 160 bytes in 1 blocks (ref 0) 0x611000009a60
721 no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x60b00000ade0
722 ../../../src/libosmocore/src/rate_ctr.c:199 contains 1552 bytes in 1 blocks (ref 0) 0x61b00001eae0
Neels Hofmeyr2c46e042017-11-18 23:29:24 +0100723 .* contains 3 bytes in 1 blocks (ref 0) 0x60b00000af40
724 msgb contains 0 bytes in 1 blocks (ref 0) 0x60800000bf80
725 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100726 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
727 talloc_total_blocks(tall_bsc_ctx));
728 if (talloc_total_blocks(tall_bsc_ctx) != expected_blocks)
729 talloc_report_full(tall_bsc_ctx, stderr);
730 fprintf(stderr, "\n");
731}
732
733static struct {
734 bool verbose;
735 int run_test_nr;
736} cmdline_opts = {
737 .verbose = false,
738 .run_test_nr = -1,
739};
740
741static void print_help(const char *program)
742{
743 printf("Usage:\n"
744 " %s [-v] [N [N...]]\n"
745 "Options:\n"
746 " -h --help show this text.\n"
747 " -v --verbose print source file and line numbers\n"
748 " N run only the Nth test (first test is N=1)\n",
749 program
750 );
751}
752
753static void handle_options(int argc, char **argv)
754{
755 while (1) {
756 int option_index = 0, c;
757 static struct option long_options[] = {
758 {"help", 0, 0, 'h'},
759 {"verbose", 1, 0, 'v'},
760 {0, 0, 0, 0}
761 };
762
763 c = getopt_long(argc, argv, "hv",
764 long_options, &option_index);
765 if (c == -1)
766 break;
767
768 switch (c) {
769 case 'h':
770 print_help(argv[0]);
771 exit(0);
772 case 'v':
773 cmdline_opts.verbose = true;
774 break;
775 default:
776 /* catch unknown options *as well as* missing arguments. */
777 fprintf(stderr, "Error in command line options. Exiting.\n");
778 exit(-1);
779 break;
780 }
781 }
782}
783
784void *msgb_ctx = NULL;
785
786void run_tests(int nr)
787{
788 int test_nr;
789 nr --; /* arg's first test is 1, in here it's 0 */
790 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr ++) {
791 if (nr >= 0 && test_nr != nr)
792 continue;
793
794 if (cmdline_opts.verbose)
795 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
796
797 msc_vlr_tests[test_nr]();
798
799 if (cmdline_opts.verbose)
800 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
801
Harald Weltea3ab1de2018-01-24 15:17:06 +0100802 check_talloc(msgb_ctx, tall_bsc_ctx, 8);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100803 } while(0);
804}
805
806int main(int argc, char **argv)
807{
808 handle_options(argc, argv);
809
810 tall_bsc_ctx = talloc_named_const(NULL, 0, "subscr_conn_test_ctx");
811 msgb_ctx = msgb_talloc_ctx_init(tall_bsc_ctx, 0);
812 osmo_init_logging(&info);
813
814 _log_lines = cmdline_opts.verbose;
815
816 OSMO_ASSERT(osmo_stderr_target);
817 log_set_use_color(osmo_stderr_target, 0);
818 log_set_print_timestamp(osmo_stderr_target, 0);
819 log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0);
820 log_set_print_category(osmo_stderr_target, 1);
821
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +0100822 if (cmdline_opts.verbose)
823 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
824
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100825 net = gsm_network_init(tall_bsc_ctx, 1, 1, mncc_recv);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100826 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
827 net->gsup_server_port = 0;
828
829 osmo_fsm_log_addr(false);
830 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
831 OSMO_ASSERT(msc_vlr_start(net) == 0);
832 OSMO_ASSERT(net->vlr);
833 OSMO_ASSERT(net->vlr->gsup_client);
834 msc_subscr_conn_init();
835
836 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
837 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
838 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
839 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
840 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
841 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
842 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
843
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200844 clear_vlr();
845
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100846 if (optind >= argc)
847 run_tests(-1);
848 else {
849 int arg;
850 long int nr;
851 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +0100852 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100853 nr = strtol(argv[arg], NULL, 10);
854 if (errno) {
855 fprintf(stderr, "Invalid argument: %s\n",
856 argv[arg]);
857 exit(1);
858 }
859
860 run_tests(nr);
861 }
862 }
863
864 printf("Done\n");
865
Harald Weltea3ab1de2018-01-24 15:17:06 +0100866 check_talloc(msgb_ctx, tall_bsc_ctx, 8);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100867 return 0;
868}