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