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