Holger Hans Peter Freyther | 68c6f88 | 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 | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 22 | #include <openbsc/gprs_llc.h> |
| 23 | #include <openbsc/sgsn.h> |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 24 | #include <openbsc/gprs_gmm.h> |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 25 | #include <openbsc/debug.h> |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 26 | #include <openbsc/gsm_subscriber.h> |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 27 | |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 28 | #include <osmocom/gprs/gprs_bssgp.h> |
| 29 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm_utils.h> |
| 31 | |
| 32 | #include <osmocom/core/application.h> |
| 33 | #include <osmocom/core/msgb.h> |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 34 | #include <osmocom/core/rate_ctr.h> |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | 68c6f88 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 38 | extern void *tall_msgb_ctx; |
| 39 | |
| 40 | void *tall_bsc_ctx; |
| 41 | static struct sgsn_instance sgsn_inst = { |
| 42 | .config_file = "osmo_sgsn.cfg", |
| 43 | .cfg = { |
| 44 | .gtp_statedir = "./", |
Jacob Erlbeck | 106f547 | 2014-11-04 10:08:37 +0100 | [diff] [blame] | 45 | .auth_policy = SGSN_AUTH_POLICY_CLOSED, |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 46 | }, |
| 47 | }; |
| 48 | struct sgsn_instance *sgsn = &sgsn_inst; |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 49 | unsigned sgsn_tx_counter = 0; |
| 50 | |
| 51 | /* override */ |
| 52 | int bssgp_tx_dl_ud(struct msgb *msg, uint16_t pdu_lifetime, |
| 53 | struct bssgp_dl_ud_par *dup) |
| 54 | { |
| 55 | sgsn_tx_counter += 1; |
| 56 | return 0; |
| 57 | } |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 58 | |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 59 | /* override, requires '-Wl,--wrap=sgsn_update_subscriber_data' */ |
| 60 | void __real_sgsn_update_subscriber_data(struct sgsn_mm_ctx *, struct gsm_subscriber *); |
| 61 | void (*update_subscriber_data_cb)(struct sgsn_mm_ctx *, struct gsm_subscriber *) = |
| 62 | &__real_sgsn_update_subscriber_data; |
| 63 | |
| 64 | void __wrap_sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx, |
| 65 | struct gsm_subscriber *subscr) |
| 66 | { |
| 67 | (*update_subscriber_data_cb)(mmctx, subscr); |
| 68 | } |
| 69 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 70 | /* override, requires '-Wl,--wrap=gprs_subscr_request_update_location' */ |
| 71 | int __real_gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx); |
| 72 | int (*subscr_request_update_location_cb)(struct sgsn_mm_ctx *mmctx) = |
| 73 | &__real_gprs_subscr_request_update_location; |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 74 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 75 | int __wrap_gprs_subscr_request_update_location(struct sgsn_mm_ctx *mmctx) { |
| 76 | return (*subscr_request_update_location_cb)(mmctx); |
| 77 | }; |
| 78 | |
| 79 | /* override, requires '-Wl,--wrap=gprs_subscr_request_auth_info' */ |
| 80 | int __real_gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx); |
| 81 | int (*subscr_request_auth_info_cb)(struct sgsn_mm_ctx *mmctx) = |
| 82 | &__real_gprs_subscr_request_auth_info; |
| 83 | |
| 84 | int __wrap_gprs_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx) { |
| 85 | return (*subscr_request_auth_info_cb)(mmctx); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 86 | }; |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 87 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 88 | static int count(struct llist_head *head) |
| 89 | { |
| 90 | struct llist_head *cur; |
| 91 | int count = 0; |
| 92 | |
| 93 | llist_for_each(cur, head) |
| 94 | count += 1; |
| 95 | |
| 96 | return count; |
| 97 | } |
| 98 | |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 99 | static struct msgb *create_msg(const uint8_t *data, size_t len) |
| 100 | { |
| 101 | struct msgb *msg = msgb_alloc(len + 8, "test message"); |
| 102 | msg->l1h = msgb_put(msg, 8); |
| 103 | msg->l2h = msgb_put(msg, len); |
| 104 | memcpy(msg->l2h, data, len); |
| 105 | |
| 106 | msgb_bcid(msg) = msg->l1h; |
| 107 | msgb_gmmh(msg) = msg->l2h; |
| 108 | return msg; |
| 109 | } |
| 110 | |
Jacob Erlbeck | abc16a5 | 2014-10-27 13:23:49 +0100 | [diff] [blame] | 111 | /* |
| 112 | * Create a context and search for it |
| 113 | */ |
| 114 | static struct sgsn_mm_ctx *alloc_mm_ctx(uint32_t tlli, struct gprs_ra_id *raid) |
| 115 | { |
| 116 | struct sgsn_mm_ctx *ctx, *ictx; |
| 117 | struct gprs_llc_lle *lle; |
| 118 | int old_count = count(gprs_llme_list()); |
| 119 | |
| 120 | lle = gprs_lle_get_or_create(tlli, 3); |
| 121 | ctx = sgsn_mm_ctx_alloc(tlli, raid); |
| 122 | ctx->mm_state = GMM_REGISTERED_NORMAL; |
| 123 | ctx->llme = lle->llme; |
| 124 | |
| 125 | ictx = sgsn_mm_ctx_by_tlli(tlli, raid); |
| 126 | OSMO_ASSERT(ictx == ctx); |
| 127 | |
| 128 | OSMO_ASSERT(count(gprs_llme_list()) == old_count + 1); |
| 129 | |
| 130 | return ctx; |
| 131 | } |
| 132 | |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 133 | static void send_0408_message(struct gprs_llc_llme *llme, uint32_t tlli, |
| 134 | const uint8_t *data, size_t data_len) |
| 135 | { |
| 136 | struct msgb *msg; |
| 137 | |
| 138 | sgsn_tx_counter = 0; |
| 139 | |
| 140 | msg = create_msg(data, data_len); |
| 141 | msgb_tlli(msg) = tlli; |
| 142 | gsm0408_gprs_rcvmsg(msg, llme); |
| 143 | msgb_free(msg); |
| 144 | } |
| 145 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 146 | static void test_llme(void) |
| 147 | { |
| 148 | struct gprs_llc_lle *lle, *lle_copy; |
| 149 | uint32_t local_tlli; |
| 150 | uint32_t foreign_tlli; |
| 151 | |
| 152 | printf("Testing LLME allocations\n"); |
| 153 | local_tlli = gprs_tmsi2tlli(0x234, TLLI_LOCAL); |
| 154 | foreign_tlli = gprs_tmsi2tlli(0x234, TLLI_FOREIGN); |
| 155 | |
| 156 | /* initial state */ |
| 157 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 158 | |
| 159 | /* Create a new entry */ |
| 160 | lle = gprs_lle_get_or_create(local_tlli, 3); |
| 161 | OSMO_ASSERT(lle); |
| 162 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 163 | |
| 164 | /* No new entry is created */ |
| 165 | lle_copy = gprs_lle_get_or_create(local_tlli, 3); |
| 166 | OSMO_ASSERT(lle == lle_copy); |
| 167 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 168 | lle_copy = gprs_lle_get_or_create(foreign_tlli, 3); |
| 169 | OSMO_ASSERT(lle == lle_copy); |
| 170 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 171 | |
| 172 | /* unassign which should delete it*/ |
| 173 | gprs_llgmm_assign(lle->llme, lle->llme->tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 174 | |
| 175 | /* Check that everything was cleaned up */ |
| 176 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 177 | } |
| 178 | |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 179 | struct gsm_subscriber *last_updated_subscr = NULL; |
| 180 | void my_dummy_sgsn_update_subscriber_data(struct sgsn_mm_ctx *mmctx, |
| 181 | struct gsm_subscriber *subscr) |
| 182 | { |
| 183 | fprintf(stderr, "Called %s, mmctx = %p, subscr = %p\n", |
| 184 | __func__, mmctx, subscr); |
| 185 | last_updated_subscr = subscr; |
| 186 | } |
| 187 | |
| 188 | static void test_subscriber(void) |
| 189 | { |
| 190 | struct gsm_subscriber *s1, *s2, *s1found, *s2found; |
| 191 | const char *imsi1 = "1234567890"; |
| 192 | const char *imsi2 = "9876543210"; |
| 193 | |
| 194 | update_subscriber_data_cb = my_dummy_sgsn_update_subscriber_data; |
| 195 | |
| 196 | printf("Testing core subscriber data API\n"); |
| 197 | |
| 198 | /* Check for emptiness */ |
| 199 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi1) == NULL); |
| 200 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi2) == NULL); |
| 201 | |
| 202 | /* Allocate entry 1 */ |
| 203 | s1 = gprs_subscr_get_or_create(imsi1); |
| 204 | s1->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
| 205 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 206 | OSMO_ASSERT(s1found == s1); |
| 207 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi2) == NULL); |
| 208 | subscr_put(s1found); |
| 209 | |
| 210 | /* Allocate entry 2 */ |
| 211 | s2 = gprs_subscr_get_or_create(imsi2); |
| 212 | s2->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
| 213 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 214 | s2found = gprs_subscr_get_by_imsi(imsi2); |
| 215 | OSMO_ASSERT(s1found == s1); |
| 216 | OSMO_ASSERT(s2found == s2); |
| 217 | subscr_put(s1found); |
| 218 | subscr_put(s2found); |
| 219 | |
| 220 | /* Update entry 1 */ |
| 221 | last_updated_subscr = NULL; |
| 222 | gprs_subscr_update(s1); |
| 223 | OSMO_ASSERT(last_updated_subscr == s1); |
| 224 | |
| 225 | /* Because of the update, it won't be freed on delete now */ |
| 226 | gprs_subscr_delete(s1); |
| 227 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 228 | OSMO_ASSERT(s1found != NULL); |
| 229 | s1 = s1found; |
| 230 | |
| 231 | /* Cancel it, so that delete will free it. |
| 232 | * Refcount it to make sure s1 won't be freed here */ |
| 233 | last_updated_subscr = NULL; |
| 234 | gprs_subscr_put_and_cancel(subscr_get(s1)); |
| 235 | OSMO_ASSERT(last_updated_subscr == s1); |
| 236 | |
| 237 | /* Cancelled entries are still being found */ |
| 238 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 239 | OSMO_ASSERT(s1found != NULL); |
| 240 | subscr_put(s1found); |
| 241 | |
| 242 | /* Free entry 1 */ |
| 243 | gprs_subscr_delete(s1); |
| 244 | s1 = NULL; |
| 245 | s2found = gprs_subscr_get_by_imsi(imsi2); |
| 246 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi1) == NULL); |
| 247 | OSMO_ASSERT(s2found == s2); |
| 248 | subscr_put(s2found); |
| 249 | |
| 250 | /* Free entry 2 */ |
| 251 | gprs_subscr_delete(s2); |
| 252 | s2 = NULL; |
| 253 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi1) == NULL); |
| 254 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi2) == NULL); |
| 255 | |
| 256 | OSMO_ASSERT(llist_empty(&active_subscribers)); |
| 257 | |
| 258 | update_subscriber_data_cb = __real_sgsn_update_subscriber_data; |
| 259 | } |
| 260 | |
Jacob Erlbeck | 7921ab1 | 2014-12-08 15:52:00 +0100 | [diff] [blame] | 261 | static void test_auth_triplets(void) |
| 262 | { |
| 263 | struct gsm_subscriber *s1, *s1found; |
| 264 | const char *imsi1 = "1234567890"; |
| 265 | struct gsm_auth_tuple *at; |
| 266 | struct sgsn_mm_ctx *ctx; |
| 267 | struct gprs_ra_id raid = { 0, }; |
| 268 | uint32_t local_tlli = 0xffeeddcc; |
| 269 | struct gprs_llc_llme *llme; |
| 270 | |
| 271 | printf("Testing authentication triplet handling\n"); |
| 272 | |
| 273 | /* Check for emptiness */ |
| 274 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi1) == NULL); |
| 275 | |
| 276 | /* Allocate entry 1 */ |
| 277 | s1 = gprs_subscr_get_or_create(imsi1); |
| 278 | s1->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
| 279 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 280 | OSMO_ASSERT(s1found == s1); |
| 281 | subscr_put(s1found); |
| 282 | |
| 283 | /* Create a context */ |
| 284 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 285 | ctx = alloc_mm_ctx(local_tlli, &raid); |
| 286 | |
| 287 | /* Attach s1 to ctx */ |
| 288 | ctx->subscr = subscr_get(s1); |
| 289 | ctx->subscr->sgsn_data->mm = ctx; |
| 290 | |
| 291 | /* Try to get auth tuple */ |
| 292 | at = sgsn_auth_get_tuple(ctx, GSM_KEY_SEQ_INVAL); |
| 293 | OSMO_ASSERT(at == NULL); |
| 294 | |
| 295 | /* Add triplets */ |
| 296 | s1->sgsn_data->auth_triplets[0].key_seq = 0; |
| 297 | s1->sgsn_data->auth_triplets[1].key_seq = 1; |
| 298 | s1->sgsn_data->auth_triplets[2].key_seq = 2; |
| 299 | |
| 300 | /* Try to get auth tuple */ |
| 301 | at = sgsn_auth_get_tuple(ctx, GSM_KEY_SEQ_INVAL); |
| 302 | OSMO_ASSERT(at != NULL); |
| 303 | OSMO_ASSERT(at->key_seq == 0); |
| 304 | OSMO_ASSERT(at->use_count == 1); |
| 305 | at = sgsn_auth_get_tuple(ctx, at->key_seq); |
| 306 | OSMO_ASSERT(at != NULL); |
| 307 | OSMO_ASSERT(at->key_seq == 1); |
| 308 | OSMO_ASSERT(at->use_count == 1); |
| 309 | at = sgsn_auth_get_tuple(ctx, at->key_seq); |
| 310 | OSMO_ASSERT(at != NULL); |
| 311 | OSMO_ASSERT(at->key_seq == 2); |
| 312 | OSMO_ASSERT(at->use_count == 1); |
| 313 | at = sgsn_auth_get_tuple(ctx, at->key_seq); |
| 314 | OSMO_ASSERT(at == NULL); |
| 315 | |
| 316 | /* Free MM context and subscriber */ |
| 317 | subscr_put(s1); |
| 318 | llme = ctx->llme; |
| 319 | sgsn_mm_ctx_free(ctx); |
| 320 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 321 | OSMO_ASSERT(s1found == NULL); |
| 322 | gprs_llgmm_assign(llme, local_tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 323 | } |
| 324 | |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 325 | #define TEST_GSUP_IMSI1_IE 0x01, 0x05, 0x21, 0x43, 0x65, 0x87, 0x09 |
| 326 | |
| 327 | static void test_subscriber_gsup(void) |
| 328 | { |
| 329 | struct gsm_subscriber *s1, *s1found; |
| 330 | const char *imsi1 = "1234567890"; |
| 331 | struct sgsn_mm_ctx *ctx; |
| 332 | struct gprs_ra_id raid = { 0, }; |
| 333 | uint32_t local_tlli = 0xffeeddcc; |
| 334 | struct gprs_llc_llme *llme; |
| 335 | struct msgb *msg; |
| 336 | int rc; |
| 337 | |
| 338 | static const uint8_t send_auth_info_res[] = { |
| 339 | 0x0a, |
| 340 | TEST_GSUP_IMSI1_IE, |
| 341 | 0x03, 0x22, /* Auth tuple */ |
| 342 | 0x20, 0x10, |
| 343 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 344 | 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, |
| 345 | 0x21, 0x04, |
| 346 | 0x21, 0x22, 0x23, 0x24, |
| 347 | 0x22, 0x08, |
| 348 | 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, |
| 349 | 0x03, 0x22, /* Auth tuple */ |
| 350 | 0x20, 0x10, |
| 351 | 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, |
| 352 | 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, |
| 353 | 0x21, 0x04, |
| 354 | 0xa1, 0xa2, 0xa3, 0xa4, |
| 355 | 0x22, 0x08, |
| 356 | 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, |
| 357 | }; |
| 358 | |
| 359 | static const uint8_t send_auth_info_err[] = { |
| 360 | 0x09, |
| 361 | TEST_GSUP_IMSI1_IE, |
| 362 | 0x02, 0x01, 0x07 /* GPRS not allowed */ |
| 363 | }; |
| 364 | |
| 365 | static const uint8_t update_location_res[] = { |
| 366 | 0x06, |
| 367 | TEST_GSUP_IMSI1_IE, |
| 368 | 0x04, 0x00, /* PDP info complete */ |
| 369 | 0x05, 0x12, |
| 370 | 0x10, 0x01, 0x01, |
| 371 | 0x11, 0x02, 0xf1, 0x21, /* IPv4 */ |
| 372 | 0x12, 0x09, 0x04, 't', 'e', 's', 't', 0x03, 'a', 'p', 'n', |
| 373 | 0x05, 0x11, |
| 374 | 0x10, 0x01, 0x02, |
| 375 | 0x11, 0x02, 0xf1, 0x21, /* IPv4 */ |
| 376 | 0x12, 0x08, 0x03, 'f', 'o', 'o', 0x03, 'a', 'p', 'n', |
| 377 | }; |
| 378 | |
| 379 | static const uint8_t update_location_err[] = { |
| 380 | 0x05, |
| 381 | TEST_GSUP_IMSI1_IE, |
| 382 | 0x02, 0x01, 0x07 /* GPRS not allowed */ |
| 383 | }; |
| 384 | |
| 385 | static const uint8_t location_cancellation_req[] = { |
| 386 | 0x1c, |
| 387 | TEST_GSUP_IMSI1_IE, |
| 388 | 0x06, 0x01, 0x00, |
| 389 | }; |
| 390 | |
| 391 | printf("Testing subcriber GSUP handling\n"); |
| 392 | |
| 393 | update_subscriber_data_cb = my_dummy_sgsn_update_subscriber_data; |
| 394 | |
| 395 | /* Check for emptiness */ |
| 396 | OSMO_ASSERT(gprs_subscr_get_by_imsi(imsi1) == NULL); |
| 397 | |
| 398 | /* Allocate entry 1 */ |
| 399 | s1 = gprs_subscr_get_or_create(imsi1); |
| 400 | s1->flags |= GSM_SUBSCRIBER_FIRST_CONTACT; |
| 401 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 402 | OSMO_ASSERT(s1found == s1); |
| 403 | subscr_put(s1found); |
| 404 | |
| 405 | /* Create a context */ |
| 406 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 407 | ctx = alloc_mm_ctx(local_tlli, &raid); |
| 408 | llme = ctx->llme; |
| 409 | |
| 410 | /* Attach s1 to ctx */ |
| 411 | ctx->subscr = subscr_get(s1); |
| 412 | ctx->subscr->sgsn_data->mm = ctx; |
| 413 | |
| 414 | /* Inject SendAuthInfoReq GSUP message */ |
| 415 | msg = msgb_alloc(1024, __func__); |
| 416 | msg->l2h = msgb_put(msg, sizeof(send_auth_info_res)); |
| 417 | memcpy(msg->l2h, send_auth_info_res, sizeof(send_auth_info_res)); |
| 418 | rc = gprs_subscr_rx_gsup_message(msg); |
| 419 | msgb_free(msg); |
| 420 | OSMO_ASSERT(rc >= 0); |
| 421 | OSMO_ASSERT(last_updated_subscr == s1); |
| 422 | |
| 423 | /* Check triplets */ |
| 424 | OSMO_ASSERT(s1->sgsn_data->auth_triplets[0].key_seq == 0); |
| 425 | OSMO_ASSERT(s1->sgsn_data->auth_triplets[1].key_seq == 1); |
| 426 | OSMO_ASSERT(s1->sgsn_data->auth_triplets[2].key_seq == GSM_KEY_SEQ_INVAL); |
| 427 | |
| 428 | /* Inject SendAuthInfoErr GSUP message */ |
| 429 | msg = msgb_alloc(1024, __func__); |
| 430 | msg->l2h = msgb_put(msg, sizeof(send_auth_info_err)); |
| 431 | memcpy(msg->l2h, send_auth_info_err, sizeof(send_auth_info_err)); |
| 432 | rc = gprs_subscr_rx_gsup_message(msg); |
| 433 | msgb_free(msg); |
| 434 | OSMO_ASSERT(rc >= 0); |
| 435 | OSMO_ASSERT(last_updated_subscr == s1); |
| 436 | |
| 437 | /* Check triplets */ |
| 438 | OSMO_ASSERT(s1->sgsn_data->auth_triplets[0].key_seq == GSM_KEY_SEQ_INVAL); |
| 439 | OSMO_ASSERT(s1->sgsn_data->auth_triplets[1].key_seq == GSM_KEY_SEQ_INVAL); |
| 440 | OSMO_ASSERT(s1->sgsn_data->auth_triplets[2].key_seq == GSM_KEY_SEQ_INVAL); |
| 441 | |
| 442 | /* Inject UpdateLocReq GSUP message */ |
| 443 | msg = msgb_alloc(1024, __func__); |
| 444 | msg->l2h = msgb_put(msg, sizeof(update_location_res)); |
| 445 | memcpy(msg->l2h, update_location_res, sizeof(update_location_res)); |
| 446 | rc = gprs_subscr_rx_gsup_message(msg); |
| 447 | msgb_free(msg); |
| 448 | OSMO_ASSERT(rc >= 0); |
| 449 | OSMO_ASSERT(last_updated_subscr == s1); |
| 450 | |
| 451 | /* Check authorization */ |
| 452 | OSMO_ASSERT(s1->authorized == 1); |
| 453 | |
| 454 | /* Inject UpdateLocErr GSUP message */ |
| 455 | msg = msgb_alloc(1024, __func__); |
| 456 | msg->l2h = msgb_put(msg, sizeof(update_location_err)); |
| 457 | memcpy(msg->l2h, update_location_err, sizeof(update_location_err)); |
| 458 | rc = gprs_subscr_rx_gsup_message(msg); |
| 459 | msgb_free(msg); |
| 460 | OSMO_ASSERT(rc >= 0); |
| 461 | OSMO_ASSERT(last_updated_subscr == s1); |
| 462 | |
| 463 | /* Check authorization */ |
| 464 | OSMO_ASSERT(s1->authorized == 0); |
| 465 | |
| 466 | /* Inject UpdateLocReq GSUP message */ |
| 467 | msg = msgb_alloc(1024, __func__); |
| 468 | msg->l2h = msgb_put(msg, sizeof(location_cancellation_req)); |
| 469 | memcpy(msg->l2h, |
| 470 | location_cancellation_req, sizeof(location_cancellation_req)); |
| 471 | rc = gprs_subscr_rx_gsup_message(msg); |
| 472 | msgb_free(msg); |
| 473 | OSMO_ASSERT(rc >= 0); |
| 474 | OSMO_ASSERT(last_updated_subscr == s1); |
| 475 | |
| 476 | /* Check cancellation result */ |
| 477 | OSMO_ASSERT(s1->flags & GPRS_SUBSCRIBER_CANCELLED); |
| 478 | OSMO_ASSERT(s1->sgsn_data->mm == NULL); |
| 479 | |
| 480 | /* Free MM context and subscriber */ |
| 481 | subscr_put(s1); |
| 482 | s1found = gprs_subscr_get_by_imsi(imsi1); |
| 483 | OSMO_ASSERT(s1found == NULL); |
| 484 | gprs_llgmm_assign(llme, local_tlli, 0xffffffff, GPRS_ALGO_GEA0, NULL); |
| 485 | |
| 486 | update_subscriber_data_cb = __real_sgsn_update_subscriber_data; |
| 487 | } |
| 488 | |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 489 | /* |
| 490 | * Test that a GMM Detach will remove the MMCTX and the |
| 491 | * associated LLME. |
| 492 | */ |
| 493 | static void test_gmm_detach(void) |
| 494 | { |
| 495 | struct gprs_ra_id raid = { 0, }; |
| 496 | struct sgsn_mm_ctx *ctx, *ictx; |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 497 | uint32_t local_tlli; |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 498 | |
| 499 | printf("Testing GMM detach\n"); |
| 500 | |
| 501 | /* DTAP - Detach Request (MO) */ |
| 502 | /* normal detach, power_off = 0 */ |
| 503 | static const unsigned char detach_req[] = { |
| 504 | 0x08, 0x05, 0x01, 0x18, 0x05, 0xf4, 0xef, 0xe2, |
| 505 | 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb |
| 506 | }; |
| 507 | |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 508 | local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL); |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 509 | |
Jacob Erlbeck | abc16a5 | 2014-10-27 13:23:49 +0100 | [diff] [blame] | 510 | /* Create a context */ |
| 511 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 512 | ctx = alloc_mm_ctx(local_tlli, &raid); |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 513 | |
| 514 | /* inject the detach */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 515 | send_0408_message(ctx->llme, local_tlli, |
| 516 | detach_req, ARRAY_SIZE(detach_req)); |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 517 | |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 518 | /* verify that a single message (hopefully the Detach Accept) has been |
| 519 | * sent by the SGSN */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 520 | OSMO_ASSERT(sgsn_tx_counter == 1); |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 521 | |
| 522 | /* verify that things are gone */ |
| 523 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 524 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
| 525 | OSMO_ASSERT(!ictx); |
| 526 | } |
| 527 | |
| 528 | /* |
| 529 | * Test that a GMM Detach will remove the MMCTX and the associated LLME but |
| 530 | * will not sent a Detach Accept message (power_off = 1) |
| 531 | */ |
| 532 | static void test_gmm_detach_power_off(void) |
| 533 | { |
| 534 | struct gprs_ra_id raid = { 0, }; |
| 535 | struct sgsn_mm_ctx *ctx, *ictx; |
| 536 | uint32_t local_tlli; |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 537 | |
| 538 | printf("Testing GMM detach (power off)\n"); |
| 539 | |
| 540 | /* DTAP - Detach Request (MO) */ |
| 541 | /* normal detach, power_off = 1 */ |
| 542 | static const unsigned char detach_req[] = { |
| 543 | 0x08, 0x05, 0x09, 0x18, 0x05, 0xf4, 0xef, 0xe2, |
| 544 | 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb |
| 545 | }; |
| 546 | |
| 547 | local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL); |
| 548 | |
| 549 | /* Create a context */ |
| 550 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 551 | ctx = alloc_mm_ctx(local_tlli, &raid); |
| 552 | |
| 553 | /* inject the detach */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 554 | send_0408_message(ctx->llme, local_tlli, |
| 555 | detach_req, ARRAY_SIZE(detach_req)); |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 556 | |
| 557 | /* verify that no message (and therefore no Detach Accept) has been |
| 558 | * sent by the SGSN */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 559 | OSMO_ASSERT(sgsn_tx_counter == 0); |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 560 | |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 561 | /* verify that things are gone */ |
| 562 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 563 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
Jacob Erlbeck | 258ce3d | 2014-09-30 13:51:45 +0200 | [diff] [blame] | 564 | OSMO_ASSERT(!ictx); |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 565 | } |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 566 | |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 567 | /* |
| 568 | * Test that a GMM Detach will remove the associated LLME if there is no MMCTX. |
| 569 | */ |
| 570 | static void test_gmm_detach_no_mmctx(void) |
| 571 | { |
| 572 | struct gprs_llc_lle *lle; |
| 573 | uint32_t local_tlli; |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 574 | |
| 575 | printf("Testing GMM detach (no MMCTX)\n"); |
| 576 | |
| 577 | /* DTAP - Detach Request (MO) */ |
| 578 | /* normal detach, power_off = 0 */ |
| 579 | static const unsigned char detach_req[] = { |
| 580 | 0x08, 0x05, 0x01, 0x18, 0x05, 0xf4, 0xef, 0xe2, |
| 581 | 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb |
| 582 | }; |
| 583 | |
| 584 | /* Create an LLME */ |
| 585 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 586 | local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL); |
| 587 | lle = gprs_lle_get_or_create(local_tlli, 3); |
| 588 | |
| 589 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 590 | |
| 591 | /* inject the detach */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 592 | send_0408_message(lle->llme, local_tlli, |
| 593 | detach_req, ARRAY_SIZE(detach_req)); |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 594 | |
| 595 | /* verify that the LLME is gone */ |
| 596 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 597 | } |
| 598 | |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 599 | /* |
Jacob Erlbeck | de4bbc7 | 2014-11-24 15:04:15 +0100 | [diff] [blame] | 600 | * Test that a single GMM Detach Accept message will not cause the SGSN to send |
| 601 | * any message or leave an MM context at the SGSN. |
| 602 | */ |
| 603 | static void test_gmm_detach_accept_unexpected(void) |
| 604 | { |
| 605 | struct gprs_llc_lle *lle; |
| 606 | uint32_t local_tlli; |
| 607 | |
| 608 | printf("Testing GMM detach accept (unexpected)\n"); |
| 609 | |
| 610 | /* DTAP - Detach Accept (MT) */ |
| 611 | /* normal detach */ |
| 612 | static const unsigned char detach_acc[] = { |
| 613 | 0x08, 0x06 |
| 614 | }; |
| 615 | |
| 616 | /* Create an LLME */ |
| 617 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 618 | local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL); |
| 619 | lle = gprs_lle_get_or_create(local_tlli, 3); |
| 620 | |
| 621 | /* inject the detach */ |
| 622 | send_0408_message(lle->llme, local_tlli, |
| 623 | detach_acc, ARRAY_SIZE(detach_acc)); |
| 624 | |
| 625 | /* verify that no message (and therefore no Status or XID reset) has been |
| 626 | * sent by the SGSN */ |
| 627 | OSMO_ASSERT(sgsn_tx_counter == 0); |
| 628 | |
| 629 | /* verify that things are gone */ |
| 630 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 631 | } |
| 632 | |
| 633 | /* |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 634 | * Test that a GMM Status will remove the associated LLME if there is no MMCTX. |
| 635 | */ |
| 636 | static void test_gmm_status_no_mmctx(void) |
| 637 | { |
| 638 | struct gprs_llc_lle *lle; |
| 639 | uint32_t local_tlli; |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 640 | |
| 641 | printf("Testing GMM Status (no MMCTX)\n"); |
| 642 | |
| 643 | /* DTAP - GMM Status, protocol error */ |
| 644 | static const unsigned char gmm_status[] = { |
| 645 | 0x08, 0x20, 0x6f |
| 646 | }; |
| 647 | |
| 648 | /* Create an LLME */ |
| 649 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 650 | local_tlli = gprs_tmsi2tlli(0x23, TLLI_LOCAL); |
| 651 | lle = gprs_lle_get_or_create(local_tlli, 3); |
| 652 | |
| 653 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 654 | |
| 655 | /* inject the detach */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 656 | send_0408_message(lle->llme, local_tlli, |
| 657 | gmm_status, ARRAY_SIZE(gmm_status)); |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 658 | |
| 659 | /* verify that no message has been sent by the SGSN */ |
Jacob Erlbeck | 94ef1c0 | 2014-10-29 10:31:18 +0100 | [diff] [blame] | 660 | OSMO_ASSERT(sgsn_tx_counter == 0); |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 661 | |
| 662 | /* verify that the LLME is gone */ |
| 663 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 664 | } |
| 665 | |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 666 | /* |
| 667 | * Test the GMM Attach procedure |
| 668 | */ |
| 669 | static void test_gmm_attach(void) |
| 670 | { |
| 671 | struct gprs_ra_id raid = { 0, }; |
| 672 | struct sgsn_mm_ctx *ctx = NULL; |
| 673 | struct sgsn_mm_ctx *ictx; |
Jacob Erlbeck | aec03a1 | 2014-11-24 14:40:28 +0100 | [diff] [blame] | 674 | uint32_t ptmsi1; |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 675 | uint32_t foreign_tlli; |
| 676 | uint32_t local_tlli = 0; |
| 677 | struct gprs_llc_lle *lle; |
| 678 | |
| 679 | /* DTAP - Attach Request */ |
| 680 | /* The P-TMSI is not known by the SGSN */ |
| 681 | static const unsigned char attach_req[] = { |
| 682 | 0x08, 0x01, 0x02, 0xf5, 0xe0, 0x21, 0x08, 0x02, 0x05, 0xf4, |
| 683 | 0xfb, 0xc5, 0x46, 0x79, 0x11, 0x22, 0x33, 0x40, 0x50, 0x60, |
| 684 | 0x19, 0x18, 0xb3, 0x43, 0x2b, 0x25, 0x96, 0x62, 0x00, 0x60, |
| 685 | 0x80, 0x9a, 0xc2, 0xc6, 0x62, 0x00, 0x60, 0x80, 0xba, 0xc8, |
| 686 | 0xc6, 0x62, 0x00, 0x60, 0x80, 0x00 |
| 687 | }; |
| 688 | |
| 689 | /* DTAP - Identity Response IMEI */ |
| 690 | static const unsigned char ident_resp_imei[] = { |
| 691 | 0x08, 0x16, 0x08, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, |
| 692 | 0x56 |
| 693 | }; |
| 694 | |
| 695 | /* DTAP - Identity Response IMSI */ |
| 696 | static const unsigned char ident_resp_imsi[] = { |
| 697 | 0x08, 0x16, 0x08, 0x19, 0x32, 0x54, 0x76, 0x98, 0x10, 0x32, |
| 698 | 0x54 |
| 699 | }; |
| 700 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 701 | /* DTAP - Authentication and Ciphering Resp */ |
| 702 | static const unsigned char auth_ciph_resp[] = { |
| 703 | 0x08, 0x13, 0x00, 0x22, 0x51, 0xe5, 0x51, 0xe5, 0x23, 0x09, |
| 704 | 0x9a, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, 0x56, 0x01 |
| 705 | }; |
| 706 | |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 707 | /* DTAP - Attach Complete */ |
| 708 | static const unsigned char attach_compl[] = { |
| 709 | 0x08, 0x03 |
| 710 | }; |
| 711 | |
| 712 | /* DTAP - Detach Request (MO) */ |
| 713 | /* normal detach, power_off = 0 */ |
| 714 | static const unsigned char detach_req[] = { |
| 715 | 0x08, 0x05, 0x01, 0x18, 0x05, 0xf4, 0xeb, 0x8b, |
| 716 | 0x45, 0x67, 0x19, 0x03, 0xb9, 0x97, 0xcb |
| 717 | }; |
| 718 | |
| 719 | printf("Testing GMM attach\n"); |
| 720 | |
| 721 | /* reset the PRNG used by sgsn_alloc_ptmsi */ |
| 722 | srand(1); |
| 723 | |
Jacob Erlbeck | aec03a1 | 2014-11-24 14:40:28 +0100 | [diff] [blame] | 724 | ptmsi1 = sgsn_alloc_ptmsi(); |
| 725 | OSMO_ASSERT(ptmsi1 != GSM_RESERVED_TMSI); |
| 726 | |
| 727 | /* reset the PRNG, so that the same P-TMSI sequence will be generated |
| 728 | * again */ |
| 729 | srand(1); |
| 730 | |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 731 | foreign_tlli = gprs_tmsi2tlli(0xc0000023, TLLI_FOREIGN); |
| 732 | |
| 733 | /* Create a LLE/LLME */ |
| 734 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 735 | lle = gprs_lle_get_or_create(foreign_tlli, 3); |
| 736 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 737 | |
| 738 | /* inject the attach request */ |
| 739 | send_0408_message(lle->llme, foreign_tlli, |
| 740 | attach_req, ARRAY_SIZE(attach_req)); |
| 741 | |
| 742 | ctx = sgsn_mm_ctx_by_tlli(foreign_tlli, &raid); |
| 743 | OSMO_ASSERT(ctx != NULL); |
| 744 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 745 | |
| 746 | /* we expect an identity request (IMEI) */ |
| 747 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 748 | |
| 749 | /* inject the identity response (IMEI) */ |
| 750 | send_0408_message(ctx->llme, foreign_tlli, |
| 751 | ident_resp_imei, ARRAY_SIZE(ident_resp_imei)); |
| 752 | |
| 753 | /* we expect an identity request (IMSI) */ |
| 754 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 755 | |
| 756 | /* inject the identity response (IMSI) */ |
| 757 | send_0408_message(ctx->llme, foreign_tlli, |
| 758 | ident_resp_imsi, ARRAY_SIZE(ident_resp_imsi)); |
| 759 | |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 760 | /* check that the MM context has not been removed due to a failed |
| 761 | * authorization */ |
| 762 | OSMO_ASSERT(ctx == sgsn_mm_ctx_by_tlli(foreign_tlli, &raid)); |
| 763 | |
Jacob Erlbeck | 0074a77 | 2014-10-28 16:23:46 +0100 | [diff] [blame] | 764 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 765 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 766 | if (ctx->auth_state == SGSN_AUTH_AUTHENTICATE) { |
| 767 | /* we expect an auth & ciph request */ |
| 768 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 769 | |
| 770 | /* inject the auth & ciph response */ |
| 771 | send_0408_message(ctx->llme, foreign_tlli, |
| 772 | auth_ciph_resp, ARRAY_SIZE(auth_ciph_resp)); |
| 773 | |
| 774 | /* check that the MM context has not been removed due to a |
| 775 | * failed authorization */ |
| 776 | OSMO_ASSERT(ctx == sgsn_mm_ctx_by_tlli(foreign_tlli, &raid)); |
| 777 | } |
| 778 | |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 779 | /* we expect an attach accept/reject */ |
| 780 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 781 | |
| 782 | /* this has been randomly assigned by the SGSN */ |
Jacob Erlbeck | aec03a1 | 2014-11-24 14:40:28 +0100 | [diff] [blame] | 783 | local_tlli = gprs_tmsi2tlli(ptmsi1, TLLI_LOCAL); |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 784 | |
| 785 | /* inject the attach complete */ |
| 786 | send_0408_message(ctx->llme, local_tlli, |
| 787 | attach_compl, ARRAY_SIZE(attach_compl)); |
| 788 | |
| 789 | OSMO_ASSERT(ctx->mm_state == GMM_REGISTERED_NORMAL); |
| 790 | |
| 791 | /* we don't expect a response */ |
| 792 | OSMO_ASSERT(sgsn_tx_counter == 0); |
| 793 | |
| 794 | /* inject the detach */ |
| 795 | send_0408_message(ctx->llme, local_tlli, |
| 796 | detach_req, ARRAY_SIZE(detach_req)); |
| 797 | |
| 798 | /* verify that things are gone */ |
| 799 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 800 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
| 801 | OSMO_ASSERT(!ictx); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 802 | } |
Jacob Erlbeck | 0c06f98 | 2014-10-29 22:12:20 +0100 | [diff] [blame] | 803 | |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 804 | static void test_gmm_attach_acl(void) |
| 805 | { |
| 806 | const enum sgsn_auth_policy saved_auth_policy = sgsn->cfg.auth_policy; |
| 807 | |
| 808 | sgsn_inst.cfg.auth_policy = SGSN_AUTH_POLICY_CLOSED; |
| 809 | sgsn_acl_add("123456789012345", &sgsn->cfg); |
| 810 | printf("Auth policy 'closed': "); |
| 811 | test_gmm_attach(); |
Jacob Erlbeck | 0c06f98 | 2014-10-29 22:12:20 +0100 | [diff] [blame] | 812 | sgsn_acl_del("123456789012345", &sgsn->cfg); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 813 | |
| 814 | sgsn->cfg.auth_policy = saved_auth_policy; |
| 815 | } |
| 816 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 817 | int my_subscr_request_update_location(struct sgsn_mm_ctx *mmctx) { |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 818 | int rc; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 819 | rc = __real_gprs_subscr_request_update_location(mmctx); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 820 | if (rc == -ENOTSUP) { |
| 821 | OSMO_ASSERT(mmctx->subscr); |
| 822 | gprs_subscr_update(mmctx->subscr); |
| 823 | } |
| 824 | return rc; |
| 825 | }; |
| 826 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 827 | int my_subscr_request_auth_info(struct sgsn_mm_ctx *mmctx) { |
| 828 | gprs_subscr_update(mmctx->subscr); |
| 829 | return 0; |
| 830 | }; |
| 831 | |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 832 | static void test_gmm_attach_subscr(void) |
| 833 | { |
| 834 | const enum sgsn_auth_policy saved_auth_policy = sgsn->cfg.auth_policy; |
| 835 | struct gsm_subscriber *subscr; |
| 836 | |
| 837 | sgsn_inst.cfg.auth_policy = SGSN_AUTH_POLICY_REMOTE; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 838 | subscr_request_update_location_cb = my_subscr_request_update_location; |
| 839 | subscr_request_auth_info_cb = my_subscr_request_auth_info; |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 840 | |
| 841 | subscr = gprs_subscr_get_or_create("123456789012345"); |
| 842 | subscr->authorized = 1; |
| 843 | subscr_put(subscr); |
| 844 | |
| 845 | printf("Auth policy 'remote': "); |
| 846 | test_gmm_attach(); |
| 847 | |
| 848 | subscr = gprs_subscr_get_by_imsi("123456789012345"); |
| 849 | OSMO_ASSERT(subscr != NULL); |
| 850 | gprs_subscr_delete(subscr); |
| 851 | |
| 852 | sgsn->cfg.auth_policy = saved_auth_policy; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 853 | subscr_request_update_location_cb = __real_gprs_subscr_request_update_location; |
| 854 | subscr_request_auth_info_cb = __real_gprs_subscr_request_auth_info; |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 855 | } |
| 856 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 857 | int my_subscr_request_auth_info_fake_auth(struct sgsn_mm_ctx *mmctx) |
| 858 | { |
| 859 | /* Fake an authentication */ |
| 860 | OSMO_ASSERT(mmctx->subscr); |
| 861 | mmctx->is_authenticated = 1; |
| 862 | gprs_subscr_update_auth_info(mmctx->subscr); |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 863 | |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 864 | return 0; |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 865 | }; |
| 866 | |
| 867 | static void test_gmm_attach_subscr_fake_auth(void) |
| 868 | { |
| 869 | const enum sgsn_auth_policy saved_auth_policy = sgsn->cfg.auth_policy; |
| 870 | struct gsm_subscriber *subscr; |
| 871 | |
| 872 | sgsn_inst.cfg.auth_policy = SGSN_AUTH_POLICY_REMOTE; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 873 | subscr_request_update_location_cb = my_subscr_request_update_location; |
| 874 | subscr_request_auth_info_cb = my_subscr_request_auth_info_fake_auth; |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 875 | |
| 876 | subscr = gprs_subscr_get_or_create("123456789012345"); |
| 877 | subscr->authorized = 1; |
| 878 | subscr->sgsn_data->authenticate = 1; |
| 879 | subscr_put(subscr); |
| 880 | |
| 881 | printf("Auth policy 'remote', auth faked: "); |
| 882 | test_gmm_attach(); |
| 883 | |
| 884 | subscr = gprs_subscr_get_by_imsi("123456789012345"); |
| 885 | OSMO_ASSERT(subscr != NULL); |
| 886 | gprs_subscr_delete(subscr); |
| 887 | |
| 888 | sgsn->cfg.auth_policy = saved_auth_policy; |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 889 | subscr_request_update_location_cb = __real_gprs_subscr_request_update_location; |
| 890 | subscr_request_auth_info_cb = __real_gprs_subscr_request_auth_info; |
| 891 | } |
| 892 | |
| 893 | int my_subscr_request_auth_info_real_auth(struct sgsn_mm_ctx *mmctx) |
| 894 | { |
| 895 | struct gsm_auth_tuple at = { |
| 896 | .sres = {0x51, 0xe5, 0x51, 0xe5}, |
| 897 | .key_seq = 0 |
| 898 | }; |
| 899 | |
| 900 | /* Fake an authentication */ |
| 901 | OSMO_ASSERT(mmctx->subscr); |
| 902 | mmctx->subscr->sgsn_data->auth_triplets[0] = at; |
| 903 | |
| 904 | gprs_subscr_update_auth_info(mmctx->subscr); |
| 905 | |
| 906 | return 0; |
| 907 | }; |
| 908 | |
| 909 | static void test_gmm_attach_subscr_real_auth(void) |
| 910 | { |
| 911 | const enum sgsn_auth_policy saved_auth_policy = sgsn->cfg.auth_policy; |
| 912 | struct gsm_subscriber *subscr; |
| 913 | |
| 914 | sgsn_inst.cfg.auth_policy = SGSN_AUTH_POLICY_REMOTE; |
| 915 | subscr_request_update_location_cb = my_subscr_request_update_location; |
| 916 | subscr_request_auth_info_cb = my_subscr_request_auth_info_real_auth; |
| 917 | |
| 918 | subscr = gprs_subscr_get_or_create("123456789012345"); |
| 919 | subscr->authorized = 1; |
| 920 | subscr->sgsn_data->authenticate = 1; |
| 921 | subscr_put(subscr); |
| 922 | |
| 923 | printf("Auth policy 'remote', triplet based auth: "); |
| 924 | test_gmm_attach(); |
| 925 | |
| 926 | subscr = gprs_subscr_get_by_imsi("123456789012345"); |
| 927 | OSMO_ASSERT(subscr != NULL); |
| 928 | gprs_subscr_delete(subscr); |
| 929 | |
| 930 | sgsn->cfg.auth_policy = saved_auth_policy; |
| 931 | subscr_request_update_location_cb = __real_gprs_subscr_request_update_location; |
| 932 | subscr_request_auth_info_cb = __real_gprs_subscr_request_auth_info; |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 935 | /* |
| 936 | * Test the GMM Rejects |
| 937 | */ |
| 938 | static void test_gmm_reject(void) |
| 939 | { |
| 940 | struct gprs_ra_id raid = { 0, }; |
| 941 | struct sgsn_mm_ctx *ctx = NULL; |
| 942 | uint32_t foreign_tlli; |
| 943 | struct gprs_llc_lle *lle; |
| 944 | int idx; |
| 945 | |
| 946 | /* DTAP - Attach Request */ |
| 947 | /* Invalid MI length */ |
| 948 | static const unsigned char attach_req_inv_mi_len[] = { |
| 949 | 0x08, 0x01, 0x02, 0xf5, 0xe0, 0x21, 0x08, 0x02, 0x09, 0xf4, |
| 950 | 0xfb, 0xc5, 0x46, 0x79, 0xff, 0xff, 0xff, 0xff, 0x11, 0x22, |
| 951 | 0x33, 0x40, 0x50, 0x60, 0x19, 0x18, 0xb3, 0x43, 0x2b, 0x25, |
| 952 | 0x96, 0x62, 0x00, 0x60, 0x80, 0x9a, 0xc2, 0xc6, 0x62, 0x00, |
| 953 | 0x60, 0x80, 0xba, 0xc8, 0xc6, 0x62, 0x00, 0x60, 0x80, 0x00 |
| 954 | }; |
| 955 | |
| 956 | /* DTAP - Attach Request */ |
| 957 | /* Invalid MI type (IMEI) */ |
| 958 | static const unsigned char attach_req_inv_mi_type[] = { |
| 959 | 0x08, 0x01, 0x02, 0xf5, 0xe0, 0x21, 0x08, 0x02, 0x05, 0xf2, |
| 960 | 0xfb, 0xc5, 0x46, 0x79, 0x11, 0x22, 0x33, 0x40, 0x50, 0x60, |
| 961 | 0x19, 0x18, 0xb3, 0x43, 0x2b, 0x25, 0x96, 0x62, 0x00, 0x60, |
| 962 | 0x80, 0x9a, 0xc2, 0xc6, 0x62, 0x00, 0x60, 0x80, 0xba, 0xc8, |
| 963 | 0xc6, 0x62, 0x00, 0x60, 0x80, 0x00 |
| 964 | }; |
| 965 | |
| 966 | /* DTAP - Routing Area Update Request */ |
| 967 | static const unsigned char dtap_ra_upd_req[] = { |
| 968 | 0x08, 0x08, 0x10, 0x11, 0x22, 0x33, 0x40, 0x50, |
| 969 | 0x60, 0x1d, 0x19, 0x13, 0x42, 0x33, 0x57, 0x2b, |
| 970 | 0xf7, 0xc8, 0x48, 0x02, 0x13, 0x48, 0x50, 0xc8, |
| 971 | 0x48, 0x02, 0x14, 0x48, 0x50, 0xc8, 0x48, 0x02, |
| 972 | 0x17, 0x49, 0x10, 0xc8, 0x48, 0x02, 0x00, 0x19, |
| 973 | 0x8b, 0xb2, 0x92, 0x17, 0x16, 0x27, 0x07, 0x04, |
| 974 | 0x31, 0x02, 0xe5, 0xe0, 0x32, 0x02, 0x20, 0x00 |
| 975 | }; |
| 976 | |
| 977 | /* DTAP - Routing Area Update Request */ |
| 978 | /* Invalid type: GPRS_UPD_T_RA_LA_IMSI_ATT */ |
| 979 | static const unsigned char dtap_ra_upd_req_inv_type[] = { |
| 980 | 0x08, 0x08, 0x12, 0x11, 0x22, 0x33, 0x40, 0x50, |
| 981 | 0x60, 0x1d, 0x19, 0x13, 0x42, 0x33, 0x57, 0x2b, |
| 982 | 0xf7, 0xc8, 0x48, 0x02, 0x13, 0x48, 0x50, 0xc8, |
| 983 | 0x48, 0x02, 0x14, 0x48, 0x50, 0xc8, 0x48, 0x02, |
| 984 | 0x17, 0x49, 0x10, 0xc8, 0x48, 0x02, 0x00, 0x19, |
| 985 | 0x8b, 0xb2, 0x92, 0x17, 0x16, 0x27, 0x07, 0x04, |
| 986 | 0x31, 0x02, 0xe5, 0xe0, 0x32, 0x02, 0x20, 0x00 |
| 987 | }; |
| 988 | |
| 989 | /* DTAP - Routing Area Update Request */ |
| 990 | /* Invalid cap length */ |
| 991 | static const unsigned char dtap_ra_upd_req_inv_cap_len[] = { |
| 992 | 0x08, 0x08, 0x10, 0x11, 0x22, 0x33, 0x40, 0x50, |
| 993 | 0x60, 0x3d, 0x19, 0x13, 0x42, 0x33, 0x57, 0x2b, |
| 994 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 995 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 996 | 0xf7, 0xc8, 0x48, 0x02, 0x13, 0x48, 0x50, 0xc8, |
| 997 | 0x48, 0x02, 0x14, 0x48, 0x50, 0xc8, 0x48, 0x02, |
| 998 | 0x17, 0x49, 0x10, 0xc8, 0x48, 0x02, 0x00, 0x19, |
| 999 | 0x8b, 0xb2, 0x92, 0x17, 0x16, 0x27, 0x07, 0x04, |
| 1000 | 0x31, 0x02, 0xe5, 0xe0, 0x32, 0x02, 0x20, 0x00 |
| 1001 | }; |
| 1002 | |
| 1003 | struct test { |
| 1004 | const char *title; |
| 1005 | const unsigned char *msg; |
| 1006 | unsigned msg_len; |
| 1007 | unsigned num_resp; |
| 1008 | |
| 1009 | }; |
| 1010 | static struct test tests[] = { |
| 1011 | { |
| 1012 | .title = "Attach Request (invalid MI length)", |
| 1013 | .msg = attach_req_inv_mi_len, |
| 1014 | .msg_len = sizeof(attach_req_inv_mi_len), |
| 1015 | .num_resp = 1 /* Reject */ |
| 1016 | |
| 1017 | }, |
| 1018 | { |
| 1019 | .title = "Attach Request (invalid MI type)", |
| 1020 | .msg = attach_req_inv_mi_type, |
| 1021 | .msg_len = sizeof(attach_req_inv_mi_type), |
| 1022 | .num_resp = 1 /* Reject */ |
| 1023 | }, |
| 1024 | { |
| 1025 | .title = "Routing Area Update Request (valid)", |
| 1026 | .msg = dtap_ra_upd_req, |
| 1027 | .msg_len = sizeof(dtap_ra_upd_req), |
| 1028 | .num_resp = 2 /* XID Reset + Reject */ |
| 1029 | }, |
| 1030 | { |
| 1031 | .title = "Routing Area Update Request (invalid type)", |
| 1032 | .msg = dtap_ra_upd_req_inv_type, |
| 1033 | .msg_len = sizeof(dtap_ra_upd_req_inv_type), |
| 1034 | .num_resp = 1 /* Reject */ |
| 1035 | }, |
| 1036 | { |
| 1037 | .title = "Routing Area Update Request (invalid CAP length)", |
| 1038 | .msg = dtap_ra_upd_req_inv_cap_len, |
| 1039 | .msg_len = sizeof(dtap_ra_upd_req_inv_cap_len), |
| 1040 | .num_resp = 1 /* Reject */ |
| 1041 | }, |
| 1042 | }; |
| 1043 | |
| 1044 | printf("Testing GMM reject\n"); |
| 1045 | |
| 1046 | /* reset the PRNG used by sgsn_alloc_ptmsi */ |
| 1047 | srand(1); |
| 1048 | |
| 1049 | foreign_tlli = gprs_tmsi2tlli(0xc0000023, TLLI_FOREIGN); |
| 1050 | |
| 1051 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 1052 | |
| 1053 | for (idx = 0; idx < ARRAY_SIZE(tests); idx++) { |
| 1054 | const struct test *test = &tests[idx]; |
| 1055 | printf(" - %s\n", test->title); |
| 1056 | |
| 1057 | /* Create a LLE/LLME */ |
| 1058 | lle = gprs_lle_get_or_create(foreign_tlli, 3); |
| 1059 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 1060 | |
| 1061 | /* Inject the Request message */ |
| 1062 | send_0408_message(lle->llme, foreign_tlli, |
| 1063 | test->msg, test->msg_len); |
| 1064 | |
| 1065 | /* We expect a Reject message */ |
| 1066 | fprintf(stderr, "sgsn_tx_counter = %d (expected %d)\n", |
| 1067 | sgsn_tx_counter, test->num_resp); |
| 1068 | OSMO_ASSERT(sgsn_tx_counter == test->num_resp); |
| 1069 | |
| 1070 | /* verify that LLME/MM are removed */ |
| 1071 | ctx = sgsn_mm_ctx_by_tlli(foreign_tlli, &raid); |
| 1072 | OSMO_ASSERT(ctx == NULL); |
| 1073 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 1074 | } |
| 1075 | } |
| 1076 | |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1077 | /* |
Jacob Erlbeck | 98647ca | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 1078 | * Test cancellation of attached MM contexts |
| 1079 | */ |
| 1080 | static void test_gmm_cancel(void) |
| 1081 | { |
| 1082 | struct gprs_ra_id raid = { 0, }; |
| 1083 | struct sgsn_mm_ctx *ctx = NULL; |
| 1084 | struct sgsn_mm_ctx *ictx; |
| 1085 | uint32_t ptmsi1; |
| 1086 | uint32_t foreign_tlli; |
| 1087 | uint32_t local_tlli = 0; |
| 1088 | struct gprs_llc_lle *lle; |
| 1089 | const enum sgsn_auth_policy saved_auth_policy = sgsn->cfg.auth_policy; |
| 1090 | |
| 1091 | /* DTAP - Attach Request */ |
| 1092 | /* The P-TMSI is not known by the SGSN */ |
| 1093 | static const unsigned char attach_req[] = { |
| 1094 | 0x08, 0x01, 0x02, 0xf5, 0xe0, 0x21, 0x08, 0x02, 0x05, 0xf4, |
| 1095 | 0xfb, 0xc5, 0x46, 0x79, 0x11, 0x22, 0x33, 0x40, 0x50, 0x60, |
| 1096 | 0x19, 0x18, 0xb3, 0x43, 0x2b, 0x25, 0x96, 0x62, 0x00, 0x60, |
| 1097 | 0x80, 0x9a, 0xc2, 0xc6, 0x62, 0x00, 0x60, 0x80, 0xba, 0xc8, |
| 1098 | 0xc6, 0x62, 0x00, 0x60, 0x80, 0x00 |
| 1099 | }; |
| 1100 | |
| 1101 | /* DTAP - Identity Response IMEI */ |
| 1102 | static const unsigned char ident_resp_imei[] = { |
| 1103 | 0x08, 0x16, 0x08, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, |
| 1104 | 0x56 |
| 1105 | }; |
| 1106 | |
| 1107 | /* DTAP - Identity Response IMSI */ |
| 1108 | static const unsigned char ident_resp_imsi[] = { |
| 1109 | 0x08, 0x16, 0x08, 0x19, 0x32, 0x54, 0x76, 0x98, 0x10, 0x32, |
| 1110 | 0x54 |
| 1111 | }; |
| 1112 | |
| 1113 | /* DTAP - Attach Complete */ |
| 1114 | static const unsigned char attach_compl[] = { |
| 1115 | 0x08, 0x03 |
| 1116 | }; |
| 1117 | |
| 1118 | printf("Testing cancellation\n"); |
| 1119 | |
| 1120 | sgsn_inst.cfg.auth_policy = SGSN_AUTH_POLICY_OPEN; |
| 1121 | |
| 1122 | /* reset the PRNG used by sgsn_alloc_ptmsi */ |
| 1123 | srand(1); |
| 1124 | |
| 1125 | ptmsi1 = sgsn_alloc_ptmsi(); |
| 1126 | OSMO_ASSERT(ptmsi1 != GSM_RESERVED_TMSI); |
| 1127 | |
| 1128 | /* reset the PRNG, so that the same P-TMSI sequence will be generated |
| 1129 | * again */ |
| 1130 | srand(1); |
| 1131 | |
| 1132 | foreign_tlli = gprs_tmsi2tlli(0xc0000023, TLLI_FOREIGN); |
| 1133 | |
| 1134 | /* Create a LLE/LLME */ |
| 1135 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 1136 | lle = gprs_lle_get_or_create(foreign_tlli, 3); |
| 1137 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 1138 | |
| 1139 | /* inject the attach request */ |
| 1140 | send_0408_message(lle->llme, foreign_tlli, |
| 1141 | attach_req, ARRAY_SIZE(attach_req)); |
| 1142 | |
| 1143 | ctx = sgsn_mm_ctx_by_tlli(foreign_tlli, &raid); |
| 1144 | OSMO_ASSERT(ctx != NULL); |
| 1145 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1146 | |
| 1147 | /* we expect an identity request (IMEI) */ |
| 1148 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1149 | |
| 1150 | /* inject the identity response (IMEI) */ |
| 1151 | send_0408_message(ctx->llme, foreign_tlli, |
| 1152 | ident_resp_imei, ARRAY_SIZE(ident_resp_imei)); |
| 1153 | |
| 1154 | /* we expect an identity request (IMSI) */ |
| 1155 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1156 | |
| 1157 | /* inject the identity response (IMSI) */ |
| 1158 | send_0408_message(ctx->llme, foreign_tlli, |
| 1159 | ident_resp_imsi, ARRAY_SIZE(ident_resp_imsi)); |
| 1160 | |
| 1161 | /* check that the MM context has not been removed due to a failed |
| 1162 | * authorization */ |
| 1163 | OSMO_ASSERT(ctx == sgsn_mm_ctx_by_tlli(foreign_tlli, &raid)); |
| 1164 | |
| 1165 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1166 | |
| 1167 | /* we expect an attach accept/reject */ |
| 1168 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1169 | |
| 1170 | /* this has been randomly assigned by the SGSN */ |
| 1171 | local_tlli = gprs_tmsi2tlli(ptmsi1, TLLI_LOCAL); |
| 1172 | |
| 1173 | /* inject the attach complete */ |
| 1174 | send_0408_message(ctx->llme, local_tlli, |
| 1175 | attach_compl, ARRAY_SIZE(attach_compl)); |
| 1176 | |
| 1177 | OSMO_ASSERT(ctx->mm_state == GMM_REGISTERED_NORMAL); |
| 1178 | |
| 1179 | /* we don't expect a response */ |
| 1180 | OSMO_ASSERT(sgsn_tx_counter == 0); |
| 1181 | |
| 1182 | /* cancel */ |
| 1183 | gsm0408_gprs_access_cancelled(ctx); |
| 1184 | |
| 1185 | /* verify that things are gone */ |
| 1186 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 1187 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
| 1188 | OSMO_ASSERT(!ictx); |
| 1189 | |
| 1190 | sgsn->cfg.auth_policy = saved_auth_policy; |
| 1191 | } |
| 1192 | |
| 1193 | /* |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1194 | * Test the dynamic allocation of P-TMSIs |
| 1195 | */ |
| 1196 | static void test_gmm_ptmsi_allocation(void) |
| 1197 | { |
| 1198 | struct gprs_ra_id raid = { 0, }; |
| 1199 | struct sgsn_mm_ctx *ctx = NULL; |
| 1200 | struct sgsn_mm_ctx *ictx; |
| 1201 | uint32_t foreign_tlli; |
| 1202 | uint32_t ptmsi1; |
| 1203 | uint32_t ptmsi2; |
| 1204 | uint32_t old_ptmsi; |
| 1205 | uint32_t local_tlli = 0; |
| 1206 | struct gprs_llc_lle *lle; |
| 1207 | const enum sgsn_auth_policy saved_auth_policy = sgsn->cfg.auth_policy; |
| 1208 | |
| 1209 | /* DTAP - Attach Request (IMSI 12131415161718) */ |
| 1210 | static const unsigned char attach_req[] = { |
| 1211 | 0x08, 0x01, 0x02, 0xf5, 0xe0, 0x21, 0x08, 0x02, |
| 1212 | 0x08, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 1213 | 0x18, 0x11, 0x22, 0x33, 0x40, 0x50, 0x60, 0x19, |
| 1214 | 0x18, 0xb3, 0x43, 0x2b, 0x25, 0x96, 0x62, 0x00, |
| 1215 | 0x60, 0x80, 0x9a, 0xc2, 0xc6, 0x62, 0x00, 0x60, |
| 1216 | 0x80, 0xba, 0xc8, 0xc6, 0x62, 0x00, 0x60, 0x80, |
| 1217 | 0x00, |
| 1218 | }; |
| 1219 | |
| 1220 | /* DTAP - Identity Response IMEI */ |
| 1221 | static const unsigned char ident_resp_imei[] = { |
| 1222 | 0x08, 0x16, 0x08, 0x9a, 0x78, 0x56, 0x34, 0x12, 0x90, 0x78, |
| 1223 | 0x56 |
| 1224 | }; |
| 1225 | |
| 1226 | /* DTAP - Attach Complete */ |
| 1227 | static const unsigned char attach_compl[] = { |
| 1228 | 0x08, 0x03 |
| 1229 | }; |
| 1230 | |
| 1231 | /* DTAP - Routing Area Update Request */ |
| 1232 | static const unsigned char ra_upd_req[] = { |
| 1233 | 0x08, 0x08, 0x10, 0x11, 0x22, 0x33, 0x40, 0x50, |
| 1234 | 0x60, 0x1d, 0x19, 0x13, 0x42, 0x33, 0x57, 0x2b, |
| 1235 | 0xf7, 0xc8, 0x48, 0x02, 0x13, 0x48, 0x50, 0xc8, |
| 1236 | 0x48, 0x02, 0x14, 0x48, 0x50, 0xc8, 0x48, 0x02, |
| 1237 | 0x17, 0x49, 0x10, 0xc8, 0x48, 0x02, 0x00, 0x19, |
| 1238 | 0x8b, 0xb2, 0x92, 0x17, 0x16, 0x27, 0x07, 0x04, |
| 1239 | 0x31, 0x02, 0xe5, 0xe0, 0x32, 0x02, 0x20, 0x00 |
| 1240 | }; |
| 1241 | |
| 1242 | /* DTAP - Routing Area Update Complete */ |
| 1243 | static const unsigned char ra_upd_complete[] = { |
| 1244 | 0x08, 0x0a |
| 1245 | }; |
| 1246 | |
| 1247 | /* DTAP - Detach Request (MO) */ |
| 1248 | /* normal detach, power_off = 1 */ |
| 1249 | static const unsigned char detach_req[] = { |
| 1250 | 0x08, 0x05, 0x09, 0x18, 0x05, 0xf4, 0xef, 0xe2, |
| 1251 | 0xb7, 0x00, 0x19, 0x03, 0xb9, 0x97, 0xcb |
| 1252 | }; |
| 1253 | |
| 1254 | sgsn->cfg.auth_policy = SGSN_AUTH_POLICY_OPEN; |
| 1255 | |
| 1256 | printf("Testing P-TMSI allocation\n"); |
| 1257 | |
| 1258 | printf(" - sgsn_alloc_ptmsi\n"); |
| 1259 | |
| 1260 | /* reset the PRNG used by sgsn_alloc_ptmsi */ |
| 1261 | srand(1); |
| 1262 | |
| 1263 | ptmsi1 = sgsn_alloc_ptmsi(); |
| 1264 | OSMO_ASSERT(ptmsi1 != GSM_RESERVED_TMSI); |
| 1265 | |
| 1266 | ptmsi2 = sgsn_alloc_ptmsi(); |
| 1267 | OSMO_ASSERT(ptmsi2 != GSM_RESERVED_TMSI); |
| 1268 | |
| 1269 | OSMO_ASSERT(ptmsi1 != ptmsi2); |
| 1270 | |
| 1271 | printf(" - Repeated Attach Request\n"); |
| 1272 | |
| 1273 | /* reset the PRNG, so that the same P-TMSI will be generated |
| 1274 | * again */ |
| 1275 | srand(1); |
| 1276 | |
| 1277 | foreign_tlli = gprs_tmsi2tlli(0xc0000023, TLLI_FOREIGN); |
| 1278 | |
| 1279 | /* Create a LLE/LLME */ |
| 1280 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 1281 | lle = gprs_lle_get_or_create(foreign_tlli, 3); |
| 1282 | OSMO_ASSERT(count(gprs_llme_list()) == 1); |
| 1283 | |
| 1284 | /* inject the attach request */ |
| 1285 | send_0408_message(lle->llme, foreign_tlli, |
| 1286 | attach_req, ARRAY_SIZE(attach_req)); |
| 1287 | |
| 1288 | ctx = sgsn_mm_ctx_by_tlli(foreign_tlli, &raid); |
| 1289 | OSMO_ASSERT(ctx != NULL); |
| 1290 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1291 | OSMO_ASSERT(ctx->p_tmsi == ptmsi1); |
| 1292 | |
| 1293 | old_ptmsi = ctx->p_tmsi_old; |
| 1294 | |
| 1295 | /* we expect an identity request (IMEI) */ |
| 1296 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1297 | |
| 1298 | /* inject the identity response (IMEI) */ |
| 1299 | send_0408_message(ctx->llme, foreign_tlli, |
| 1300 | ident_resp_imei, ARRAY_SIZE(ident_resp_imei)); |
| 1301 | |
| 1302 | /* check that the MM context has not been removed due to a failed |
| 1303 | * authorization */ |
| 1304 | OSMO_ASSERT(ctx == sgsn_mm_ctx_by_tlli(foreign_tlli, &raid)); |
| 1305 | |
| 1306 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1307 | OSMO_ASSERT(ctx->p_tmsi == ptmsi1); |
| 1308 | |
| 1309 | /* we expect an attach accept */ |
| 1310 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1311 | |
| 1312 | /* we ignore this and send the attach again */ |
| 1313 | send_0408_message(lle->llme, foreign_tlli, |
| 1314 | attach_req, ARRAY_SIZE(attach_req)); |
| 1315 | |
| 1316 | /* the allocated P-TMSI should be the same */ |
| 1317 | ctx = sgsn_mm_ctx_by_tlli(foreign_tlli, &raid); |
| 1318 | OSMO_ASSERT(ctx != NULL); |
| 1319 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1320 | OSMO_ASSERT(ctx->p_tmsi_old == old_ptmsi); |
| 1321 | OSMO_ASSERT(ctx->p_tmsi == ptmsi1); |
| 1322 | |
| 1323 | /* inject the attach complete */ |
| 1324 | local_tlli = gprs_tmsi2tlli(ptmsi1, TLLI_LOCAL); |
| 1325 | send_0408_message(ctx->llme, local_tlli, |
| 1326 | attach_compl, ARRAY_SIZE(attach_compl)); |
| 1327 | |
| 1328 | /* we don't expect a response */ |
| 1329 | OSMO_ASSERT(sgsn_tx_counter == 0); |
| 1330 | |
| 1331 | OSMO_ASSERT(ctx->mm_state == GMM_REGISTERED_NORMAL); |
| 1332 | OSMO_ASSERT(ctx->p_tmsi_old == 0); |
| 1333 | OSMO_ASSERT(ctx->p_tmsi == ptmsi1); |
| 1334 | |
| 1335 | printf(" - Repeated RA Update Request\n"); |
| 1336 | |
| 1337 | /* inject the RA update request */ |
| 1338 | send_0408_message(ctx->llme, local_tlli, |
| 1339 | ra_upd_req, ARRAY_SIZE(ra_upd_req)); |
| 1340 | |
| 1341 | /* we expect an RA update accept */ |
| 1342 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1343 | |
| 1344 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1345 | OSMO_ASSERT(ctx->p_tmsi_old == ptmsi1); |
| 1346 | OSMO_ASSERT(ctx->p_tmsi == ptmsi2); |
| 1347 | |
| 1348 | /* repeat the RA update request */ |
| 1349 | send_0408_message(ctx->llme, local_tlli, |
| 1350 | ra_upd_req, ARRAY_SIZE(ra_upd_req)); |
| 1351 | |
| 1352 | /* we expect an RA update accept */ |
| 1353 | OSMO_ASSERT(sgsn_tx_counter == 1); |
| 1354 | |
| 1355 | OSMO_ASSERT(ctx->mm_state == GMM_COMMON_PROC_INIT); |
| 1356 | OSMO_ASSERT(ctx->p_tmsi_old == ptmsi1); |
| 1357 | OSMO_ASSERT(ctx->p_tmsi == ptmsi2); |
| 1358 | |
| 1359 | /* inject the RA update complete */ |
| 1360 | local_tlli = gprs_tmsi2tlli(ptmsi2, TLLI_LOCAL); |
| 1361 | send_0408_message(ctx->llme, local_tlli, |
| 1362 | ra_upd_complete, ARRAY_SIZE(ra_upd_complete)); |
| 1363 | |
| 1364 | /* we don't expect a response */ |
| 1365 | OSMO_ASSERT(sgsn_tx_counter == 0); |
| 1366 | |
| 1367 | OSMO_ASSERT(ctx->mm_state == GMM_REGISTERED_NORMAL); |
| 1368 | OSMO_ASSERT(ctx->p_tmsi_old == 0); |
| 1369 | OSMO_ASSERT(ctx->p_tmsi == ptmsi2); |
| 1370 | |
| 1371 | /* inject the detach */ |
| 1372 | send_0408_message(ctx->llme, local_tlli, |
| 1373 | detach_req, ARRAY_SIZE(detach_req)); |
| 1374 | |
| 1375 | /* verify that things are gone */ |
| 1376 | OSMO_ASSERT(count(gprs_llme_list()) == 0); |
| 1377 | ictx = sgsn_mm_ctx_by_tlli(local_tlli, &raid); |
| 1378 | OSMO_ASSERT(!ictx); |
| 1379 | |
| 1380 | sgsn->cfg.auth_policy = saved_auth_policy; |
| 1381 | } |
| 1382 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 1383 | static struct log_info_cat gprs_categories[] = { |
| 1384 | [DMM] = { |
| 1385 | .name = "DMM", |
| 1386 | .description = "Layer3 Mobility Management (MM)", |
| 1387 | .color = "\033[1;33m", |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 1388 | .enabled = 1, .loglevel = LOGL_DEBUG, |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 1389 | }, |
| 1390 | [DPAG] = { |
| 1391 | .name = "DPAG", |
| 1392 | .description = "Paging Subsystem", |
| 1393 | .color = "\033[1;38m", |
| 1394 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 1395 | }, |
| 1396 | [DMEAS] = { |
| 1397 | .name = "DMEAS", |
| 1398 | .description = "Radio Measurement Processing", |
| 1399 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 1400 | }, |
| 1401 | [DREF] = { |
| 1402 | .name = "DREF", |
| 1403 | .description = "Reference Counting", |
| 1404 | .enabled = 0, .loglevel = LOGL_NOTICE, |
| 1405 | }, |
| 1406 | [DGPRS] = { |
| 1407 | .name = "DGPRS", |
| 1408 | .description = "GPRS Packet Service", |
| 1409 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 1410 | }, |
| 1411 | [DNS] = { |
| 1412 | .name = "DNS", |
| 1413 | .description = "GPRS Network Service (NS)", |
| 1414 | .enabled = 1, .loglevel = LOGL_INFO, |
| 1415 | }, |
| 1416 | [DBSSGP] = { |
| 1417 | .name = "DBSSGP", |
| 1418 | .description = "GPRS BSS Gateway Protocol (BSSGP)", |
| 1419 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 1420 | }, |
| 1421 | [DLLC] = { |
| 1422 | .name = "DLLC", |
| 1423 | .description = "GPRS Logical Link Control Protocol (LLC)", |
| 1424 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 1425 | }, |
| 1426 | [DSNDCP] = { |
| 1427 | .name = "DSNDCP", |
| 1428 | .description = "GPRS Sub-Network Dependent Control Protocol (SNDCP)", |
| 1429 | .enabled = 1, .loglevel = LOGL_DEBUG, |
| 1430 | }, |
| 1431 | }; |
| 1432 | |
| 1433 | static struct log_info info = { |
| 1434 | .cat = gprs_categories, |
| 1435 | .num_cat = ARRAY_SIZE(gprs_categories), |
| 1436 | }; |
| 1437 | |
Holger Hans Peter Freyther | 68c6f88 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 1438 | int main(int argc, char **argv) |
| 1439 | { |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 1440 | osmo_init_logging(&info); |
| 1441 | tall_bsc_ctx = talloc_named_const(NULL, 0, "osmo_sgsn"); |
| 1442 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 0, "msgb"); |
| 1443 | |
Jacob Erlbeck | a0b6efb | 2014-11-13 10:48:39 +0100 | [diff] [blame] | 1444 | sgsn_auth_init(); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 1445 | gprs_subscr_init(sgsn); |
Jacob Erlbeck | 27cb4d5 | 2014-10-29 12:11:58 +0100 | [diff] [blame] | 1446 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 1447 | test_llme(); |
Jacob Erlbeck | 33b6dad | 2014-11-12 10:12:11 +0100 | [diff] [blame] | 1448 | test_subscriber(); |
Jacob Erlbeck | 7921ab1 | 2014-12-08 15:52:00 +0100 | [diff] [blame] | 1449 | test_auth_triplets(); |
Jacob Erlbeck | a6ddc2d | 2014-12-12 15:01:37 +0100 | [diff] [blame] | 1450 | test_subscriber_gsup(); |
Holger Hans Peter Freyther | fe92133 | 2014-10-02 22:24:47 +0200 | [diff] [blame] | 1451 | test_gmm_detach(); |
Jacob Erlbeck | 189999d | 2014-10-27 14:34:13 +0100 | [diff] [blame] | 1452 | test_gmm_detach_power_off(); |
Jacob Erlbeck | 5a38f64 | 2014-10-21 13:09:55 +0200 | [diff] [blame] | 1453 | test_gmm_detach_no_mmctx(); |
Jacob Erlbeck | de4bbc7 | 2014-11-24 15:04:15 +0100 | [diff] [blame] | 1454 | test_gmm_detach_accept_unexpected(); |
Jacob Erlbeck | 14ae582 | 2014-10-28 09:47:03 +0100 | [diff] [blame] | 1455 | test_gmm_status_no_mmctx(); |
Jacob Erlbeck | be2c8d9 | 2014-11-12 10:18:09 +0100 | [diff] [blame] | 1456 | test_gmm_attach_acl(); |
| 1457 | test_gmm_attach_subscr(); |
Jacob Erlbeck | 2e5e94c | 2014-12-08 15:26:47 +0100 | [diff] [blame] | 1458 | test_gmm_attach_subscr_fake_auth(); |
Jacob Erlbeck | 98a95ac | 2014-11-28 14:55:25 +0100 | [diff] [blame] | 1459 | test_gmm_attach_subscr_real_auth(); |
Jacob Erlbeck | 80d07e3 | 2014-11-06 13:43:41 +0100 | [diff] [blame] | 1460 | test_gmm_reject(); |
Jacob Erlbeck | 98647ca | 2014-11-11 14:47:38 +0100 | [diff] [blame] | 1461 | test_gmm_cancel(); |
Jacob Erlbeck | f6e7d99 | 2014-11-06 15:43:10 +0100 | [diff] [blame] | 1462 | test_gmm_ptmsi_allocation(); |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 1463 | printf("Done\n"); |
Jacob Erlbeck | 07de92e | 2015-01-13 11:46:32 +0100 | [diff] [blame^] | 1464 | |
| 1465 | talloc_report_full(tall_bsc_ctx, stderr); |
| 1466 | /* TODO: The following check currently fails */ |
| 1467 | /* OSMO_ASSERT(talloc_total_blocks(tall_msgb_ctx) == 1); */ |
Holger Hans Peter Freyther | 68c6f88 | 2014-09-30 09:10:25 +0200 | [diff] [blame] | 1468 | return 0; |
| 1469 | } |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 1470 | |
| 1471 | |
| 1472 | /* stubs */ |
| 1473 | struct osmo_prim_hdr; |
| 1474 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 1475 | { |
| 1476 | abort(); |
| 1477 | } |