blob: b08c31d96925650ae03a9bfb7dc0b685b2fbdf9b [file] [log] [blame]
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +02001/* Code for a software PCU to test a SGSN.. */
2
3/* (C) 2013 by Holger Hans Peter Freyther
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22extern "C" {
23#include <osmocom/core/talloc.h>
24#include <pcu_vty.h>
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +010025#include <alloc_algo.h>
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020026}
27
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +020028#include "gprs_tests.h"
29
30
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020031#include <gprs_bssgp_pcu.h>
32#include <gprs_rlcmac.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020033#include <bts.h>
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020034
35#include <stdlib.h>
36#include <sys/types.h>
37#include <sys/socket.h>
38
Jacob Erlbeck0494c752015-05-04 09:34:00 +020039static size_t current_test;
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +020040
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020041/* Extern data to please the underlying code */
42void *tall_pcu_ctx;
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020043int16_t spoof_mnc = 0, spoof_mcc = 0;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010044bool spoof_mnc_3_digits = false;
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020045
Holger Hans Peter Freyther741481d2013-07-28 21:14:51 +020046extern void test_replay_gprs_attach(struct gprs_bssgp_pcu *pcu);
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +020047extern void test_replay_gprs_data(struct gprs_bssgp_pcu *, struct msgb *, struct tlv_parsed *);
Holger Hans Peter Freyther741481d2013-07-28 21:14:51 +020048
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +020049extern void test_pdp_activation_start(struct gprs_bssgp_pcu *pcu);
50extern void test_pdp_activation_data(struct gprs_bssgp_pcu *, struct msgb *, struct tlv_parsed*);
51
52struct gprs_test all_tests[] = {
53 gprs_test("gprs_attach_with_tmsi",
54 "A simple test that verifies that N(U) is "
55 "increasing across various messages. This makes "
56 "sure that no new LLE/LLME is created on the fly.",
57 test_replay_gprs_attach,
58 test_replay_gprs_data),
59 gprs_test("gprs_full_attach_pdp_activation",
60 "A simple test to do a GPRS attach and open a PDP "
61 "context. Then goes to sleep and waits for you to ping "
62 "the connection and hopefully re-produce a crash.",
63 test_pdp_activation_start,
64 test_pdp_activation_data),
65};
66
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +010067static void init_main_bts(struct gprs_rlcmac_bts *bts)
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020068{
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020069 bts->initial_cs_dl = bts->initial_cs_ul = 1;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +010070 bts->cs_mask = 1 << 0; /* CS-1 always enabled by default */
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020071 bts->n3101 = 10;
72 bts->n3103 = 4;
73 bts->n3105 = 8;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +010074}
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020075
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +010076static void init_pcu(struct gprs_pcu *pcu)
77{
78 if (!pcu->alloc_algorithm)
79 pcu->alloc_algorithm = alloc_algorithm_b;
Pau Espin Pedrole8912222021-01-14 14:03:17 +010080 pcu->vty.fc_interval = 100;
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020081}
82
Holger Hans Peter Freyther741481d2013-07-28 21:14:51 +020083static void bvci_unblocked(struct gprs_bssgp_pcu *pcu)
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020084{
85 printf("BVCI unblocked. We can begin with test cases.\n");
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +020086 all_tests[current_test].start(pcu);
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020087}
88
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +020089static void bssgp_data(struct gprs_bssgp_pcu *pcu, struct msgb *msg, struct tlv_parsed *tp)
90{
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +020091 all_tests[current_test].data(pcu, msg, tp);
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +020092}
93
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +020094void create_and_connect_bssgp(struct gprs_rlcmac_bts *bts,
95 uint32_t sgsn_ip, uint16_t sgsn_port)
96{
97 struct gprs_bssgp_pcu *pcu;
Alexander Couzens290d9032020-09-16 21:52:02 +020098 struct osmo_sockaddr local, remote;
Alexander Couzens5bece2a2020-10-12 02:27:22 +020099 uint16_t nsvci = 20;
100 uint16_t nsei = 20;
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200101
Alexander Couzens290d9032020-09-16 21:52:02 +0200102 local.u.sin.sin_family = AF_INET;
103 local.u.sin.sin_addr.s_addr = 0;
104 local.u.sin.sin_port = 0;
105
106 remote.u.sin.sin_family = AF_INET;
107 remote.u.sin.sin_addr.s_addr = htonl(sgsn_ip);
108 remote.u.sin.sin_port = htons(sgsn_port);
109
110 pcu = gprs_bssgp_init(bts, 20, 20, 901, 99, false, 1, 0, 0);
Alexander Couzens94a367f2021-02-16 16:57:15 +0100111 gprs_ns_update_config(bts, nsei, &local, &remote, &nsvci, 1);
Alexander Couzens290d9032020-09-16 21:52:02 +0200112
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200113 pcu->on_unblock_ack = bvci_unblocked;
Holger Hans Peter Freytherbc1e52c2013-08-22 08:44:38 +0200114 pcu->on_dl_unit_data = bssgp_data;
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200115}
116
117int main(int argc, char **argv)
118{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100119 struct gprs_pcu *pcu = gprs_pcu_alloc(tall_pcu_ctx);
120 the_pcu = pcu; /* globally avaialable object */
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100121 struct gprs_rlcmac_bts *bts = bts_alloc(pcu, 0);
Alexander Couzens290d9032020-09-16 21:52:02 +0200122
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200123 tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile Emu-PCU context");
124 if (!tall_pcu_ctx)
125 abort();
126
Neels Hofmeyr78ce5912017-02-08 17:07:31 +0100127 msgb_talloc_ctx_init(tall_pcu_ctx, 0);
Neels Hofmeyr42f2d612018-04-01 16:54:40 +0200128 osmo_init_logging2(tall_pcu_ctx, &gprs_log_info);
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200129
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100130 pcu->nsi = gprs_ns2_instantiate(tall_pcu_ctx, &gprs_ns_prim_cb, NULL);
131 if (!pcu->nsi) {
Alexander Couzens5c3783b2019-05-25 05:41:18 +0200132 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NS instance\n");
133 abort();
134 }
135
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200136 vty_init(&pcu_vty_info);
Pau Espin Pedrolcd2ac562019-08-05 14:30:44 +0200137 pcu_vty_init();
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200138
Jacob Erlbeck0494c752015-05-04 09:34:00 +0200139 current_test = 0;
140
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100141 init_pcu(pcu);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100142 init_main_bts(bts);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100143 bssgp_set_bssgp_callback(gprs_gp_send_cb, pcu->nsi);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100144 create_and_connect_bssgp(bts, INADDR_LOOPBACK, 23000);
Pau Espin Pedrolb1bbcae2020-10-30 17:10:32 +0100145
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200146 for (;;)
147 osmo_select_main(0);
148
149 return EXIT_SUCCESS;
150}
151
Holger Hans Peter Freyther9d938382013-07-31 21:59:29 +0200152
153/*
154 * Test handling..
155 */
156void gprs_test_success(struct gprs_bssgp_pcu *pcu)
157{
158 current_test += 1;
159 if (current_test >= ARRAY_SIZE(all_tests)) {
160 printf("All tests executed.\n");
161 exit(EXIT_SUCCESS);
162 }
163
164 all_tests[current_test].start(pcu);
165}
166
Holger Hans Peter Freyther4ea94072013-07-28 18:34:49 +0200167/*
168 * stubs that should not be reached
169 */
170extern "C" {
171void l1if_pdch_req() { abort(); }
172void l1if_connect_pdch() { abort(); }
173void l1if_close_pdch() { abort(); }
174void l1if_open_pdch() { abort(); }
175}