blob: f0a112b2a588b31fa4f29e16fc8fca7a61fe217d [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
52struct gsm_bts *the_bts;
53
54const char *gsup_tx_expected = NULL;
55bool gsup_tx_confirmed;
56
57struct msgb *dtap_tx_expected = NULL;
58bool dtap_tx_confirmed;
59
60enum result_sent lu_result_sent;
61enum result_sent cm_service_result_sent;
62bool auth_request_sent;
63const char *auth_request_expect_rand;
64const char *auth_request_expect_autn;
65bool cipher_mode_cmd_sent;
66bool cipher_mode_cmd_sent_with_imeisv;
67
Philipp Maierfbf66102017-04-09 12:32:51 +020068bool iu_release_expected = false;
69bool iu_release_sent = false;
70bool bssap_clear_expected = false;
71bool bssap_clear_sent = false;
72
Neels Hofmeyra99b4272017-11-21 17:13:23 +010073uint32_t cc_to_mncc_tx_expected_msg_type = 0;
74const char *cc_to_mncc_tx_expected_imsi = NULL;
75bool cc_to_mncc_tx_confirmed = false;
76uint32_t cc_to_mncc_tx_got_callref = 0;
77
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010078struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
79{
80 struct msgb *msg = msgb_alloc(size, label);
81 unsigned char *rc;
82 msg->l2h = msg->head;
83 rc = msgb_put(msg, osmo_hexparse(hex, msg->head, msgb_tailroom(msg)));
84 OSMO_ASSERT(rc == msg->l2h);
85 return msg;
86}
87
Neels Hofmeyr78ada642017-03-10 02:15:20 +010088const char *gh_type_name(struct gsm48_hdr *gh)
89{
90 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
91 gsm48_hdr_msg_type(gh));
92}
93
94const char *msg_type_name(struct msgb *msg)
95{
96 return gh_type_name((void*)msg->data);
97}
98
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010099void dtap_expect_tx(const char *hex)
100{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200101 /* Has the previously expected dtap been received? */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100102 OSMO_ASSERT(!dtap_tx_expected);
103 if (!hex)
104 return;
105 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
106 dtap_tx_confirmed = false;
107}
108
109void dtap_expect_tx_ussd(char *ussd_text)
110{
111 uint8_t ussd_enc[128];
112 int len;
113 /* header */
114 char ussd_msg_hex[128] = "8b2a1c27a225020100302002013b301b04010f0416";
115
116 log("expecting USSD:\n %s", ussd_text);
117 /* append encoded USSD text */
118 gsm_7bit_encode_n_ussd(ussd_enc, sizeof(ussd_enc), ussd_text,
119 &len);
120 strncat(ussd_msg_hex, osmo_hexdump_nospc(ussd_enc, len),
121 sizeof(ussd_msg_hex) - strlen(ussd_msg_hex));
122 dtap_expect_tx(ussd_msg_hex);
123}
124
125int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg);
126
127void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
128{
129 int rc;
130 struct msgb *msg;
131 const char *label;
132
133 gsup_expect_tx(expect_tx_hex);
134
135 msg = msgb_from_hex("gsup", 1024, rx_hex);
136 label = osmo_gsup_message_type_name(msg->l2h[0]);
137 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
138 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
Neels Hofmeyr834f94a2017-09-28 03:07:16 +0200139 /* GSUP read cb takes ownership of msgb */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100140 rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg);
141 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
142 label, rc);
143 if (expect_tx_hex)
144 OSMO_ASSERT(gsup_tx_confirmed);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100145}
146
147bool conn_exists(struct gsm_subscriber_connection *conn)
148{
149 struct gsm_subscriber_connection *c;
150 llist_for_each_entry(c, &net->subscr_conns, entry) {
151 if (c == conn)
152 return true;
153 }
154 return false;
155}
156
157enum ran_type rx_from_ran = RAN_GERAN_A;
158
159struct gsm_subscriber_connection *conn_new(void)
160{
161 struct gsm_subscriber_connection *conn;
162 conn = msc_subscr_con_allocate(net);
163 conn->bts = the_bts;
164 conn->via_ran = rx_from_ran;
Neels Hofmeyr8a656eb2017-11-22 02:55:52 +0100165 conn->lac = 23;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200166 if (conn->via_ran == RAN_UTRAN_IU) {
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200167 struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx);
168 *ue_ctx = (struct ranap_ue_conn_ctx){
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200169 .conn_id = 42,
170 };
171 conn->iu.ue_ctx = ue_ctx;
172 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100173 return conn;
174}
175
176struct gsm_subscriber_connection *g_conn = NULL;
177
178void rx_from_ms(struct msgb *msg)
179{
180 int rc;
181
182 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100183
184 log("MSC <--%s-- MS: %s",
185 ran_type_name(rx_from_ran),
186 gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100187
188 if (g_conn && !conn_exists(g_conn))
189 g_conn = NULL;
190
191 if (!g_conn) {
192 log("new conn");
193 g_conn = conn_new();
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200194 rc = msc_compl_l3(g_conn, msg, 23);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100195 if (rc == BSC_API_CONN_POL_REJECT) {
196 msc_subscr_con_free(g_conn);
197 g_conn = NULL;
198 }
199 } else {
200 if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR)
201 && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL))
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200202 msc_cipher_mode_compl(g_conn, msg, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100203 else
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200204 msc_dtap(g_conn, 23, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100205 }
206
207 if (g_conn && !conn_exists(g_conn))
208 g_conn = NULL;
209}
210
211void ms_sends_msg(const char *hex)
212{
213 struct msgb *msg;
214
215 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
216 msg->l1h = msg->l2h = msg->l3h = msg->data;
217 rx_from_ms(msg);
218 talloc_free(msg);
219}
220
221int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
222{
223 int rc;
224 struct msgb *msg;
225 struct gsm48_hdr *gh;
226
227 msg = msgb_alloc(1024, "ms_sends_msg_fake");
228 msg->l1h = msg->l2h = msg->l3h = msg->data;
229
230 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
231 gh->proto_discr = pdisc;
232 gh->msg_type = msg_type;
233 /* some amount of data, whatever */
234 msgb_put(msg, 123);
235
236 rc = gsm0408_dispatch(g_conn, msg);
237
238 talloc_free(msg);
239 return rc;
240}
241
242void thwart_rx_non_initial_requests()
243{
244 log("requests shall be thwarted");
245 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_CC, GSM48_MT_CC_SETUP) == -EACCES);
246 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_MM, 0x33 /* nonexistent */) == -EACCES);
247 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1) == -EACCES);
248 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_SMS, GSM411_MT_CP_DATA) == -EACCES);
249}
250
251void send_sms(struct vlr_subscr *receiver,
252 struct vlr_subscr *sender,
253 char *str)
254{
255 struct gsm_sms *sms = sms_from_text(receiver, sender, 0, str);
256 gsm411_send_sms_subscr(receiver, sms);
257}
258
259unsigned char next_rand_byte = 0;
260/* override, requires '-Wl,--wrap=RAND_bytes' */
261int __real_RAND_bytes(unsigned char *buf, int num);
262int __wrap_RAND_bytes(unsigned char *buf, int num)
263{
264 int i;
265 for (i = 0; i < num; i++)
266 buf[i] = next_rand_byte++;
267 return 1;
268}
269
270/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
271void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
272void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
273{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200274 /* Write fixed time bytes for deterministic test results */
275 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100276}
277
278const char *paging_expecting_imsi = NULL;
279uint32_t paging_expecting_tmsi;
280bool paging_sent;
281bool paging_stopped;
282
283void paging_expect_imsi(const char *imsi)
284{
285 paging_expecting_imsi = imsi;
286 paging_expecting_tmsi = GSM_RESERVED_TMSI;
287}
288
289void paging_expect_tmsi(uint32_t tmsi)
290{
291 paging_expecting_tmsi = tmsi;
292 paging_expecting_imsi = NULL;
293}
294
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200295int _paging_sent(enum ran_type via_ran, const char *imsi, uint32_t tmsi, uint32_t lac)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100296{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200297 log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u",
298 ran_type_name(via_ran), imsi, tmsi, lac);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100299 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
300 if (paging_expecting_imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200301 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d");
302 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
303 VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x");
304 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100305 paging_sent = true;
306 paging_stopped = false;
307 return 1;
308}
309
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200310/* override, requires '-Wl,--wrap=ranap_iu_page_cs' */
311int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
312int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200313{
314 return _paging_sent(RAN_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
315}
316
Philipp Maierfbf66102017-04-09 12:32:51 +0200317/* override, requires '-Wl,--wrap=a_iface_tx_paging' */
318int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac);
319int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200320{
321 return _paging_sent(RAN_GERAN_A, imsi, tmsi, lac);
322}
323
324/* override, requires '-Wl,--wrap=msc_stop_paging' */
325void __real_msc_stop_paging(struct vlr_subscr *vsub);
326void __wrap_msc_stop_paging(struct vlr_subscr *vsub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100327{
328 paging_stopped = true;
329}
330
331void clear_vlr()
332{
333 struct vlr_subscr *vsub, *n;
334 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
335 vlr_subscr_free(vsub);
336 }
337
338 net->authentication_required = false;
339 net->a5_encryption = VLR_CIPH_NONE;
340 net->vlr->cfg.check_imei_rqd = false;
341 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200342 net->vlr->cfg.retrieve_imeisv_early = false;
343 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr7b1418e2017-10-29 02:12:16 +0100344 net->vlr->cfg.auth_tuple_max_reuse_count = 0;
345 net->vlr->cfg.auth_reuse_old_sets_on_error = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100346
347 rx_from_ran = RAN_GERAN_A;
348 auth_request_sent = false;
349 auth_request_expect_rand = NULL;
350 auth_request_expect_autn = NULL;
351
352 next_rand_byte = 0;
353
Philipp Maierfbf66102017-04-09 12:32:51 +0200354 iu_release_expected = false;
355 iu_release_sent = false;
356 bssap_clear_expected = false;
357 bssap_clear_sent = false;
358
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100359 osmo_gettimeofday_override = false;
360}
361
362static struct log_info_cat test_categories[] = {
363 [DMSC] = {
364 .name = "DMSC",
365 .description = "Mobile Switching Center",
366 .enabled = 1, .loglevel = LOGL_DEBUG,
367 },
368 [DRLL] = {
369 .name = "DRLL",
370 .description = "A-bis Radio Link Layer (RLL)",
371 .enabled = 1, .loglevel = LOGL_DEBUG,
372 },
373 [DMM] = {
374 .name = "DMM",
375 .description = "Layer3 Mobility Management (MM)",
376 .enabled = 1, .loglevel = LOGL_DEBUG,
377 },
378 [DRR] = {
379 .name = "DRR",
380 .description = "Layer3 Radio Resource (RR)",
381 .enabled = 1, .loglevel = LOGL_DEBUG,
382 },
383 [DCC] = {
384 .name = "DCC",
385 .description = "Layer3 Call Control (CC)",
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100386 .enabled = 1, .loglevel = LOGL_INFO,
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100387 },
388 [DMM] = {
389 .name = "DMM",
390 .description = "Layer3 Mobility Management (MM)",
391 .enabled = 1, .loglevel = LOGL_DEBUG,
392 },
393 [DVLR] = {
394 .name = "DVLR",
395 .description = "Visitor Location Register",
396 .enabled = 1, .loglevel = LOGL_DEBUG,
397 },
398 [DREF] = {
399 .name = "DREF",
400 .description = "Reference Counting",
401 .enabled = 1, .loglevel = LOGL_DEBUG,
402 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200403 [DPAG] = {
404 .name = "DPAG",
405 .description = "Paging Subsystem",
406 .enabled = 1, .loglevel = LOGL_DEBUG,
407 },
408 [DIUCS] = {
409 .name = "DIUCS",
410 .description = "Iu-CS Protocol",
411 .enabled = 1, .loglevel = LOGL_DEBUG,
412 },
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100413 [DMNCC] = {
414 .name = "DMNCC",
415 .description = "MNCC API for Call Control application",
416 .enabled = 1, .loglevel = LOGL_DEBUG,
417 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100418};
419
420static struct log_info info = {
421 .cat = test_categories,
422 .num_cat = ARRAY_SIZE(test_categories),
423};
424
425extern void *tall_bsc_ctx;
426
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100427int mncc_recv(struct gsm_network *net, struct msgb *msg)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100428{
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100429 struct gsm_mncc *mncc = (void*)msg->data;
430 log("MSC --> MNCC: callref 0x%x: %s", mncc->callref,
431 get_mncc_name(mncc->msg_type));
432
433 OSMO_ASSERT(cc_to_mncc_tx_expected_msg_type);
434 if (cc_to_mncc_tx_expected_msg_type != mncc->msg_type) {
435 log("Mismatch! Expected MNCC msg type: %s",
436 get_mncc_name(cc_to_mncc_tx_expected_msg_type));
437 abort();
438 }
439
440 if (strcmp(cc_to_mncc_tx_expected_imsi, mncc->imsi)) {
441 log("Mismatch! Expected MNCC msg IMSI: '%s', got '%s'",
442 cc_to_mncc_tx_expected_imsi,
443 mncc->imsi);
444 abort();
445 }
446
447 cc_to_mncc_tx_confirmed = true;
448 cc_to_mncc_tx_got_callref = mncc->callref;
449 cc_to_mncc_tx_expected_imsi = NULL;
450 cc_to_mncc_tx_expected_msg_type = 0;
451 talloc_free(msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100452 return 0;
453}
454
455/* override, requires '-Wl,--wrap=gsup_client_create' */
456struct gsup_client *
457__real_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
458 gsup_client_read_cb_t read_cb,
459 struct oap_client_config *oap_config);
460struct gsup_client *
461__wrap_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
462 gsup_client_read_cb_t read_cb,
463 struct oap_client_config *oap_config)
464{
465 struct gsup_client *gsupc;
466 gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client);
467 OSMO_ASSERT(gsupc);
468 return gsupc;
469}
470
471/* override, requires '-Wl,--wrap=gsup_client_send' */
472int __real_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg);
473int __wrap_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg)
474{
475 const char *is = osmo_hexdump_nospc(msg->data, msg->len);
476 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
477 osmo_gsup_message_type_name(msg->data[0]), is);
478
479 OSMO_ASSERT(gsup_tx_expected);
480 if (strcmp(gsup_tx_expected, is)) {
481 fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected);
482 abort();
483 }
484
485 talloc_free(msg);
486 gsup_tx_confirmed = true;
487 gsup_tx_expected = NULL;
488 return 0;
489}
490
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200491int _validate_dtap(struct msgb *msg, enum ran_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100492{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100493 btw("DTAP --%s--> MS: %s: %s",
494 ran_type_name(to_ran), msg_type_name(msg),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200495 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100496
497 OSMO_ASSERT(dtap_tx_expected);
498 if (msg->len != dtap_tx_expected->len
499 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
500 fprintf(stderr, "Mismatch! Expected:\n%s\n",
501 osmo_hexdump_nospc(dtap_tx_expected->data,
502 dtap_tx_expected->len));
503 abort();
504 }
505
506 btw("DTAP matches expected message");
507
508 talloc_free(msg);
509 dtap_tx_confirmed = true;
510 talloc_free(dtap_tx_expected);
511 dtap_tx_expected = NULL;
512 return 0;
513}
514
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200515/* override, requires '-Wl,--wrap=ranap_iu_tx' */
516int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
517int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200518{
519 return _validate_dtap(msg, RAN_UTRAN_IU);
520}
521
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200522/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
523int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
524int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200525{
526 btw("Iu Release --%s--> MS", ran_type_name(RAN_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200527 OSMO_ASSERT(iu_release_expected);
528 iu_release_expected = false;
529 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200530 return 0;
531}
532
533/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200534int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
535int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200536{
537 btw("Iu Common ID --%s--> MS (IMSI=%s)", ran_type_name(RAN_UTRAN_IU), imsi);
538 return 0;
539}
540
Philipp Maierfbf66102017-04-09 12:32:51 +0200541/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
542int __real_a_iface_tx_dtap(struct msgb *msg);
543int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200544{
545 return _validate_dtap(msg, RAN_GERAN_A);
546}
547
Philipp Maierfbf66102017-04-09 12:32:51 +0200548/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
549int __real_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn);
550int __wrap_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
551{
552 btw("BSSAP Clear --%s--> MS", ran_type_name(RAN_GERAN_A));
553 OSMO_ASSERT(bssap_clear_expected);
554 bssap_clear_expected = false;
555 bssap_clear_sent = true;
556 return 0;
557}
558
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100559/* override, requires '-Wl,--wrap=msc_call_assignment' */
560int __real_msc_call_assignment(struct gsm_trans *trans);
561int __wrap_msc_call_assignment(struct gsm_trans *trans)
562{
563 log("MS <--Call Assignment-- MSC: subscr=%s callref=0x%x",
564 vlr_subscr_name(trans->vsub), trans->callref);
565 return 0;
566}
567
568/* override, requires '-Wl,--wrap=msc_call_release' */
569void __real_msc_call_release(struct gsm_trans *trans);
570void __wrap_msc_call_release(struct gsm_trans *trans)
571{
572 log("MS <--Call Release-- MSC: subscr=%s callref=0x%x",
573 vlr_subscr_name(trans->vsub), trans->callref);
574}
575
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100576static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
577{
578 struct gsm_subscriber_connection *conn = msc_conn_ref;
579 if (send_tmsi == GSM_RESERVED_TMSI)
580 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
581 else
582 btw("sending LU Accept for %s, with TMSI 0x%08x",
583 vlr_subscr_name(conn->vsub), send_tmsi);
584 lu_result_sent |= RES_ACCEPT;
585 return 0;
586}
587
588static int fake_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
589{
590 struct gsm_subscriber_connection *conn = msc_conn_ref;
591 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
592 lu_result_sent |= RES_REJECT;
593 return 0;
594}
595
596static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
597{
598 struct gsm_subscriber_connection *conn = msc_conn_ref;
599 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
600 cm_service_result_sent |= RES_ACCEPT;
601 return 0;
602}
603
604static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref,
605 enum vlr_proc_arq_result result)
606{
607 struct gsm_subscriber_connection *conn = msc_conn_ref;
608 btw("sending CM Service Reject for %s, result %s",
609 vlr_subscr_name(conn->vsub),
610 vlr_proc_arq_result_name(result));
611 cm_service_result_sent |= RES_REJECT;
612 return 0;
613}
614
615static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
616 bool send_autn)
617{
618 struct gsm_subscriber_connection *conn = msc_conn_ref;
619 char *hex;
620 bool ok = true;
621 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
622 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
623 at->use_count, at->key_seq, at->vec.auth_types);
624
625 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
626 btw("...rand=%s", hex);
627 if (!auth_request_expect_rand
628 || strcmp(hex, auth_request_expect_rand) != 0) {
629 ok = false;
630 log("FAILURE: expected rand=%s",
631 auth_request_expect_rand ? auth_request_expect_rand : "-");
632 }
633
634 if (send_autn) {
635 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
636 btw("...autn=%s", hex);
637 if (!auth_request_expect_autn
638 || strcmp(hex, auth_request_expect_autn) != 0) {
639 ok = false;
640 log("FAILURE: expected autn=%s",
641 auth_request_expect_autn ? auth_request_expect_autn : "-");
642 }
643 } else if (auth_request_expect_autn) {
644 ok = false;
645 log("FAILURE: no AUTN sent, expected AUTN = %s",
646 auth_request_expect_autn);
647 }
648
649 if (send_autn)
650 btw("...expecting res=%s",
651 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
652 else
653 btw("...expecting sres=%s",
654 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
655
656 auth_request_sent = ok;
657 return 0;
658}
659
660static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
661{
662 struct gsm_subscriber_connection *conn = msc_conn_ref;
663 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
664 return 0;
665}
666
667static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, enum vlr_ciph ciph,
668 bool retrieve_imeisv)
669{
670 /* FIXME: we actually would like to see the message bytes checked here,
671 * not possible while msc_vlr_set_ciph_mode() calls
672 * gsm0808_cipher_mode() directly. When the MSCSPLIT is ready, check
673 * the tx bytes in the sense of dtap_expect_tx() above. */
674 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200675 switch (conn->via_ran) {
676 case RAN_GERAN_A:
677 btw("sending Ciphering Mode Command for %s: cipher=%s kc=%s"
678 " retrieve_imeisv=%d",
679 vlr_subscr_name(conn->vsub),
680 vlr_ciph_name(conn->network->a5_encryption),
681 osmo_hexdump_nospc(conn->vsub->last_tuple->vec.kc, 8),
682 retrieve_imeisv);
683 break;
684 case RAN_UTRAN_IU:
685 btw("sending SecurityModeControl for %s",
686 vlr_subscr_name(conn->vsub));
687 break;
688 default:
689 btw("UNKNOWN RAN TYPE %d", conn->via_ran);
690 OSMO_ASSERT(false);
691 return -1;
692 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100693 cipher_mode_cmd_sent = true;
694 cipher_mode_cmd_sent_with_imeisv = retrieve_imeisv;
695 return 0;
696}
697
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200698void ms_sends_security_mode_complete()
699{
700 OSMO_ASSERT(g_conn);
701 OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU);
702 OSMO_ASSERT(g_conn->iu.ue_ctx);
703 msc_rx_sec_mode_compl(g_conn);
704}
705
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100706const struct timeval fake_time_start_time = { 123, 456 };
707
708void fake_time_start()
709{
710 osmo_gettimeofday_override_time = fake_time_start_time;
711 osmo_gettimeofday_override = true;
712 fake_time_passes(0, 0);
713}
714
715void check_talloc(void *msgb_ctx, void *tall_bsc_ctx, int expected_blocks)
716{
717 talloc_report_full(msgb_ctx, stderr);
Neels Hofmeyr2c46e042017-11-18 23:29:24 +0100718 /* Expecting these to stick around in tall_bsc_ctx:
719full talloc report on 'msgb' (total 0 bytes in 1 blocks)
720talloc_total_blocks(tall_bsc_ctx) == 9
721full talloc report on 'subscr_conn_test_ctx' (total 2658 bytes in 9 blocks)
722 struct gsup_client contains 248 bytes in 1 blocks (ref 0) 0x61300000dee0
723 struct gsm_network contains 2410 bytes in 6 blocks (ref 0) 0x61700000fce0
724 struct vlr_instance contains 160 bytes in 1 blocks (ref 0) 0x611000009a60
725 no_gsup_server contains 15 bytes in 1 blocks (ref 0) 0x60b00000ade0
726 ../../../src/libosmocore/src/rate_ctr.c:199 contains 1552 bytes in 1 blocks (ref 0) 0x61b00001eae0
727 struct llist_head contains 16 bytes in 1 blocks (ref 0) 0x60b00000ae90
728 .* contains 3 bytes in 1 blocks (ref 0) 0x60b00000af40
729 msgb contains 0 bytes in 1 blocks (ref 0) 0x60800000bf80
730 */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100731 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
732 talloc_total_blocks(tall_bsc_ctx));
733 if (talloc_total_blocks(tall_bsc_ctx) != expected_blocks)
734 talloc_report_full(tall_bsc_ctx, stderr);
735 fprintf(stderr, "\n");
736}
737
738static struct {
739 bool verbose;
740 int run_test_nr;
741} cmdline_opts = {
742 .verbose = false,
743 .run_test_nr = -1,
744};
745
746static void print_help(const char *program)
747{
748 printf("Usage:\n"
749 " %s [-v] [N [N...]]\n"
750 "Options:\n"
751 " -h --help show this text.\n"
752 " -v --verbose print source file and line numbers\n"
753 " N run only the Nth test (first test is N=1)\n",
754 program
755 );
756}
757
758static void handle_options(int argc, char **argv)
759{
760 while (1) {
761 int option_index = 0, c;
762 static struct option long_options[] = {
763 {"help", 0, 0, 'h'},
764 {"verbose", 1, 0, 'v'},
765 {0, 0, 0, 0}
766 };
767
768 c = getopt_long(argc, argv, "hv",
769 long_options, &option_index);
770 if (c == -1)
771 break;
772
773 switch (c) {
774 case 'h':
775 print_help(argv[0]);
776 exit(0);
777 case 'v':
778 cmdline_opts.verbose = true;
779 break;
780 default:
781 /* catch unknown options *as well as* missing arguments. */
782 fprintf(stderr, "Error in command line options. Exiting.\n");
783 exit(-1);
784 break;
785 }
786 }
787}
788
789void *msgb_ctx = NULL;
790
791void run_tests(int nr)
792{
793 int test_nr;
794 nr --; /* arg's first test is 1, in here it's 0 */
795 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr ++) {
796 if (nr >= 0 && test_nr != nr)
797 continue;
798
799 if (cmdline_opts.verbose)
800 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
801
802 msc_vlr_tests[test_nr]();
803
804 if (cmdline_opts.verbose)
805 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
806
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200807 check_talloc(msgb_ctx, tall_bsc_ctx, 9);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100808 } while(0);
809}
810
811int main(int argc, char **argv)
812{
813 handle_options(argc, argv);
814
815 tall_bsc_ctx = talloc_named_const(NULL, 0, "subscr_conn_test_ctx");
816 msgb_ctx = msgb_talloc_ctx_init(tall_bsc_ctx, 0);
817 osmo_init_logging(&info);
818
819 _log_lines = cmdline_opts.verbose;
820
821 OSMO_ASSERT(osmo_stderr_target);
822 log_set_use_color(osmo_stderr_target, 0);
823 log_set_print_timestamp(osmo_stderr_target, 0);
824 log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0);
825 log_set_print_category(osmo_stderr_target, 1);
826
Neels Hofmeyr3f5b7802017-12-15 03:49:55 +0100827 if (cmdline_opts.verbose)
828 log_set_category_filter(osmo_stderr_target, DLSMS, 1, LOGL_DEBUG);
829
Neels Hofmeyra99b4272017-11-21 17:13:23 +0100830 net = gsm_network_init(tall_bsc_ctx, 1, 1, mncc_recv);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100831 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
832 net->gsup_server_port = 0;
833
834 osmo_fsm_log_addr(false);
835 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
836 OSMO_ASSERT(msc_vlr_start(net) == 0);
837 OSMO_ASSERT(net->vlr);
838 OSMO_ASSERT(net->vlr->gsup_client);
839 msc_subscr_conn_init();
840
841 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
842 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
843 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
844 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
845 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
846 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
847 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
848
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200849 clear_vlr();
850
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100851 if (optind >= argc)
852 run_tests(-1);
853 else {
854 int arg;
855 long int nr;
856 for (arg = optind; arg < argc; arg++) {
Neels Hofmeyr9c848b52017-11-22 01:59:36 +0100857 errno = 0;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100858 nr = strtol(argv[arg], NULL, 10);
859 if (errno) {
860 fprintf(stderr, "Invalid argument: %s\n",
861 argv[arg]);
862 exit(1);
863 }
864
865 run_tests(nr);
866 }
867 }
868
869 printf("Done\n");
870
871 talloc_free(the_bts);
872
873 check_talloc(msgb_ctx, tall_bsc_ctx, 9);
874 return 0;
875}