blob: d05aa29f4bca9dd60ecaca912172a709c6a9c4d6 [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>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020036
37#if BUILD_IU
Neels Hofmeyr90843962017-09-04 15:04:35 +020038#include <osmocom/msc/iucs_ranap.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020039#include <osmocom/ranap/iu_client.h>
40#else
Neels Hofmeyr90843962017-09-04 15:04:35 +020041#include <osmocom/msc/iu_dummy.h>
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020042#endif
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010043
44#include "msc_vlr_tests.h"
45
46bool _log_lines = false;
47
48struct gsm_network *net = NULL;
49
50struct gsm_bts *the_bts;
51
52const 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 Hofmeyr6a29d322017-01-25 15:04:16 +010071struct msgb *msgb_from_hex(const char *label, uint16_t size, const char *hex)
72{
73 struct msgb *msg = msgb_alloc(size, label);
74 unsigned char *rc;
75 msg->l2h = msg->head;
76 rc = msgb_put(msg, osmo_hexparse(hex, msg->head, msgb_tailroom(msg)));
77 OSMO_ASSERT(rc == msg->l2h);
78 return msg;
79}
80
Neels Hofmeyr78ada642017-03-10 02:15:20 +010081const char *gh_type_name(struct gsm48_hdr *gh)
82{
83 return gsm48_pdisc_msgtype_name(gsm48_hdr_pdisc(gh),
84 gsm48_hdr_msg_type(gh));
85}
86
87const char *msg_type_name(struct msgb *msg)
88{
89 return gh_type_name((void*)msg->data);
90}
91
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010092void dtap_expect_tx(const char *hex)
93{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020094 /* Has the previously expected dtap been received? */
Neels Hofmeyr6a29d322017-01-25 15:04:16 +010095 OSMO_ASSERT(!dtap_tx_expected);
96 if (!hex)
97 return;
98 dtap_tx_expected = msgb_from_hex("dtap_tx_expected", 1024, hex);
99 dtap_tx_confirmed = false;
100}
101
102void dtap_expect_tx_ussd(char *ussd_text)
103{
104 uint8_t ussd_enc[128];
105 int len;
106 /* header */
107 char ussd_msg_hex[128] = "8b2a1c27a225020100302002013b301b04010f0416";
108
109 log("expecting USSD:\n %s", ussd_text);
110 /* append encoded USSD text */
111 gsm_7bit_encode_n_ussd(ussd_enc, sizeof(ussd_enc), ussd_text,
112 &len);
113 strncat(ussd_msg_hex, osmo_hexdump_nospc(ussd_enc, len),
114 sizeof(ussd_msg_hex) - strlen(ussd_msg_hex));
115 dtap_expect_tx(ussd_msg_hex);
116}
117
118int vlr_gsupc_read_cb(struct gsup_client *gsupc, struct msgb *msg);
119
120void gsup_rx(const char *rx_hex, const char *expect_tx_hex)
121{
122 int rc;
123 struct msgb *msg;
124 const char *label;
125
126 gsup_expect_tx(expect_tx_hex);
127
128 msg = msgb_from_hex("gsup", 1024, rx_hex);
129 label = osmo_gsup_message_type_name(msg->l2h[0]);
130 fprintf(stderr, "<-- GSUP rx %s: %s\n", label,
131 osmo_hexdump_nospc(msgb_l2(msg), msgb_l2len(msg)));
132 rc = vlr_gsupc_read_cb(net->vlr->gsup_client, msg);
133 fprintf(stderr, "<-- GSUP rx %s: vlr_gsupc_read_cb() returns %d\n",
134 label, rc);
135 if (expect_tx_hex)
136 OSMO_ASSERT(gsup_tx_confirmed);
137 talloc_free(msg);
138}
139
140bool conn_exists(struct gsm_subscriber_connection *conn)
141{
142 struct gsm_subscriber_connection *c;
143 llist_for_each_entry(c, &net->subscr_conns, entry) {
144 if (c == conn)
145 return true;
146 }
147 return false;
148}
149
150enum ran_type rx_from_ran = RAN_GERAN_A;
151
152struct gsm_subscriber_connection *conn_new(void)
153{
154 struct gsm_subscriber_connection *conn;
155 conn = msc_subscr_con_allocate(net);
156 conn->bts = the_bts;
157 conn->via_ran = rx_from_ran;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200158 if (conn->via_ran == RAN_UTRAN_IU) {
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200159 struct ranap_ue_conn_ctx *ue_ctx = talloc_zero(conn, struct ranap_ue_conn_ctx);
160 *ue_ctx = (struct ranap_ue_conn_ctx){
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200161 .conn_id = 42,
162 };
163 conn->iu.ue_ctx = ue_ctx;
164 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100165 return conn;
166}
167
168struct gsm_subscriber_connection *g_conn = NULL;
169
170void rx_from_ms(struct msgb *msg)
171{
172 int rc;
173
174 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100175
176 log("MSC <--%s-- MS: %s",
177 ran_type_name(rx_from_ran),
178 gh_type_name(gh));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100179
180 if (g_conn && !conn_exists(g_conn))
181 g_conn = NULL;
182
183 if (!g_conn) {
184 log("new conn");
185 g_conn = conn_new();
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200186 rc = msc_compl_l3(g_conn, msg, 23);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100187 if (rc == BSC_API_CONN_POL_REJECT) {
188 msc_subscr_con_free(g_conn);
189 g_conn = NULL;
190 }
191 } else {
192 if ((gsm48_hdr_pdisc(gh) == GSM48_PDISC_RR)
193 && (gsm48_hdr_msg_type(gh) == GSM48_MT_RR_CIPH_M_COMPL))
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200194 msc_cipher_mode_compl(g_conn, msg, 0);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100195 else
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200196 msc_dtap(g_conn, 23, msg);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100197 }
198
199 if (g_conn && !conn_exists(g_conn))
200 g_conn = NULL;
201}
202
203void ms_sends_msg(const char *hex)
204{
205 struct msgb *msg;
206
207 msg = msgb_from_hex("ms_sends_msg", 1024, hex);
208 msg->l1h = msg->l2h = msg->l3h = msg->data;
209 rx_from_ms(msg);
210 talloc_free(msg);
211}
212
213int ms_sends_msg_fake(uint8_t pdisc, uint8_t msg_type)
214{
215 int rc;
216 struct msgb *msg;
217 struct gsm48_hdr *gh;
218
219 msg = msgb_alloc(1024, "ms_sends_msg_fake");
220 msg->l1h = msg->l2h = msg->l3h = msg->data;
221
222 gh = (struct gsm48_hdr*)msgb_put(msg, sizeof(*gh));
223 gh->proto_discr = pdisc;
224 gh->msg_type = msg_type;
225 /* some amount of data, whatever */
226 msgb_put(msg, 123);
227
228 rc = gsm0408_dispatch(g_conn, msg);
229
230 talloc_free(msg);
231 return rc;
232}
233
234void thwart_rx_non_initial_requests()
235{
236 log("requests shall be thwarted");
237 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_CC, GSM48_MT_CC_SETUP) == -EACCES);
238 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_MM, 0x33 /* nonexistent */) == -EACCES);
239 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_RR, GSM48_MT_RR_SYSINFO_1) == -EACCES);
240 OSMO_ASSERT(ms_sends_msg_fake(GSM48_PDISC_SMS, GSM411_MT_CP_DATA) == -EACCES);
241}
242
243void send_sms(struct vlr_subscr *receiver,
244 struct vlr_subscr *sender,
245 char *str)
246{
247 struct gsm_sms *sms = sms_from_text(receiver, sender, 0, str);
248 gsm411_send_sms_subscr(receiver, sms);
249}
250
251unsigned char next_rand_byte = 0;
252/* override, requires '-Wl,--wrap=RAND_bytes' */
253int __real_RAND_bytes(unsigned char *buf, int num);
254int __wrap_RAND_bytes(unsigned char *buf, int num)
255{
256 int i;
257 for (i = 0; i < num; i++)
258 buf[i] = next_rand_byte++;
259 return 1;
260}
261
262/* override, requires '-Wl,--wrap=gsm340_gen_scts' */
263void __real_gsm340_gen_scts(uint8_t *scts, time_t time);
264void __wrap_gsm340_gen_scts(uint8_t *scts, time_t time)
265{
Neels Hofmeyr05230ea2017-08-22 18:06:42 +0200266 /* Write fixed time bytes for deterministic test results */
267 osmo_hexparse("07101000000000", scts, 7);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100268}
269
270const char *paging_expecting_imsi = NULL;
271uint32_t paging_expecting_tmsi;
272bool paging_sent;
273bool paging_stopped;
274
275void paging_expect_imsi(const char *imsi)
276{
277 paging_expecting_imsi = imsi;
278 paging_expecting_tmsi = GSM_RESERVED_TMSI;
279}
280
281void paging_expect_tmsi(uint32_t tmsi)
282{
283 paging_expecting_tmsi = tmsi;
284 paging_expecting_imsi = NULL;
285}
286
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200287int _paging_sent(enum ran_type via_ran, const char *imsi, uint32_t tmsi, uint32_t lac)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100288{
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200289 log("%s sends out paging request to IMSI %s, TMSI 0x%08x, LAC %u",
290 ran_type_name(via_ran), imsi, tmsi, lac);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100291 OSMO_ASSERT(paging_expecting_imsi || (paging_expecting_tmsi != GSM_RESERVED_TMSI));
292 if (paging_expecting_imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200293 VERBOSE_ASSERT(strcmp(paging_expecting_imsi, imsi), == 0, "%d");
294 if (paging_expecting_tmsi != GSM_RESERVED_TMSI) {
295 VERBOSE_ASSERT(paging_expecting_tmsi, == tmsi, "0x%08x");
296 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100297 paging_sent = true;
298 paging_stopped = false;
299 return 1;
300}
301
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200302/* override, requires '-Wl,--wrap=ranap_iu_page_cs' */
303int __real_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac);
304int __wrap_ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200305{
306 return _paging_sent(RAN_UTRAN_IU, imsi, tmsi ? *tmsi : GSM_RESERVED_TMSI, lac);
307}
308
Philipp Maierfbf66102017-04-09 12:32:51 +0200309/* override, requires '-Wl,--wrap=a_iface_tx_paging' */
310int __real_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac);
311int __wrap_a_iface_tx_paging(const char *imsi, uint32_t tmsi, uint16_t lac)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200312{
313 return _paging_sent(RAN_GERAN_A, imsi, tmsi, lac);
314}
315
316/* override, requires '-Wl,--wrap=msc_stop_paging' */
317void __real_msc_stop_paging(struct vlr_subscr *vsub);
318void __wrap_msc_stop_paging(struct vlr_subscr *vsub)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100319{
320 paging_stopped = true;
321}
322
323void clear_vlr()
324{
325 struct vlr_subscr *vsub, *n;
326 llist_for_each_entry_safe(vsub, n, &net->vlr->subscribers, list) {
327 vlr_subscr_free(vsub);
328 }
329
330 net->authentication_required = false;
331 net->a5_encryption = VLR_CIPH_NONE;
332 net->vlr->cfg.check_imei_rqd = false;
333 net->vlr->cfg.assign_tmsi = false;
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200334 net->vlr->cfg.retrieve_imeisv_early = false;
335 net->vlr->cfg.retrieve_imeisv_ciphered = false;
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100336
337 rx_from_ran = RAN_GERAN_A;
338 auth_request_sent = false;
339 auth_request_expect_rand = NULL;
340 auth_request_expect_autn = NULL;
341
342 next_rand_byte = 0;
343
Philipp Maierfbf66102017-04-09 12:32:51 +0200344 iu_release_expected = false;
345 iu_release_sent = false;
346 bssap_clear_expected = false;
347 bssap_clear_sent = false;
348
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100349 osmo_gettimeofday_override = false;
350}
351
352static struct log_info_cat test_categories[] = {
353 [DMSC] = {
354 .name = "DMSC",
355 .description = "Mobile Switching Center",
356 .enabled = 1, .loglevel = LOGL_DEBUG,
357 },
358 [DRLL] = {
359 .name = "DRLL",
360 .description = "A-bis Radio Link Layer (RLL)",
361 .enabled = 1, .loglevel = LOGL_DEBUG,
362 },
363 [DMM] = {
364 .name = "DMM",
365 .description = "Layer3 Mobility Management (MM)",
366 .enabled = 1, .loglevel = LOGL_DEBUG,
367 },
368 [DRR] = {
369 .name = "DRR",
370 .description = "Layer3 Radio Resource (RR)",
371 .enabled = 1, .loglevel = LOGL_DEBUG,
372 },
373 [DCC] = {
374 .name = "DCC",
375 .description = "Layer3 Call Control (CC)",
376 .enabled = 1, .loglevel = LOGL_NOTICE,
377 },
378 [DMM] = {
379 .name = "DMM",
380 .description = "Layer3 Mobility Management (MM)",
381 .enabled = 1, .loglevel = LOGL_DEBUG,
382 },
383 [DVLR] = {
384 .name = "DVLR",
385 .description = "Visitor Location Register",
386 .enabled = 1, .loglevel = LOGL_DEBUG,
387 },
388 [DREF] = {
389 .name = "DREF",
390 .description = "Reference Counting",
391 .enabled = 1, .loglevel = LOGL_DEBUG,
392 },
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200393 [DPAG] = {
394 .name = "DPAG",
395 .description = "Paging Subsystem",
396 .enabled = 1, .loglevel = LOGL_DEBUG,
397 },
398 [DIUCS] = {
399 .name = "DIUCS",
400 .description = "Iu-CS Protocol",
401 .enabled = 1, .loglevel = LOGL_DEBUG,
402 },
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100403};
404
405static struct log_info info = {
406 .cat = test_categories,
407 .num_cat = ARRAY_SIZE(test_categories),
408};
409
410extern void *tall_bsc_ctx;
411
412int fake_mncc_recv(struct gsm_network *net, struct msgb *msg)
413{
414 fprintf(stderr, "rx MNCC\n");
415 return 0;
416}
417
418/* override, requires '-Wl,--wrap=gsup_client_create' */
419struct gsup_client *
420__real_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
421 gsup_client_read_cb_t read_cb,
422 struct oap_client_config *oap_config);
423struct gsup_client *
424__wrap_gsup_client_create(const char *ip_addr, unsigned int tcp_port,
425 gsup_client_read_cb_t read_cb,
426 struct oap_client_config *oap_config)
427{
428 struct gsup_client *gsupc;
429 gsupc = talloc_zero(tall_bsc_ctx, struct gsup_client);
430 OSMO_ASSERT(gsupc);
431 return gsupc;
432}
433
434/* override, requires '-Wl,--wrap=gsup_client_send' */
435int __real_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg);
436int __wrap_gsup_client_send(struct gsup_client *gsupc, struct msgb *msg)
437{
438 const char *is = osmo_hexdump_nospc(msg->data, msg->len);
439 fprintf(stderr, "GSUP --> HLR: %s: %s\n",
440 osmo_gsup_message_type_name(msg->data[0]), is);
441
442 OSMO_ASSERT(gsup_tx_expected);
443 if (strcmp(gsup_tx_expected, is)) {
444 fprintf(stderr, "Mismatch! Expected:\n%s\n", gsup_tx_expected);
445 abort();
446 }
447
448 talloc_free(msg);
449 gsup_tx_confirmed = true;
450 gsup_tx_expected = NULL;
451 return 0;
452}
453
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200454int _validate_dtap(struct msgb *msg, enum ran_type to_ran)
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100455{
Neels Hofmeyr78ada642017-03-10 02:15:20 +0100456 btw("DTAP --%s--> MS: %s: %s",
457 ran_type_name(to_ran), msg_type_name(msg),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200458 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100459
460 OSMO_ASSERT(dtap_tx_expected);
461 if (msg->len != dtap_tx_expected->len
462 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
463 fprintf(stderr, "Mismatch! Expected:\n%s\n",
464 osmo_hexdump_nospc(dtap_tx_expected->data,
465 dtap_tx_expected->len));
466 abort();
467 }
468
469 btw("DTAP matches expected message");
470
471 talloc_free(msg);
472 dtap_tx_confirmed = true;
473 talloc_free(dtap_tx_expected);
474 dtap_tx_expected = NULL;
475 return 0;
476}
477
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200478/* override, requires '-Wl,--wrap=ranap_iu_tx' */
479int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
480int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200481{
482 return _validate_dtap(msg, RAN_UTRAN_IU);
483}
484
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200485/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
486int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
487int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200488{
489 btw("Iu Release --%s--> MS", ran_type_name(RAN_UTRAN_IU));
Philipp Maierfbf66102017-04-09 12:32:51 +0200490 OSMO_ASSERT(iu_release_expected);
491 iu_release_expected = false;
492 iu_release_sent = true;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200493 return 0;
494}
495
496/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr00e82d62017-07-05 15:19:52 +0200497int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
498int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200499{
500 btw("Iu Common ID --%s--> MS (IMSI=%s)", ran_type_name(RAN_UTRAN_IU), imsi);
501 return 0;
502}
503
Philipp Maierfbf66102017-04-09 12:32:51 +0200504/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
505int __real_a_iface_tx_dtap(struct msgb *msg);
506int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200507{
508 return _validate_dtap(msg, RAN_GERAN_A);
509}
510
Philipp Maierfbf66102017-04-09 12:32:51 +0200511/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
512int __real_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn);
513int __wrap_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
514{
515 btw("BSSAP Clear --%s--> MS", ran_type_name(RAN_GERAN_A));
516 OSMO_ASSERT(bssap_clear_expected);
517 bssap_clear_expected = false;
518 bssap_clear_sent = true;
519 return 0;
520}
521
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100522static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
523{
524 struct gsm_subscriber_connection *conn = msc_conn_ref;
525 if (send_tmsi == GSM_RESERVED_TMSI)
526 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
527 else
528 btw("sending LU Accept for %s, with TMSI 0x%08x",
529 vlr_subscr_name(conn->vsub), send_tmsi);
530 lu_result_sent |= RES_ACCEPT;
531 return 0;
532}
533
534static int fake_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
535{
536 struct gsm_subscriber_connection *conn = msc_conn_ref;
537 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
538 lu_result_sent |= RES_REJECT;
539 return 0;
540}
541
542static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
543{
544 struct gsm_subscriber_connection *conn = msc_conn_ref;
545 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
546 cm_service_result_sent |= RES_ACCEPT;
547 return 0;
548}
549
550static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref,
551 enum vlr_proc_arq_result result)
552{
553 struct gsm_subscriber_connection *conn = msc_conn_ref;
554 btw("sending CM Service Reject for %s, result %s",
555 vlr_subscr_name(conn->vsub),
556 vlr_proc_arq_result_name(result));
557 cm_service_result_sent |= RES_REJECT;
558 return 0;
559}
560
561static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
562 bool send_autn)
563{
564 struct gsm_subscriber_connection *conn = msc_conn_ref;
565 char *hex;
566 bool ok = true;
567 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
568 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
569 at->use_count, at->key_seq, at->vec.auth_types);
570
571 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
572 btw("...rand=%s", hex);
573 if (!auth_request_expect_rand
574 || strcmp(hex, auth_request_expect_rand) != 0) {
575 ok = false;
576 log("FAILURE: expected rand=%s",
577 auth_request_expect_rand ? auth_request_expect_rand : "-");
578 }
579
580 if (send_autn) {
581 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
582 btw("...autn=%s", hex);
583 if (!auth_request_expect_autn
584 || strcmp(hex, auth_request_expect_autn) != 0) {
585 ok = false;
586 log("FAILURE: expected autn=%s",
587 auth_request_expect_autn ? auth_request_expect_autn : "-");
588 }
589 } else if (auth_request_expect_autn) {
590 ok = false;
591 log("FAILURE: no AUTN sent, expected AUTN = %s",
592 auth_request_expect_autn);
593 }
594
595 if (send_autn)
596 btw("...expecting res=%s",
597 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
598 else
599 btw("...expecting sres=%s",
600 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
601
602 auth_request_sent = ok;
603 return 0;
604}
605
606static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
607{
608 struct gsm_subscriber_connection *conn = msc_conn_ref;
609 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
610 return 0;
611}
612
613static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, enum vlr_ciph ciph,
614 bool retrieve_imeisv)
615{
616 /* FIXME: we actually would like to see the message bytes checked here,
617 * not possible while msc_vlr_set_ciph_mode() calls
618 * gsm0808_cipher_mode() directly. When the MSCSPLIT is ready, check
619 * the tx bytes in the sense of dtap_expect_tx() above. */
620 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200621 switch (conn->via_ran) {
622 case RAN_GERAN_A:
623 btw("sending Ciphering Mode Command for %s: cipher=%s kc=%s"
624 " retrieve_imeisv=%d",
625 vlr_subscr_name(conn->vsub),
626 vlr_ciph_name(conn->network->a5_encryption),
627 osmo_hexdump_nospc(conn->vsub->last_tuple->vec.kc, 8),
628 retrieve_imeisv);
629 break;
630 case RAN_UTRAN_IU:
631 btw("sending SecurityModeControl for %s",
632 vlr_subscr_name(conn->vsub));
633 break;
634 default:
635 btw("UNKNOWN RAN TYPE %d", conn->via_ran);
636 OSMO_ASSERT(false);
637 return -1;
638 }
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100639 cipher_mode_cmd_sent = true;
640 cipher_mode_cmd_sent_with_imeisv = retrieve_imeisv;
641 return 0;
642}
643
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200644void ms_sends_security_mode_complete()
645{
646 OSMO_ASSERT(g_conn);
647 OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU);
648 OSMO_ASSERT(g_conn->iu.ue_ctx);
649 msc_rx_sec_mode_compl(g_conn);
650}
651
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100652const struct timeval fake_time_start_time = { 123, 456 };
653
654void fake_time_start()
655{
656 osmo_gettimeofday_override_time = fake_time_start_time;
657 osmo_gettimeofday_override = true;
658 fake_time_passes(0, 0);
659}
660
661void check_talloc(void *msgb_ctx, void *tall_bsc_ctx, int expected_blocks)
662{
663 talloc_report_full(msgb_ctx, stderr);
664 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
665 talloc_total_blocks(tall_bsc_ctx));
666 if (talloc_total_blocks(tall_bsc_ctx) != expected_blocks)
667 talloc_report_full(tall_bsc_ctx, stderr);
668 fprintf(stderr, "\n");
669}
670
671static struct {
672 bool verbose;
673 int run_test_nr;
674} cmdline_opts = {
675 .verbose = false,
676 .run_test_nr = -1,
677};
678
679static void print_help(const char *program)
680{
681 printf("Usage:\n"
682 " %s [-v] [N [N...]]\n"
683 "Options:\n"
684 " -h --help show this text.\n"
685 " -v --verbose print source file and line numbers\n"
686 " N run only the Nth test (first test is N=1)\n",
687 program
688 );
689}
690
691static void handle_options(int argc, char **argv)
692{
693 while (1) {
694 int option_index = 0, c;
695 static struct option long_options[] = {
696 {"help", 0, 0, 'h'},
697 {"verbose", 1, 0, 'v'},
698 {0, 0, 0, 0}
699 };
700
701 c = getopt_long(argc, argv, "hv",
702 long_options, &option_index);
703 if (c == -1)
704 break;
705
706 switch (c) {
707 case 'h':
708 print_help(argv[0]);
709 exit(0);
710 case 'v':
711 cmdline_opts.verbose = true;
712 break;
713 default:
714 /* catch unknown options *as well as* missing arguments. */
715 fprintf(stderr, "Error in command line options. Exiting.\n");
716 exit(-1);
717 break;
718 }
719 }
720}
721
722void *msgb_ctx = NULL;
723
724void run_tests(int nr)
725{
726 int test_nr;
727 nr --; /* arg's first test is 1, in here it's 0 */
728 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr ++) {
729 if (nr >= 0 && test_nr != nr)
730 continue;
731
732 if (cmdline_opts.verbose)
733 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
734
735 msc_vlr_tests[test_nr]();
736
737 if (cmdline_opts.verbose)
738 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
739
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200740 check_talloc(msgb_ctx, tall_bsc_ctx, 9);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100741 } while(0);
742}
743
744int main(int argc, char **argv)
745{
746 handle_options(argc, argv);
747
748 tall_bsc_ctx = talloc_named_const(NULL, 0, "subscr_conn_test_ctx");
749 msgb_ctx = msgb_talloc_ctx_init(tall_bsc_ctx, 0);
750 osmo_init_logging(&info);
751
752 _log_lines = cmdline_opts.verbose;
753
754 OSMO_ASSERT(osmo_stderr_target);
755 log_set_use_color(osmo_stderr_target, 0);
756 log_set_print_timestamp(osmo_stderr_target, 0);
757 log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0);
758 log_set_print_category(osmo_stderr_target, 1);
759
760 net = gsm_network_init(tall_bsc_ctx, 1, 1, fake_mncc_recv);
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100761 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
762 net->gsup_server_port = 0;
763
764 osmo_fsm_log_addr(false);
765 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
766 OSMO_ASSERT(msc_vlr_start(net) == 0);
767 OSMO_ASSERT(net->vlr);
768 OSMO_ASSERT(net->vlr->gsup_client);
769 msc_subscr_conn_init();
770
771 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
772 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
773 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
774 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
775 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
776 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
777 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
778
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200779 clear_vlr();
780
Neels Hofmeyr6a29d322017-01-25 15:04:16 +0100781 if (optind >= argc)
782 run_tests(-1);
783 else {
784 int arg;
785 long int nr;
786 for (arg = optind; arg < argc; arg++) {
787 nr = strtol(argv[arg], NULL, 10);
788 if (errno) {
789 fprintf(stderr, "Invalid argument: %s\n",
790 argv[arg]);
791 exit(1);
792 }
793
794 run_tests(nr);
795 }
796 }
797
798 printf("Done\n");
799
800 talloc_free(the_bts);
801
802 check_talloc(msgb_ctx, tall_bsc_ctx, 9);
803 return 0;
804}