Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 1 | /* 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 | |
| 22 | extern "C" { |
| 23 | #include <osmocom/core/talloc.h> |
| 24 | #include <pcu_vty.h> |
| 25 | } |
| 26 | |
Holger Hans Peter Freyther | 9d93838 | 2013-07-31 21:59:29 +0200 | [diff] [blame] | 27 | #include "gprs_tests.h" |
| 28 | |
| 29 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 30 | #include <gprs_bssgp_pcu.h> |
| 31 | #include <gprs_rlcmac.h> |
Holger Hans Peter Freyther | 67ed34e | 2013-10-17 17:01:54 +0200 | [diff] [blame] | 32 | #include <bts.h> |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/socket.h> |
| 37 | |
Jacob Erlbeck | 0494c75 | 2015-05-04 09:34:00 +0200 | [diff] [blame] | 38 | static size_t current_test; |
Holger Hans Peter Freyther | 9d93838 | 2013-07-31 21:59:29 +0200 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 40 | /* Extern data to please the underlying code */ |
| 41 | void *tall_pcu_ctx; |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 42 | int16_t spoof_mnc = 0, spoof_mcc = 0; |
Neels Hofmeyr | bdc55fa | 2018-02-21 00:39:07 +0100 | [diff] [blame] | 43 | bool spoof_mnc_3_digits = false; |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 44 | |
Holger Hans Peter Freyther | 741481d | 2013-07-28 21:14:51 +0200 | [diff] [blame] | 45 | extern void test_replay_gprs_attach(struct gprs_bssgp_pcu *pcu); |
Holger Hans Peter Freyther | bc1e52c | 2013-08-22 08:44:38 +0200 | [diff] [blame] | 46 | extern void test_replay_gprs_data(struct gprs_bssgp_pcu *, struct msgb *, struct tlv_parsed *); |
Holger Hans Peter Freyther | 741481d | 2013-07-28 21:14:51 +0200 | [diff] [blame] | 47 | |
Holger Hans Peter Freyther | 9d93838 | 2013-07-31 21:59:29 +0200 | [diff] [blame] | 48 | extern void test_pdp_activation_start(struct gprs_bssgp_pcu *pcu); |
| 49 | extern void test_pdp_activation_data(struct gprs_bssgp_pcu *, struct msgb *, struct tlv_parsed*); |
| 50 | |
| 51 | struct gprs_test all_tests[] = { |
| 52 | gprs_test("gprs_attach_with_tmsi", |
| 53 | "A simple test that verifies that N(U) is " |
| 54 | "increasing across various messages. This makes " |
| 55 | "sure that no new LLE/LLME is created on the fly.", |
| 56 | test_replay_gprs_attach, |
| 57 | test_replay_gprs_data), |
| 58 | gprs_test("gprs_full_attach_pdp_activation", |
| 59 | "A simple test to do a GPRS attach and open a PDP " |
| 60 | "context. Then goes to sleep and waits for you to ping " |
| 61 | "the connection and hopefully re-produce a crash.", |
| 62 | test_pdp_activation_start, |
| 63 | test_pdp_activation_data), |
| 64 | }; |
| 65 | |
Holger Hans Peter Freyther | b6acfda | 2013-10-17 19:41:11 +0200 | [diff] [blame] | 66 | static void init_main_bts() |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 67 | { |
Holger Hans Peter Freyther | b6acfda | 2013-10-17 19:41:11 +0200 | [diff] [blame] | 68 | struct gprs_rlcmac_bts *bts = bts_main_data(); |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 69 | bts->initial_cs_dl = bts->initial_cs_ul = 1; |
Pau Espin Pedrol | d87722d | 2020-10-30 17:14:26 +0100 | [diff] [blame] | 70 | bts->cs_mask = 1 << 0; /* CS-1 always enabled by default */ |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 71 | bts->n3101 = 10; |
| 72 | bts->n3103 = 4; |
| 73 | bts->n3105 = 8; |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 74 | } |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 75 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 76 | static void init_pcu(struct gprs_pcu *pcu) |
| 77 | { |
| 78 | if (!pcu->alloc_algorithm) |
| 79 | pcu->alloc_algorithm = alloc_algorithm_b; |
Pau Espin Pedrol | e891222 | 2021-01-14 14:03:17 +0100 | [diff] [blame] | 80 | pcu->vty.fc_interval = 100; |
Pau Espin Pedrol | a281495 | 2021-01-14 12:53:53 +0100 | [diff] [blame] | 81 | pcu->vty.alpha = 0; /* a = 0.0 */ |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Holger Hans Peter Freyther | 741481d | 2013-07-28 21:14:51 +0200 | [diff] [blame] | 84 | static void bvci_unblocked(struct gprs_bssgp_pcu *pcu) |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 85 | { |
| 86 | printf("BVCI unblocked. We can begin with test cases.\n"); |
Holger Hans Peter Freyther | 9d93838 | 2013-07-31 21:59:29 +0200 | [diff] [blame] | 87 | all_tests[current_test].start(pcu); |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Holger Hans Peter Freyther | bc1e52c | 2013-08-22 08:44:38 +0200 | [diff] [blame] | 90 | static void bssgp_data(struct gprs_bssgp_pcu *pcu, struct msgb *msg, struct tlv_parsed *tp) |
| 91 | { |
Holger Hans Peter Freyther | 9d93838 | 2013-07-31 21:59:29 +0200 | [diff] [blame] | 92 | all_tests[current_test].data(pcu, msg, tp); |
Holger Hans Peter Freyther | bc1e52c | 2013-08-22 08:44:38 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 95 | void create_and_connect_bssgp(struct gprs_rlcmac_bts *bts, |
| 96 | uint32_t sgsn_ip, uint16_t sgsn_port) |
| 97 | { |
| 98 | struct gprs_bssgp_pcu *pcu; |
Alexander Couzens | 290d903 | 2020-09-16 21:52:02 +0200 | [diff] [blame] | 99 | struct osmo_sockaddr local, remote; |
Alexander Couzens | 5bece2a | 2020-10-12 02:27:22 +0200 | [diff] [blame] | 100 | uint16_t nsvci = 20; |
| 101 | uint16_t nsei = 20; |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 102 | |
Alexander Couzens | 290d903 | 2020-09-16 21:52:02 +0200 | [diff] [blame] | 103 | local.u.sin.sin_family = AF_INET; |
| 104 | local.u.sin.sin_addr.s_addr = 0; |
| 105 | local.u.sin.sin_port = 0; |
| 106 | |
| 107 | remote.u.sin.sin_family = AF_INET; |
| 108 | remote.u.sin.sin_addr.s_addr = htonl(sgsn_ip); |
| 109 | remote.u.sin.sin_port = htons(sgsn_port); |
| 110 | |
| 111 | pcu = gprs_bssgp_init(bts, 20, 20, 901, 99, false, 1, 0, 0); |
Alexander Couzens | 5bece2a | 2020-10-12 02:27:22 +0200 | [diff] [blame] | 112 | gprs_ns_config(bts, nsei, &local, &remote, &nsvci, 1); |
Alexander Couzens | 290d903 | 2020-09-16 21:52:02 +0200 | [diff] [blame] | 113 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 114 | pcu->on_unblock_ack = bvci_unblocked; |
Holger Hans Peter Freyther | bc1e52c | 2013-08-22 08:44:38 +0200 | [diff] [blame] | 115 | pcu->on_dl_unit_data = bssgp_data; |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | int main(int argc, char **argv) |
| 119 | { |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 120 | struct gprs_pcu *pcu = gprs_pcu_alloc(tall_pcu_ctx); |
| 121 | the_pcu = pcu; /* globally avaialable object */ |
| 122 | pcu->bts = bts_alloc(pcu); |
Alexander Couzens | 290d903 | 2020-09-16 21:52:02 +0200 | [diff] [blame] | 123 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 124 | tall_pcu_ctx = talloc_named_const(NULL, 1, "moiji-mobile Emu-PCU context"); |
| 125 | if (!tall_pcu_ctx) |
| 126 | abort(); |
| 127 | |
Neels Hofmeyr | 78ce591 | 2017-02-08 17:07:31 +0100 | [diff] [blame] | 128 | msgb_talloc_ctx_init(tall_pcu_ctx, 0); |
Neels Hofmeyr | 42f2d61 | 2018-04-01 16:54:40 +0200 | [diff] [blame] | 129 | osmo_init_logging2(tall_pcu_ctx, &gprs_log_info); |
Alexander Couzens | 5c3783b | 2019-05-25 05:41:18 +0200 | [diff] [blame] | 130 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 131 | pcu->nsi = gprs_ns2_instantiate(tall_pcu_ctx, &gprs_ns_prim_cb, NULL); |
| 132 | if (!pcu->nsi) { |
Alexander Couzens | 5c3783b | 2019-05-25 05:41:18 +0200 | [diff] [blame] | 133 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create NS instance\n"); |
| 134 | abort(); |
| 135 | } |
| 136 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 137 | vty_init(&pcu_vty_info); |
Pau Espin Pedrol | cd2ac56 | 2019-08-05 14:30:44 +0200 | [diff] [blame] | 138 | pcu_vty_init(); |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 139 | |
Jacob Erlbeck | 0494c75 | 2015-05-04 09:34:00 +0200 | [diff] [blame] | 140 | current_test = 0; |
| 141 | |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 142 | init_pcu(pcu); |
Holger Hans Peter Freyther | b6acfda | 2013-10-17 19:41:11 +0200 | [diff] [blame] | 143 | init_main_bts(); |
Pau Espin Pedrol | ac3fd12 | 2021-01-13 18:54:38 +0100 | [diff] [blame] | 144 | bssgp_set_bssgp_callback(gprs_gp_send_cb, pcu->nsi); |
Pau Espin Pedrol | 2182e62 | 2021-01-14 16:48:38 +0100 | [diff] [blame^] | 145 | create_and_connect_bssgp(pcu->bts, INADDR_LOOPBACK, 23000); |
Pau Espin Pedrol | b1bbcae | 2020-10-30 17:10:32 +0100 | [diff] [blame] | 146 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 147 | for (;;) |
| 148 | osmo_select_main(0); |
| 149 | |
| 150 | return EXIT_SUCCESS; |
| 151 | } |
| 152 | |
Holger Hans Peter Freyther | 9d93838 | 2013-07-31 21:59:29 +0200 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Test handling.. |
| 156 | */ |
| 157 | void gprs_test_success(struct gprs_bssgp_pcu *pcu) |
| 158 | { |
| 159 | current_test += 1; |
| 160 | if (current_test >= ARRAY_SIZE(all_tests)) { |
| 161 | printf("All tests executed.\n"); |
| 162 | exit(EXIT_SUCCESS); |
| 163 | } |
| 164 | |
| 165 | all_tests[current_test].start(pcu); |
| 166 | } |
| 167 | |
Holger Hans Peter Freyther | 4ea9407 | 2013-07-28 18:34:49 +0200 | [diff] [blame] | 168 | /* |
| 169 | * stubs that should not be reached |
| 170 | */ |
| 171 | extern "C" { |
| 172 | void l1if_pdch_req() { abort(); } |
| 173 | void l1if_connect_pdch() { abort(); } |
| 174 | void l1if_close_pdch() { abort(); } |
| 175 | void l1if_open_pdch() { abort(); } |
| 176 | } |