Holger Hans Peter Freyther | 232f621 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 1 | /* Test the SGSN */ |
| 2 | /* |
| 3 | * (C) 2014 by Holger Hans Peter Freyther |
| 4 | * (C) 2014 by sysmocom s.f.m.c. GmbH |
| 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 Affero 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 | |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 22 | #include <openbsc/gprs_llc.h> |
| 23 | #include <openbsc/sgsn.h> |
Holger Hans Peter Freyther | 9424684 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 24 | #include <openbsc/gprs_gmm.h> |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 25 | #include <openbsc/debug.h> |
| 26 | |
| 27 | #include <osmocom/gsm/gsm_utils.h> |
| 28 | |
| 29 | #include <osmocom/core/application.h> |
| 30 | #include <osmocom/core/msgb.h> |
| 31 | |
Holger Hans Peter Freyther | 232f621 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 34 | extern void *tall_msgb_ctx; |
| 35 | |
| 36 | void *tall_bsc_ctx; |
| 37 | static struct sgsn_instance sgsn_inst = { |
| 38 | .config_file = "osmo_sgsn.cfg", |
| 39 | .cfg = { |
| 40 | .gtp_statedir = "./", |
| 41 | .acl_enabled = 1, |
| 42 | }, |
| 43 | }; |
| 44 | struct sgsn_instance *sgsn = &sgsn_inst; |
| 45 | |
| 46 | static int count(struct llist_head *head) |
| 47 | { |
| 48 | struct llist_head *cur; |
| 49 | int count = 0; |
| 50 | |
| 51 | llist_for_each(cur, head) |
| 52 | count += 1; |
| 53 | |
| 54 | return count; |
| 55 | } |
| 56 | |
Holger Hans Peter Freyther | 9424684 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 57 | static struct msgb *create_msg(const uint8_t *data, size_t len) |
| 58 | { |
| 59 | struct msgb *msg = msgb_alloc(len + 8, "test message"); |
| 60 | msg->l1h = msgb_put(msg, 8); |
| 61 | msg->l2h = msgb_put(msg, len); |
| 62 | memcpy(msg->l2h, data, len); |
| 63 | |
| 64 | msgb_bcid(msg) = msg->l1h; |
| 65 | msgb_gmmh(msg) = msg->l2h; |
| 66 | return msg; |
| 67 | } |
| 68 | |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 69 | static void test_llme(void) |
| 70 | { |
| 71 | struct gprs_llc_lle *lle, *lle_copy; |
| 72 | uint32_t local_tlli; |
| 73 | uint32_t foreign_tlli; |
| 74 | |
| 75 | printf("Testing LLME allocations\n"); |
| 76 | local_tlli = gprs_tmsi2tlli(0x234, TLLI_LOCAL); |
| 77 | foreign_tlli = gprs_tmsi2tlli(0x234, TLLI_FOREIGN); |
| 78 | |
| 79 | /* initial state */ |
| 80 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 81 | |
| 82 | /* Create a new entry */ |
| 83 | lle = gprs_lle_get_or_create(local_tlli, 3); |
| 84 | OSMO_ASSERT(lle); |
| 85 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 86 | |
| 87 | /* No new entry is created */ |
| 88 | lle_copy = gprs_lle_get_or_create(local_tlli, 3); |
| 89 | OSMO_ASSERT(lle == lle_copy); |
| 90 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 91 | lle_copy = gprs_lle_get_or_create(foreign_tlli, 3); |
| 92 | OSMO_ASSERT(lle == lle_copy); |
| 93 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 94 | |
| 95 | /* unassign which should delete it*/ |
| 96 | gprs_llgmm_assign(lle->llme, lle->llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 97 | |
| 98 | /* Check that everything was cleaned up */ |
| 99 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 100 | } |
| 101 | |
Holger Hans Peter Freyther | 9424684 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 102 | /* |
| 103 | * Test that a GMM Detach will remove the MMCTX and the |
| 104 | * associated LLME. |
| 105 | */ |
| 106 | static void test_gmm_detach(void) |
| 107 | { |
| 108 | struct gprs_ra_id raid = { 0, }; |
| 109 | struct sgsn_mm_ctx *ctx, *ictx; |
| 110 | struct gprs_llc_lle *lle; |
| 111 | uint32_t local_tlli; |
| 112 | struct msgb *msg; |
| 113 | |
| 114 | printf("Testing GMM detach\n"); |
| 115 | |
| 116 | /* DTAP - Detach Request (MO) */ |
| 117 | /* normal detach, power_off = 0 */ |
| 118 | static const unsigned char detach_req[] = { |
| 119 | 0x08, 0x05, 0x01, 0x18, 0x05, 0xf4, 0xef, 0xe2, |
| 120 | 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb |
| 121 | }; |
| 122 | |
| 123 | /* Create a conext and search for it */ |
| 124 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 125 | local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL); |
| 126 | lle = gprs_lle_get_or_create(local_tlli, 3); |
| 127 | ctx = sgsn_mm_ctx_alloc(local_tlli, &raid); |
| 128 | ctx->mm_state = GMM_REGISTERED_NORMAL; |
| 129 | ctx->llme = lle->llme; |
| 130 | |
| 131 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
| 132 | OSMO_ASSERT(ictx == ctx); |
| 133 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 134 | |
| 135 | /* inject the detach */ |
| 136 | msg = create_msg(detach_req, ARRAY_SIZE(detach_req)); |
| 137 | msgb_tlli(msg) = local_tlli; |
| 138 | gsm0408_gprs_rcvmsg(msg, ctx->llme); |
| 139 | msgb_free(msg); |
| 140 | |
| 141 | /* verify that things are gone */ |
| 142 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 143 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
Jacob Erlbeck | 12396bd | 2014-09-30 13:51:45 +0200 | [diff] [blame] | 144 | OSMO_ASSERT(!ictx); |
Holger Hans Peter Freyther | 9424684 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 145 | } |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 146 | |
| 147 | static struct log_info_cat gprs_categories[] = { |
| 148 | [DMM] = { |
| 149 | .name = "DMM", |
| 150 | .description = "Layer3 Mobility Management (MM)", |
| 151 | .color = "\033[1;33m", |
| 152 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 153 | }, |
| 154 | [DPAG] = { |
| 155 | .name = "DPAG", |
| 156 | .description = "Paging Subsystem", |
| 157 | .color = "\033[1;38m", |
| 158 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 159 | }, |
| 160 | [DMEAS] = { |
| 161 | .name = "DMEAS", |
| 162 | .description = "Radio Measurement Processing", |
| 163 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 164 | }, |
| 165 | [DREF] = { |
| 166 | .name = "DREF", |
| 167 | .description = "Reference Counting", |
| 168 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 169 | }, |
| 170 | [DGPRS] = { |
| 171 | .name = "DGPRS", |
| 172 | .description = "GPRS Packet Service", |
| 173 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 174 | }, |
| 175 | [DNS] = { |
| 176 | .name = "DNS", |
| 177 | .description = "GPRS Network Service (NS)", |
| 178 | .enabled = 1, .loglevel = LOGL_INFO, |
| 179 | }, |
| 180 | [DBSSGP] = { |
| 181 | .name = "DBSSGP", |
| 182 | .description = "GPRS BSS Gateway Protocol (BSSGP)", |
| 183 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 184 | }, |
| 185 | [DLLC] = { |
| 186 | .name = "DLLC", |
| 187 | .description = "GPRS Logical Link Control Protocol (LLC)", |
| 188 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 189 | }, |
| 190 | [DSNDCP] = { |
| 191 | .name = "DSNDCP", |
| 192 | .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 193 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 194 | }, |
| 195 | }; |
| 196 | |
| 197 | static struct log_info info = { |
| 198 | .cat = gprs_categories, |
| 199 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 200 | }; |
| 201 | |
Holger Hans Peter Freyther | 232f621 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 202 | int main(int argc, char **argv) |
| 203 | { |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 204 | osmo_init_logging(&info); |
| 205 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
| 206 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 207 | |
| 208 | test_llme(); |
Holger Hans Peter Freyther | 9424684 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 209 | test_gmm_detach(); |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 210 | printf("Done\n"); |
Holger Hans Peter Freyther | 232f621 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 211 | return 0; |
| 212 | } |
Holger Hans Peter Freyther | 49dbcd9 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 213 | |
| 214 | |
| 215 | /* stubs */ |
| 216 | struct osmo_prim_hdr; |
| 217 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 218 | { |
| 219 | abort(); |
| 220 | } |