blob: 71f971352bf0e9ab927350098eff8517e2400553 [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 btw("DTAP --%s--> MS: %s: %s",
458 ran_type_name(to_ran), msg_type_name(msg),
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200459 osmo_hexdump_nospc(msg->data, msg->len));
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100460
461 OSMO_ASSERT(dtap_tx_expected);
462 if (msg->len != dtap_tx_expected->len
463 || memcmp(msg->data, dtap_tx_expected->data, msg->len)) {
464 fprintf(stderr, "Mismatch! Expected:\n%s\n",
465 osmo_hexdump_nospc(dtap_tx_expected->data,
466 dtap_tx_expected->len));
467 abort();
468 }
469
470 btw("DTAP matches expected message");
471
472 talloc_free(msg);
473 dtap_tx_confirmed = true;
474 talloc_free(dtap_tx_expected);
475 dtap_tx_expected = NULL;
476 return 0;
477}
478
Neels Hofmeyr5e40da62017-07-05 15:19:52 +0200479/* override, requires '-Wl,--wrap=ranap_iu_tx' */
480int __real_ranap_iu_tx(struct msgb *msg, uint8_t sapi);
481int __wrap_ranap_iu_tx(struct msgb *msg, uint8_t sapi)
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200482{
483 return _validate_dtap(msg, RAN_UTRAN_IU);
484}
485
Neels Hofmeyr5e40da62017-07-05 15:19:52 +0200486/* override, requires '-Wl,--wrap=ranap_iu_tx_release' */
487int __real_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause);
488int __wrap_ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200489{
490 btw("Iu Release --%s--> MS", ran_type_name(RAN_UTRAN_IU));
Philipp Maier68760a82017-04-09 12:32:51 +0200491 OSMO_ASSERT(iu_release_expected);
492 iu_release_expected = false;
493 iu_release_sent = true;
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200494 return 0;
495}
496
497/* override, requires '-Wl,--wrap=iu_tx_common_id' */
Neels Hofmeyr5e40da62017-07-05 15:19:52 +0200498int __real_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi);
499int __wrap_ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *ue_ctx, const char *imsi)
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200500{
501 btw("Iu Common ID --%s--> MS (IMSI=%s)", ran_type_name(RAN_UTRAN_IU), imsi);
502 return 0;
503}
504
Philipp Maier68760a82017-04-09 12:32:51 +0200505/* override, requires '-Wl,--wrap=a_iface_tx_dtap' */
506int __real_a_iface_tx_dtap(struct msgb *msg);
507int __wrap_a_iface_tx_dtap(struct msgb *msg)
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200508{
509 return _validate_dtap(msg, RAN_GERAN_A);
510}
511
Philipp Maier68760a82017-04-09 12:32:51 +0200512/* override, requires '-Wl,--wrap=a_iface_tx_clear_cmd' */
513int __real_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn);
514int __wrap_a_iface_tx_clear_cmd(struct gsm_subscriber_connection *conn)
515{
516 btw("BSSAP Clear --%s--> MS", ran_type_name(RAN_GERAN_A));
517 OSMO_ASSERT(bssap_clear_expected);
518 bssap_clear_expected = false;
519 bssap_clear_sent = true;
520 return 0;
521}
522
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100523static int fake_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
524{
525 struct gsm_subscriber_connection *conn = msc_conn_ref;
526 if (send_tmsi == GSM_RESERVED_TMSI)
527 btw("sending LU Accept for %s", vlr_subscr_name(conn->vsub));
528 else
529 btw("sending LU Accept for %s, with TMSI 0x%08x",
530 vlr_subscr_name(conn->vsub), send_tmsi);
531 lu_result_sent |= RES_ACCEPT;
532 return 0;
533}
534
535static int fake_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
536{
537 struct gsm_subscriber_connection *conn = msc_conn_ref;
538 btw("sending LU Reject for %s, cause %u", vlr_subscr_name(conn->vsub), cause);
539 lu_result_sent |= RES_REJECT;
540 return 0;
541}
542
543static int fake_vlr_tx_cm_serv_acc(void *msc_conn_ref)
544{
545 struct gsm_subscriber_connection *conn = msc_conn_ref;
546 btw("sending CM Service Accept for %s", vlr_subscr_name(conn->vsub));
547 cm_service_result_sent |= RES_ACCEPT;
548 return 0;
549}
550
551static int fake_vlr_tx_cm_serv_rej(void *msc_conn_ref,
552 enum vlr_proc_arq_result result)
553{
554 struct gsm_subscriber_connection *conn = msc_conn_ref;
555 btw("sending CM Service Reject for %s, result %s",
556 vlr_subscr_name(conn->vsub),
557 vlr_proc_arq_result_name(result));
558 cm_service_result_sent |= RES_REJECT;
559 return 0;
560}
561
562static int fake_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
563 bool send_autn)
564{
565 struct gsm_subscriber_connection *conn = msc_conn_ref;
566 char *hex;
567 bool ok = true;
568 btw("sending %s Auth Request for %s: tuple use_count=%d key_seq=%d auth_types=0x%x and...",
569 send_autn? "UMTS" : "GSM", vlr_subscr_name(conn->vsub),
570 at->use_count, at->key_seq, at->vec.auth_types);
571
572 hex = osmo_hexdump_nospc((void*)&at->vec.rand, sizeof(at->vec.rand));
573 btw("...rand=%s", hex);
574 if (!auth_request_expect_rand
575 || strcmp(hex, auth_request_expect_rand) != 0) {
576 ok = false;
577 log("FAILURE: expected rand=%s",
578 auth_request_expect_rand ? auth_request_expect_rand : "-");
579 }
580
581 if (send_autn) {
582 hex = osmo_hexdump_nospc((void*)&at->vec.autn, sizeof(at->vec.autn));
583 btw("...autn=%s", hex);
584 if (!auth_request_expect_autn
585 || strcmp(hex, auth_request_expect_autn) != 0) {
586 ok = false;
587 log("FAILURE: expected autn=%s",
588 auth_request_expect_autn ? auth_request_expect_autn : "-");
589 }
590 } else if (auth_request_expect_autn) {
591 ok = false;
592 log("FAILURE: no AUTN sent, expected AUTN = %s",
593 auth_request_expect_autn);
594 }
595
596 if (send_autn)
597 btw("...expecting res=%s",
598 osmo_hexdump_nospc((void*)&at->vec.res, at->vec.res_len));
599 else
600 btw("...expecting sres=%s",
601 osmo_hexdump_nospc((void*)&at->vec.sres, sizeof(at->vec.sres)));
602
603 auth_request_sent = ok;
604 return 0;
605}
606
607static int fake_vlr_tx_auth_rej(void *msc_conn_ref)
608{
609 struct gsm_subscriber_connection *conn = msc_conn_ref;
610 btw("sending Auth Reject for %s", vlr_subscr_name(conn->vsub));
611 return 0;
612}
613
614static int fake_vlr_tx_ciph_mode_cmd(void *msc_conn_ref, enum vlr_ciph ciph,
615 bool retrieve_imeisv)
616{
617 /* FIXME: we actually would like to see the message bytes checked here,
618 * not possible while msc_vlr_set_ciph_mode() calls
619 * gsm0808_cipher_mode() directly. When the MSCSPLIT is ready, check
620 * the tx bytes in the sense of dtap_expect_tx() above. */
621 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200622 switch (conn->via_ran) {
623 case RAN_GERAN_A:
624 btw("sending Ciphering Mode Command for %s: cipher=%s kc=%s"
625 " retrieve_imeisv=%d",
626 vlr_subscr_name(conn->vsub),
627 vlr_ciph_name(conn->network->a5_encryption),
628 osmo_hexdump_nospc(conn->vsub->last_tuple->vec.kc, 8),
629 retrieve_imeisv);
630 break;
631 case RAN_UTRAN_IU:
632 btw("sending SecurityModeControl for %s",
633 vlr_subscr_name(conn->vsub));
634 break;
635 default:
636 btw("UNKNOWN RAN TYPE %d", conn->via_ran);
637 OSMO_ASSERT(false);
638 return -1;
639 }
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100640 cipher_mode_cmd_sent = true;
641 cipher_mode_cmd_sent_with_imeisv = retrieve_imeisv;
642 return 0;
643}
644
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200645void ms_sends_security_mode_complete()
646{
647 OSMO_ASSERT(g_conn);
648 OSMO_ASSERT(g_conn->via_ran == RAN_UTRAN_IU);
649 OSMO_ASSERT(g_conn->iu.ue_ctx);
650 msc_rx_sec_mode_compl(g_conn);
651}
652
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100653const struct timeval fake_time_start_time = { 123, 456 };
654
655void fake_time_start()
656{
657 osmo_gettimeofday_override_time = fake_time_start_time;
658 osmo_gettimeofday_override = true;
659 fake_time_passes(0, 0);
660}
661
662void check_talloc(void *msgb_ctx, void *tall_bsc_ctx, int expected_blocks)
663{
664 talloc_report_full(msgb_ctx, stderr);
665 fprintf(stderr, "talloc_total_blocks(tall_bsc_ctx) == %zu\n",
666 talloc_total_blocks(tall_bsc_ctx));
667 if (talloc_total_blocks(tall_bsc_ctx) != expected_blocks)
668 talloc_report_full(tall_bsc_ctx, stderr);
669 fprintf(stderr, "\n");
670}
671
672static struct {
673 bool verbose;
674 int run_test_nr;
675} cmdline_opts = {
676 .verbose = false,
677 .run_test_nr = -1,
678};
679
680static void print_help(const char *program)
681{
682 printf("Usage:\n"
683 " %s [-v] [N [N...]]\n"
684 "Options:\n"
685 " -h --help show this text.\n"
686 " -v --verbose print source file and line numbers\n"
687 " N run only the Nth test (first test is N=1)\n",
688 program
689 );
690}
691
692static void handle_options(int argc, char **argv)
693{
694 while (1) {
695 int option_index = 0, c;
696 static struct option long_options[] = {
697 {"help", 0, 0, 'h'},
698 {"verbose", 1, 0, 'v'},
699 {0, 0, 0, 0}
700 };
701
702 c = getopt_long(argc, argv, "hv",
703 long_options, &option_index);
704 if (c == -1)
705 break;
706
707 switch (c) {
708 case 'h':
709 print_help(argv[0]);
710 exit(0);
711 case 'v':
712 cmdline_opts.verbose = true;
713 break;
714 default:
715 /* catch unknown options *as well as* missing arguments. */
716 fprintf(stderr, "Error in command line options. Exiting.\n");
717 exit(-1);
718 break;
719 }
720 }
721}
722
723void *msgb_ctx = NULL;
724
725void run_tests(int nr)
726{
727 int test_nr;
728 nr --; /* arg's first test is 1, in here it's 0 */
729 for (test_nr = 0; msc_vlr_tests[test_nr]; test_nr ++) {
730 if (nr >= 0 && test_nr != nr)
731 continue;
732
733 if (cmdline_opts.verbose)
734 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
735
736 msc_vlr_tests[test_nr]();
737
738 if (cmdline_opts.verbose)
739 fprintf(stderr, "(test nr %d)\n", test_nr + 1);
740
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200741 check_talloc(msgb_ctx, tall_bsc_ctx, 9);
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100742 } while(0);
743}
744
745int main(int argc, char **argv)
746{
747 handle_options(argc, argv);
748
749 tall_bsc_ctx = talloc_named_const(NULL, 0, "subscr_conn_test_ctx");
750 msgb_ctx = msgb_talloc_ctx_init(tall_bsc_ctx, 0);
751 osmo_init_logging(&info);
752
753 _log_lines = cmdline_opts.verbose;
754
755 OSMO_ASSERT(osmo_stderr_target);
756 log_set_use_color(osmo_stderr_target, 0);
757 log_set_print_timestamp(osmo_stderr_target, 0);
758 log_set_print_filename(osmo_stderr_target, _log_lines? 1 : 0);
759 log_set_print_category(osmo_stderr_target, 1);
760
761 net = gsm_network_init(tall_bsc_ctx, 1, 1, fake_mncc_recv);
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100762 net->gsup_server_addr_str = talloc_strdup(net, "no_gsup_server");
763 net->gsup_server_port = 0;
764
765 osmo_fsm_log_addr(false);
766 OSMO_ASSERT(msc_vlr_alloc(net) == 0);
767 OSMO_ASSERT(msc_vlr_start(net) == 0);
768 OSMO_ASSERT(net->vlr);
769 OSMO_ASSERT(net->vlr->gsup_client);
770 msc_subscr_conn_init();
771
772 net->vlr->ops.tx_lu_acc = fake_vlr_tx_lu_acc;
773 net->vlr->ops.tx_lu_rej = fake_vlr_tx_lu_rej;
774 net->vlr->ops.tx_cm_serv_acc = fake_vlr_tx_cm_serv_acc;
775 net->vlr->ops.tx_cm_serv_rej = fake_vlr_tx_cm_serv_rej;
776 net->vlr->ops.tx_auth_req = fake_vlr_tx_auth_req;
777 net->vlr->ops.tx_auth_rej = fake_vlr_tx_auth_rej;
778 net->vlr->ops.set_ciph_mode = fake_vlr_tx_ciph_mode_cmd;
779
Neels Hofmeyrd489ea32016-05-20 21:59:55 +0200780 clear_vlr();
781
Neels Hofmeyr3dc2c642017-01-25 15:04:16 +0100782 if (optind >= argc)
783 run_tests(-1);
784 else {
785 int arg;
786 long int nr;
787 for (arg = optind; arg < argc; arg++) {
788 nr = strtol(argv[arg], NULL, 10);
789 if (errno) {
790 fprintf(stderr, "Invalid argument: %s\n",
791 argv[arg]);
792 exit(1);
793 }
794
795 run_tests(nr);
796 }
797 }
798
799 printf("Done\n");
800
801 talloc_free(the_bts);
802
803 check_talloc(msgb_ctx, tall_bsc_ctx, 9);
804 return 0;
805}