Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 1 | /* Test the GTP hub */ |
| 2 | |
| 3 | /* (C) 2015 by sysmocom s.f.m.c. GmbH |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * Author: Neels Hofmeyr <nhofmeyr@sysmcom.de> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU Affero General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <limits.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <osmocom/core/utils.h> |
| 29 | #include <osmocom/core/msgb.h> |
| 30 | #include <osmocom/core/application.h> |
| 31 | |
| 32 | #include <openbsc/debug.h> |
| 33 | |
| 34 | #include <openbsc/gtphub.h> |
| 35 | #include <gtp.h> |
| 36 | #include <gtpie.h> |
| 37 | |
| 38 | #define EXPIRE_ALL ((60 * GTPH_TEI_MAPPING_EXPIRY_MINUTES) + 1) |
| 39 | |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 40 | #define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', sizeof(*(struct_pointer))) |
| 41 | |
| 42 | #define LVL2_ASSERT(exp) LVL2_ASSERT_R(exp, return 0) |
| 43 | #define LVL2_ASSERT_R(exp, ret) \ |
| 44 | if (!(exp)) { \ |
| 45 | fprintf(stderr, "LVL2 Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \ |
| 46 | osmo_generate_backtrace(); \ |
| 47 | ret; \ |
| 48 | } |
| 49 | |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 50 | /* Convenience makro, note: only within this C file. */ |
| 51 | #define LOG(label) \ |
| 52 | { LOGP(DGTPHUB, LOGL_NOTICE, "\n\n" label "\n"); \ |
| 53 | printf(label "\n"); } |
| 54 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 55 | void gtphub_init(struct gtphub *hub); |
| 56 | |
| 57 | void *osmo_gtphub_ctx; |
| 58 | |
| 59 | /* TODO copied from libosmo-abis/src/subchan_demux.c, remove dup */ |
| 60 | static int llist_len(struct llist_head *head) |
| 61 | { |
| 62 | struct llist_head *entry; |
| 63 | int i = 0; |
| 64 | |
| 65 | llist_for_each(entry, head) |
| 66 | i++; |
| 67 | |
| 68 | return i; |
| 69 | } |
| 70 | |
| 71 | static void nr_mapping_free(struct expiring_item *e) |
| 72 | { |
| 73 | struct nr_mapping *m = container_of(e, struct nr_mapping, |
| 74 | expiry_entry); |
| 75 | nr_mapping_del(m); |
| 76 | talloc_free(m); |
| 77 | } |
| 78 | |
| 79 | static struct nr_mapping *nr_mapping_alloc(void) |
| 80 | { |
| 81 | struct nr_mapping *m; |
| 82 | m = talloc(osmo_gtphub_ctx, struct nr_mapping); |
| 83 | nr_mapping_init(m); |
| 84 | m->expiry_entry.del_cb = nr_mapping_free; |
| 85 | return m; |
| 86 | } |
| 87 | |
| 88 | static struct nr_mapping *nr_map_have(struct nr_map *map, void *origin, nr_t orig, time_t now) |
| 89 | { |
| 90 | struct nr_mapping *mapping; |
| 91 | |
| 92 | mapping = nr_map_get(map, origin, orig); |
| 93 | if (!mapping) { |
| 94 | mapping = nr_mapping_alloc(); |
| 95 | mapping->origin = origin; |
| 96 | mapping->orig = orig; |
| 97 | nr_map_add(map, mapping, now); |
| 98 | } |
| 99 | |
| 100 | return mapping; |
| 101 | } |
| 102 | |
| 103 | static nr_t nr_map_verify(const struct nr_map *map, void *origin, nr_t orig, nr_t expect_repl) |
| 104 | { |
| 105 | struct nr_mapping *m; |
| 106 | m = nr_map_get(map, origin, orig); |
| 107 | |
| 108 | if (!m) { |
| 109 | printf("mapping not found for %p %d\n", origin, orig); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | if (m->repl != expect_repl) { |
| 114 | printf("mapping found, but nr mismatches: expect %d, got %d\n", |
| 115 | (int)expect_repl, (int)m->repl); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | static int nr_map_verify_inv(const struct nr_map *map, nr_t repl, |
| 123 | void *expect_origin, nr_t expect_orig) |
| 124 | { |
| 125 | struct nr_mapping *m; |
| 126 | m = nr_map_get_inv(map, repl); |
| 127 | if (!m) { |
| 128 | printf("mapping not found for %d\n", (int)repl); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | if (m->origin != expect_origin) { |
| 133 | printf("mapping found, but origin mismatches: expect %p, got %p\n", |
| 134 | expect_origin, m->origin); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | if (m->orig != expect_orig) { |
| 139 | printf("mapping found, but nr mismatches: expect %d, got %d\n", |
| 140 | (int)expect_orig, (int)m->orig); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | static void test_nr_map_basic(void) |
| 149 | { |
| 150 | struct nr_pool _pool; |
| 151 | struct nr_pool *pool = &_pool; |
| 152 | struct nr_map _map; |
| 153 | struct nr_map *map = &_map; |
| 154 | |
| 155 | nr_pool_init(pool); |
| 156 | nr_map_init(map, pool, NULL); |
| 157 | |
| 158 | OSMO_ASSERT(llist_empty(&map->mappings)); |
| 159 | |
| 160 | #define TEST_N_HALF 100 |
| 161 | #define TEST_N (2*TEST_N_HALF) |
| 162 | #define TEST_I 123 |
| 163 | uint32_t i, check_i; |
| 164 | uint32_t m[TEST_N]; |
| 165 | struct nr_mapping *mapping; |
| 166 | |
| 167 | /* create half of TEST_N mappings from one origin */ |
| 168 | void *origin1 = (void*)0x1234; |
| 169 | for (i = 0; i < TEST_N_HALF; i++) { |
| 170 | nr_t orig = TEST_I + i; |
| 171 | mapping = nr_map_have(map, origin1, orig, 0); |
| 172 | m[i] = mapping->repl; |
| 173 | OSMO_ASSERT(m[i] != 0); |
| 174 | OSMO_ASSERT(llist_len(&map->mappings) == (i+1)); |
| 175 | for (check_i = 0; check_i < i; check_i++) |
| 176 | OSMO_ASSERT(m[check_i] != m[i]); |
| 177 | } |
| 178 | OSMO_ASSERT(llist_len(&map->mappings) == TEST_N_HALF); |
| 179 | |
| 180 | /* create another TEST_N mappings with the same original numbers, but |
| 181 | * from a different origin */ |
| 182 | void *origin2 = (void*)0x5678; |
| 183 | for (i = 0; i < TEST_N_HALF; i++) { |
| 184 | int i2 = TEST_N_HALF + i; |
| 185 | nr_t orig = TEST_I + i; |
| 186 | mapping = nr_map_have(map, origin2, orig, 0); |
| 187 | m[i2] = mapping->repl; |
| 188 | OSMO_ASSERT(m[i2] != 0); |
| 189 | OSMO_ASSERT(llist_len(&map->mappings) == (i2+1)); |
| 190 | for (check_i = 0; check_i < i2; check_i++) |
| 191 | OSMO_ASSERT(m[check_i] != m[i2]); |
| 192 | } |
| 193 | OSMO_ASSERT(llist_len(&map->mappings) == TEST_N); |
| 194 | |
| 195 | /* verify mappings */ |
| 196 | for (i = 0; i < TEST_N_HALF; i++) { |
| 197 | nr_t orig = TEST_I + i; |
| 198 | { |
| 199 | OSMO_ASSERT(nr_map_verify(map, origin1, orig, m[i])); |
| 200 | OSMO_ASSERT(nr_map_verify_inv(map, m[i], origin1, orig)); |
| 201 | } |
| 202 | { |
| 203 | int i2 = TEST_N_HALF + i; |
| 204 | OSMO_ASSERT(nr_map_verify(map, origin2, orig, m[i2])); |
| 205 | OSMO_ASSERT(nr_map_verify_inv(map, m[i2], origin2, orig)); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* remove all mappings */ |
| 210 | for (i = 0; i < TEST_N_HALF; i++) { |
| 211 | OSMO_ASSERT(llist_len(&map->mappings) == (TEST_N - 2*i)); |
| 212 | |
| 213 | nr_t orig = TEST_I + i; |
| 214 | nr_mapping_del(nr_map_get(map, origin1, orig)); |
| 215 | nr_mapping_del(nr_map_get(map, origin2, orig)); |
| 216 | } |
| 217 | OSMO_ASSERT(llist_empty(&map->mappings)); |
| 218 | #undef TEST_N |
| 219 | #undef TEST_I |
| 220 | } |
| 221 | |
| 222 | static int nr_map_is(struct nr_map *map, const char *str) |
| 223 | { |
| 224 | static char buf[4096]; |
| 225 | char *pos = buf; |
| 226 | size_t len = sizeof(buf); |
| 227 | struct nr_mapping *m; |
| 228 | llist_for_each_entry(m, &map->mappings, entry) { |
| 229 | size_t wrote = snprintf(pos, len, "(%d->%d@%d), ", |
| 230 | (int)m->orig, |
| 231 | (int)m->repl, |
| 232 | (int)m->expiry_entry.expiry); |
| 233 | OSMO_ASSERT(wrote < len); |
| 234 | pos += wrote; |
| 235 | len -= wrote; |
| 236 | } |
| 237 | *pos = '\0'; |
| 238 | |
| 239 | if (strncmp(buf, str, sizeof(buf)) != 0) { |
| 240 | printf("FAILURE: nr_map_is() mismatches expected value:\n" |
| 241 | "expected: \"%s\"\n" |
| 242 | "is: \"%s\"\n", |
| 243 | str, buf); |
| 244 | return 0; |
| 245 | } |
| 246 | return 1; |
| 247 | } |
| 248 | |
| 249 | static void test_expiry(void) |
| 250 | { |
| 251 | struct expiry expiry; |
| 252 | struct nr_pool pool; |
| 253 | struct nr_map map; |
| 254 | int i; |
| 255 | |
| 256 | expiry_init(&expiry, 30); |
| 257 | nr_pool_init(&pool); |
| 258 | nr_map_init(&map, &pool, &expiry); |
| 259 | OSMO_ASSERT(nr_map_is(&map, "")); |
| 260 | |
| 261 | /* tick on empty map */ |
| 262 | OSMO_ASSERT(expiry_tick(&expiry, 10000) == 0); |
| 263 | OSMO_ASSERT(nr_map_is(&map, "")); |
| 264 | |
| 265 | #define MAP1 \ |
| 266 | "(10->1@10040), " \ |
| 267 | "" |
| 268 | |
| 269 | #define MAP2 \ |
| 270 | "(20->2@10050), " \ |
| 271 | "(21->3@10051), " \ |
| 272 | "(22->4@10052), " \ |
| 273 | "(23->5@10053), " \ |
| 274 | "(24->6@10054), " \ |
| 275 | "(25->7@10055), " \ |
| 276 | "(26->8@10056), " \ |
| 277 | "(27->9@10057), " \ |
| 278 | "" |
| 279 | |
| 280 | #define MAP3 \ |
| 281 | "(420->10@10072), " \ |
| 282 | "(421->11@10072), " \ |
| 283 | "(422->12@10072), " \ |
| 284 | "(423->13@10072), " \ |
| 285 | "(424->14@10072), " \ |
| 286 | "(425->15@10072), " \ |
| 287 | "(426->16@10072), " \ |
| 288 | "(427->17@10072), " \ |
| 289 | "" |
| 290 | |
| 291 | /* add mapping at time 10010. */ |
| 292 | nr_map_have(&map, 0, 10, 10010); |
| 293 | OSMO_ASSERT(nr_map_is(&map, MAP1)); |
| 294 | |
| 295 | /* tick on unexpired item. */ |
| 296 | OSMO_ASSERT(expiry_tick(&expiry, 10010) == 0); |
| 297 | OSMO_ASSERT(expiry_tick(&expiry, 10011) == 0); |
| 298 | OSMO_ASSERT(nr_map_is(&map, MAP1)); |
| 299 | |
| 300 | /* Spread mappings at 10020, 10021, ... 10027. */ |
| 301 | for (i = 0; i < 8; i++) |
| 302 | nr_map_have(&map, 0, 20 + i, 10020 + i); |
| 303 | OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2)); |
| 304 | |
| 305 | /* tick on unexpired items. */ |
| 306 | OSMO_ASSERT(expiry_tick(&expiry, 10030) == 0); |
| 307 | OSMO_ASSERT(expiry_tick(&expiry, 10039) == 0); |
| 308 | OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2)); |
| 309 | |
| 310 | /* expire the first item (from 10010). */ |
| 311 | OSMO_ASSERT(expiry_tick(&expiry, 10010 + 30) == 1); |
| 312 | OSMO_ASSERT(nr_map_is(&map, MAP2)); |
| 313 | |
| 314 | /* again nothing to expire */ |
| 315 | OSMO_ASSERT(expiry_tick(&expiry, 10041) == 0); |
| 316 | OSMO_ASSERT(nr_map_is(&map, MAP2)); |
| 317 | |
| 318 | /* Mappings all at the same time. */ |
| 319 | for (i = 0; i < 8; i++) |
| 320 | nr_map_have(&map, 0, 420 + i, 10042); |
| 321 | OSMO_ASSERT(nr_map_is(&map, MAP2 MAP3)); |
| 322 | |
| 323 | /* Eight to expire, were added further above to be chronologically |
| 324 | * correct, at 10020..10027. */ |
| 325 | OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 8); |
| 326 | OSMO_ASSERT(nr_map_is(&map, MAP3)); |
| 327 | |
| 328 | /* again nothing to expire */ |
| 329 | OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 0); |
| 330 | OSMO_ASSERT(nr_map_is(&map, MAP3)); |
| 331 | |
| 332 | /* Eight to expire, from 10042. Now at 10042 + 30: */ |
| 333 | OSMO_ASSERT(expiry_tick(&expiry, 10042 + 30) == 8); |
| 334 | OSMO_ASSERT(nr_map_is(&map, "")); |
| 335 | |
| 336 | #undef MAP1 |
| 337 | #undef MAP2 |
| 338 | #undef MAP3 |
| 339 | } |
| 340 | |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 341 | char resolve_ggsn_got_imsi[GSM_IMSI_LENGTH]; |
| 342 | char resolve_ggsn_got_ni[GSM_APN_LENGTH]; |
| 343 | |
| 344 | struct osmo_sockaddr resolved_ggsn_addr; |
| 345 | static int resolve_to_ggsn(const char *addr, uint16_t port) |
| 346 | { |
| 347 | LVL2_ASSERT(osmo_sockaddr_init_udp(&resolved_ggsn_addr, |
| 348 | addr, port) |
| 349 | == 0); |
| 350 | return 1; |
| 351 | } |
| 352 | |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 353 | struct osmo_sockaddr resolved_sgsn_addr; |
| 354 | static int resolve_to_sgsn(const char *addr, uint16_t port) |
| 355 | { |
| 356 | LVL2_ASSERT(osmo_sockaddr_init_udp(&resolved_sgsn_addr, |
| 357 | addr, port) |
| 358 | == 0); |
| 359 | return 1; |
| 360 | } |
| 361 | |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 362 | struct osmo_sockaddr sgsn_sender; |
| 363 | static int send_from_sgsn(const char *addr, uint16_t port) |
| 364 | { |
| 365 | LVL2_ASSERT(osmo_sockaddr_init_udp(&sgsn_sender, |
| 366 | addr, port) |
| 367 | == 0); |
| 368 | return 1; |
| 369 | } |
| 370 | |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 371 | struct osmo_sockaddr ggsn_sender; |
| 372 | static int send_from_ggsn(const char *addr, uint16_t port) |
| 373 | { |
| 374 | LVL2_ASSERT(osmo_sockaddr_init_udp(&ggsn_sender, |
| 375 | addr, port) |
| 376 | == 0); |
| 377 | return 1; |
| 378 | } |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 379 | |
| 380 | |
| 381 | /* override, requires '-Wl,--wrap=gtphub_resolve_ggsn_addr' */ |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 382 | struct gtphub_peer_port *__real_gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 383 | const char *imsi_str, |
| 384 | const char *apn_ni_str); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 385 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 386 | struct gtphub_peer_port *__wrap_gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 387 | const char *imsi_str, |
| 388 | const char *apn_ni_str) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 389 | { |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 390 | struct gsn_addr resolved_gsna; |
| 391 | uint16_t resolved_port; |
| 392 | |
| 393 | OSMO_ASSERT(gsn_addr_from_sockaddr(&resolved_gsna, &resolved_port, |
| 394 | &resolved_ggsn_addr) == 0); |
| 395 | |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 396 | struct gtphub_peer_port *pp; |
| 397 | pp = gtphub_port_have(hub, &hub->to_ggsns[GTPH_PLANE_CTRL], |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 398 | &resolved_gsna, resolved_port); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 399 | printf("- __wrap_gtphub_resolve_ggsn_addr():\n" |
| 400 | " returning GGSN addr from imsi %s ni %s: %s\n", |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 401 | imsi_str, apn_ni_str, gtphub_port_str(pp)); |
| 402 | |
| 403 | if (imsi_str) { |
| 404 | strncpy(resolve_ggsn_got_imsi, imsi_str, sizeof(resolve_ggsn_got_imsi)); |
| 405 | resolve_ggsn_got_imsi[sizeof(resolve_ggsn_got_imsi) - 1] = '\0'; |
| 406 | } |
| 407 | else |
| 408 | strcpy(resolve_ggsn_got_imsi, "(null)"); |
| 409 | |
| 410 | if (apn_ni_str) { |
| 411 | strncpy(resolve_ggsn_got_ni, apn_ni_str, sizeof(resolve_ggsn_got_ni)); |
| 412 | resolve_ggsn_got_ni[sizeof(resolve_ggsn_got_ni) - 1] = '\0'; |
| 413 | } |
| 414 | else |
| 415 | strcpy(resolve_ggsn_got_ni, "(null)"); |
| 416 | |
| 417 | return pp; |
| 418 | } |
| 419 | |
| 420 | #define was_resolved_for(IMSI,NI) _was_resolved_for(IMSI, NI, __FILE__, __LINE__) |
| 421 | static int _was_resolved_for(const char *imsi, const char *ni, const char *file, int line) |
| 422 | { |
| 423 | int cmp0 = strncmp(imsi, resolve_ggsn_got_imsi, sizeof(resolve_ggsn_got_imsi)); |
| 424 | |
| 425 | if (cmp0 != 0) { |
| 426 | printf("\n%s:%d: was_resolved_for(): MISMATCH for IMSI\n" |
| 427 | " expecting: '%s'\n" |
| 428 | " got: '%s'\n\n", |
| 429 | file, |
| 430 | line, |
| 431 | imsi, resolve_ggsn_got_imsi); |
| 432 | } |
| 433 | |
| 434 | int cmp1 = strncmp(ni, resolve_ggsn_got_ni, sizeof(resolve_ggsn_got_ni)); |
| 435 | if (cmp1 != 0) { |
| 436 | printf("\n%s:%d: was_resolved_for(): MISMATCH for NI\n" |
| 437 | " expecting: '%s'\n" |
| 438 | " got: '%s'\n\n", |
| 439 | file, |
| 440 | line, |
| 441 | ni, resolve_ggsn_got_ni); |
| 442 | } |
| 443 | |
| 444 | return (cmp0 == 0) && (cmp1 == 0); |
| 445 | } |
| 446 | |
| 447 | /* override, requires '-Wl,--wrap=gtphub_ares_init' */ |
| 448 | int __real_gtphub_ares_init(struct gtphub *hub); |
| 449 | |
| 450 | int __wrap_gtphub_ares_init(struct gtphub *hub) |
| 451 | { |
| 452 | /* Do nothing. */ |
| 453 | return 0; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | #define buf_len 1024 |
| 457 | static uint8_t buf[buf_len]; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 458 | static uint8_t *reply_buf; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 459 | |
| 460 | static unsigned int msg(const char *hex) |
| 461 | { |
| 462 | unsigned int l = osmo_hexparse(hex, buf, buf_len); |
| 463 | OSMO_ASSERT(l > 0); |
| 464 | return l; |
| 465 | } |
| 466 | |
| 467 | /* Compare static buf to given string constant. The amount of bytes is obtained |
| 468 | * from parsing the GTP header in buf. hex must match an osmo_hexdump() of the |
| 469 | * desired message. Return 1 if size and content match. */ |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 470 | #define reply_is(MSG) _reply_is(MSG, __FILE__, __LINE__) |
| 471 | static int _reply_is(const char *hex, const char *file, int line) |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 472 | { |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 473 | struct gtp1_header_long *h = (void*)reply_buf; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 474 | int len = ntoh16(h->length) + 8; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 475 | const char *dump = osmo_hexdump_nospc(reply_buf, len); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 476 | int cmp = strcmp(dump, hex); |
| 477 | |
| 478 | if (cmp != 0) { |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 479 | printf("\n%s:%d: reply_is(): MISMATCH\n" |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 480 | " expecting:\n'%s'\n" |
| 481 | " got:\n'%s'\n\n", |
| 482 | file, |
| 483 | line, |
| 484 | hex, dump); |
| 485 | int i; |
| 486 | int l = strlen(hex); |
| 487 | int m = strlen(dump); |
| 488 | if (m < l) |
| 489 | l = m; |
| 490 | for (i = 0; i < l; i++) { |
| 491 | if (hex[i] != dump[i]) { |
| 492 | printf("First mismatch at position %d:\n" |
| 493 | " %s\n %s\n", i, hex + i, dump + i); |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | return cmp == 0; |
| 499 | } |
| 500 | |
| 501 | #define same_addr(GOT, EXPECTED) _same_addr((GOT),(EXPECTED), __FILE__, __LINE__) |
| 502 | static int _same_addr(const struct osmo_sockaddr *got, |
| 503 | const struct osmo_sockaddr *expected, |
| 504 | const char *file, int line) |
| 505 | { |
| 506 | int cmp = osmo_sockaddr_cmp(got, expected); |
| 507 | if (!cmp) |
| 508 | return 1; |
| 509 | char buf[256]; |
| 510 | printf("\n%s:%d: addr_is(): MISMATCH\n" |
| 511 | " expecting: '%s'\n" |
| 512 | " got: '%s'\n\n", |
| 513 | file, line, |
| 514 | osmo_sockaddr_to_str(expected), |
| 515 | osmo_sockaddr_to_strb(got, buf, sizeof(buf))); |
| 516 | return 0; |
| 517 | } |
| 518 | |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 519 | |
| 520 | time_t now; |
| 521 | static struct gtphub _hub; |
| 522 | static struct gtphub *hub = &_hub; |
| 523 | |
| 524 | static int setup_test_hub() |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 525 | { |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 526 | /* Not really needed, but to make 100% sure... */ |
| 527 | ZERO_STRUCT(hub); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 528 | |
| 529 | gtphub_init(hub); |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 530 | |
| 531 | /* Tell this mock gtphub its local address for this test. */ |
| 532 | LVL2_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_CTRL].local_addr, |
| 533 | "127.0.1.1") == 0); |
| 534 | LVL2_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_USER].local_addr, |
| 535 | "127.0.1.2") == 0); |
| 536 | LVL2_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_CTRL].local_addr, |
| 537 | "127.0.2.1") == 0); |
| 538 | LVL2_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_USER].local_addr, |
| 539 | "127.0.2.2") == 0); |
| 540 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 541 | hub->restart_counter = 0x23; |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 542 | now = 345; |
| 543 | LVL2_ASSERT(send_from_sgsn("192.168.42.23", 423)); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 544 | LVL2_ASSERT(resolve_to_ggsn("192.168.43.34", 2123)); |
| 545 | LVL2_ASSERT(send_from_ggsn("192.168.43.34", 321)); |
| 546 | LVL2_ASSERT(resolve_to_sgsn("192.168.42.23", 2123)); |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 547 | |
| 548 | return 1; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | static void test_echo(void) |
| 553 | { |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 554 | LOG("test_echo"); |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 555 | OSMO_ASSERT(setup_test_hub()); |
| 556 | |
| 557 | now = 123; |
Neels Hofmeyr | 30f7bcb | 2015-11-08 20:34:47 +0100 | [diff] [blame] | 558 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 559 | const char *gtp_ping_from_sgsn = |
| 560 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ |
| 561 | "01" /* type 01: Echo request */ |
| 562 | "0004" /* length of 4 after header TEI */ |
| 563 | "00000000" /* header TEI == 0 in Echo */ |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 564 | "abcd" /* some 2 octet sequence nr */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 565 | "0000" /* N-PDU 0, no extension header (why is this here?) */ |
| 566 | ; |
| 567 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 568 | const char *gtp_pong_to_sgsn = |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 569 | "32" |
| 570 | "02" /* type 02: Echo response */ |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 571 | "0006" /* length of 6 after header TEI */ |
| 572 | "00000000" /* header TEI == 0 in Echo */ |
| 573 | "abcd" /* same sequence nr */ |
| 574 | "0000" |
| 575 | "0e23" /* Recovery with restart counter */ |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 576 | ; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 577 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 578 | struct osmo_fd *ggsn_ofd = NULL; |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 579 | struct osmo_sockaddr to_addr; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 580 | int send; |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 581 | send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &sgsn_sender, |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 582 | buf, msg(gtp_ping_from_sgsn), now, |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 583 | &reply_buf, &ggsn_ofd, &to_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 584 | OSMO_ASSERT(send > 0); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 585 | OSMO_ASSERT(to_addr.l); |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 586 | OSMO_ASSERT(same_addr(&to_addr, &sgsn_sender)); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 587 | OSMO_ASSERT(reply_is(gtp_pong_to_sgsn)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 588 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 589 | struct gtphub_peer_port *sgsn_port = |
| 590 | gtphub_port_find_sa(&hub->to_sgsns[GTPH_PLANE_CTRL], |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 591 | &sgsn_sender); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 592 | /* We don't record Echo peers. */ |
| 593 | OSMO_ASSERT(!sgsn_port); |
| 594 | |
| 595 | const char *gtp_ping_from_ggsn = |
| 596 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ |
| 597 | "01" /* type 01: Echo request */ |
| 598 | "0004" /* length of 4 after header TEI */ |
| 599 | "00000000" /* header TEI == 0 in Echo */ |
| 600 | "cdef" /* some 2 octet sequence nr */ |
| 601 | "0000" /* N-PDU 0, no extension header (why is this here?) */ |
| 602 | ; |
| 603 | |
| 604 | const char *gtp_pong_to_ggsn = |
| 605 | "32" |
| 606 | "02" /* type 02: Echo response */ |
| 607 | "0006" /* length of 6 after header TEI */ |
| 608 | "00000000" /* header TEI == 0 in Echo */ |
| 609 | "cdef" /* same sequence nr */ |
| 610 | "0000" |
| 611 | "0e23" /* Recovery with restart counter */ |
| 612 | ; |
| 613 | |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 614 | struct osmo_fd *sgsn_ofd = NULL; |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 615 | send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &ggsn_sender, |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 616 | buf, msg(gtp_ping_from_ggsn), now, |
| 617 | &reply_buf, &sgsn_ofd, &to_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 618 | OSMO_ASSERT(send > 0); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 619 | OSMO_ASSERT(same_addr(&to_addr, &ggsn_sender)); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 620 | OSMO_ASSERT(reply_is(gtp_pong_to_ggsn)); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 621 | |
| 622 | struct gtphub_peer_port *ggsn_port = |
| 623 | gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL], |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 624 | &sgsn_sender); |
Neels Hofmeyr | bb3d678 | 2015-11-09 15:12:25 +0100 | [diff] [blame] | 625 | /* We don't record Echo peers. */ |
| 626 | OSMO_ASSERT(!ggsn_port); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 627 | |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 628 | now += EXPIRE_ALL; |
| 629 | gtphub_gc(hub, now); |
| 630 | } |
| 631 | |
| 632 | |
| 633 | #define MSG_PDP_CTX_REQ(len, seq, restart, imsi, tei_u, tei_c, apn, gsn_c, gsn_u) \ |
| 634 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \ |
| 635 | "10" /* type 16: Create PDP Context Request */ \ |
| 636 | len /* msg length = 8 + len (2 octets) */ \ |
| 637 | "00000000" /* No TEI yet */ \ |
| 638 | seq /* Sequence nr (2 octets) */ \ |
| 639 | "00" /* N-PDU 0 */ \ |
| 640 | "00" /* No extensions */ \ |
| 641 | /* IEs */ \ |
| 642 | "0e" restart /* 14: Recovery = 96 (restart counter: 1 octet) */ \ |
| 643 | "02" /* 2 = IMSI */ \ |
| 644 | imsi /* (8 octets) */ \ |
| 645 | "0f01" /* 15: Selection mode = MS provided APN, subscription not verified*/ \ |
| 646 | "10" /* 16: TEI Data I */ \ |
| 647 | tei_u /* (4 octets) */ \ |
| 648 | "11" /* 17: TEI Control Plane */ \ |
| 649 | tei_c /* (4 octets) */ \ |
| 650 | "1400" /* 20: NSAPI = 0*/ \ |
| 651 | "1a" /* 26: Charging Characteristics */ \ |
| 652 | "0800" \ |
| 653 | "80" /* 128: End User Address */ \ |
| 654 | "0002" /* length = 2: empty PDP Address */ \ |
| 655 | "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ \ |
| 656 | "83" /* 131: Access Point Name */ \ |
| 657 | apn /* (2 octets length, N octets encoded APN-NI) */ \ |
| 658 | "84" /* 132: Protocol Configuration Options */ \ |
| 659 | "0015" /* length = 21 */ \ |
| 660 | "80c0231101010011036d69670868656d6d656c6967" \ |
| 661 | "85" /* 133: GSN Address */ \ |
| 662 | gsn_c /* (2 octets length, N octets addr) */ \ |
| 663 | "85" /* 133: GSN Address (second entry) */ \ |
| 664 | gsn_u /* (2 octets length, N octets addr) */ \ |
| 665 | "86" /* 134: MS International PSTN/ISDN Number (MSISDN) */ \ |
| 666 | "0007" /* length */ \ |
| 667 | "916407123254f6" /* 1946702123456(f) */ \ |
| 668 | "87" /* 135: Quality of Service (QoS) Profile */ \ |
| 669 | "0004" /* length */ \ |
| 670 | "00" /* priority */ \ |
| 671 | "0b921f" /* QoS profile data */ |
| 672 | |
| 673 | #define MSG_PDP_CTX_RSP(len, tei_h, seq, restart, tei_u, tei_c, gsn_c, gsn_u) \ |
| 674 | "32" \ |
| 675 | "11" /* Create PDP Context Response */ \ |
| 676 | len /* msg length = 8 + len (2 octets) */ \ |
| 677 | tei_h /* destination TEI (sent in req above) */ \ |
| 678 | seq /* mapped seq */ \ |
| 679 | "00" "00" \ |
| 680 | /* IEs */ \ |
| 681 | "01" /* 1: Cause */ \ |
| 682 | "80" /* value = 0b10000000 = response, no rejection. */ \ |
| 683 | "08" /* 8: Reordering Required */ \ |
| 684 | "00" /* not required. */ \ |
| 685 | "0e" restart /* 14: Recovery = 1 */ \ |
| 686 | "10" /* 16: TEI Data I */ \ |
| 687 | tei_u \ |
| 688 | "11" /* 17: TEI Control */ \ |
| 689 | tei_c \ |
| 690 | "7f" /* 127: Charging ID */ \ |
| 691 | "00000001" \ |
| 692 | "80" /* 128: End User Address */ \ |
| 693 | "0006" /* length = 6 */ \ |
| 694 | "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ \ |
| 695 | "7f000002" \ |
| 696 | "84" /* 132: Protocol Configuration Options */ \ |
| 697 | "0014" /* len = 20 */ \ |
| 698 | "8080211002000010810608080808830600000000" \ |
| 699 | "85" /* 133: GSN Address (Ctrl) */ \ |
| 700 | gsn_c \ |
| 701 | "85" /* 133: GSN Address (User) */ \ |
| 702 | gsn_u \ |
| 703 | "87" /* 135: Quality of Service (QoS) Profile */ \ |
| 704 | "0004" /* length */ \ |
| 705 | "00" /* priority */ \ |
| 706 | "0b921f" /* QoS profile data */ |
| 707 | |
| 708 | #define msg_from_sgsn_c(A,B,C,D) msg_from_sgsn(GTPH_PLANE_CTRL, A,B,C,D) |
| 709 | #define msg_from_sgsn_u(A,B,C,D) msg_from_sgsn(GTPH_PLANE_USER, A,B,C,D) |
| 710 | static int msg_from_sgsn(int plane_idx, |
| 711 | struct osmo_sockaddr *_sgsn_sender, |
| 712 | struct osmo_sockaddr *ggsn_receiver, |
| 713 | const char *hex_from_sgsn, |
| 714 | const char *hex_to_ggsn) |
| 715 | { |
| 716 | struct osmo_fd *ggsn_ofd = NULL; |
| 717 | struct osmo_sockaddr ggsn_addr; |
| 718 | int send; |
| 719 | send = gtphub_from_sgsns_handle_buf(hub, plane_idx, _sgsn_sender, buf, |
| 720 | msg(hex_from_sgsn), now, |
| 721 | &reply_buf, &ggsn_ofd, &ggsn_addr); |
| 722 | LVL2_ASSERT(send > 0); |
| 723 | LVL2_ASSERT(same_addr(&ggsn_addr, ggsn_receiver)); |
| 724 | LVL2_ASSERT(reply_is(hex_to_ggsn)); |
| 725 | return 1; |
| 726 | } |
| 727 | |
| 728 | #define msg_from_ggsn_c(A,B,C,D) msg_from_ggsn(GTPH_PLANE_CTRL, A,B,C,D) |
| 729 | #define msg_from_ggsn_u(A,B,C,D) msg_from_ggsn(GTPH_PLANE_USER, A,B,C,D) |
| 730 | static int msg_from_ggsn(int plane_idx, |
| 731 | struct osmo_sockaddr *ggsn_sender, |
| 732 | struct osmo_sockaddr *sgsn_receiver, |
| 733 | const char *msg_from_ggsn, |
| 734 | const char *msg_to_sgsn) |
| 735 | { |
| 736 | struct osmo_fd *sgsn_ofd; |
| 737 | struct osmo_sockaddr sgsn_addr; |
| 738 | int send; |
| 739 | send = gtphub_from_ggsns_handle_buf(hub, plane_idx, ggsn_sender, buf, |
| 740 | msg(msg_from_ggsn), now, |
| 741 | &reply_buf, &sgsn_ofd, &sgsn_addr); |
| 742 | LVL2_ASSERT(send > 0); |
| 743 | LVL2_ASSERT(same_addr(&sgsn_addr, sgsn_receiver)); |
| 744 | LVL2_ASSERT(reply_is(msg_to_sgsn)); |
| 745 | return 1; |
| 746 | } |
| 747 | |
| 748 | static int create_pdp_ctx() |
| 749 | { |
| 750 | const char *gtp_req_from_sgsn = MSG_PDP_CTX_REQ("0068", |
| 751 | "abcd", |
| 752 | "60", |
| 753 | "42000121436587f9", |
| 754 | "00000123", |
| 755 | "00000321", |
| 756 | "0009""08696e7465726e6574", /* "(8)internet" */ |
| 757 | "0004""c0a82a17", /* same as default sgsn_sender */ |
| 758 | "0004""c0a82a17" |
| 759 | ); |
| 760 | const char *gtp_req_to_ggsn = MSG_PDP_CTX_REQ("0068", |
| 761 | "6d31", /* mapped seq ("abcd") */ |
| 762 | "60", |
| 763 | "42000121436587f9", |
| 764 | "00000001", /* mapped TEI Data I ("123") */ |
| 765 | "00000001", /* mapped TEI Control ("321") */ |
| 766 | "0009""08696e7465726e6574", |
| 767 | "0004""7f000201", /* replaced with gtphub's address ggsn ctrl */ |
| 768 | "0004""7f000202" /* replaced with gtphub's address ggsn user */ |
| 769 | ); |
| 770 | |
| 771 | LVL2_ASSERT(msg_from_sgsn_c(&sgsn_sender, |
| 772 | &resolved_ggsn_addr, |
| 773 | gtp_req_from_sgsn, |
| 774 | gtp_req_to_ggsn)); |
| 775 | LVL2_ASSERT(was_resolved_for("240010123456789", "internet")); |
| 776 | |
| 777 | const char *gtp_resp_from_ggsn = MSG_PDP_CTX_RSP("004e", |
| 778 | "00000001", /* destination TEI (sent in req above) */ |
| 779 | "6d31", /* mapped seq */ |
| 780 | "01", /* restart */ |
| 781 | "00000567", /* TEI U */ |
| 782 | "00000765", /* TEI C */ |
| 783 | "0004""c0a82b22", /* GSN addresses */ |
| 784 | "0004""c0a82b22" /* (== resolved_ggsn_addr) */ |
| 785 | ); |
| 786 | const char *gtp_resp_to_sgsn = MSG_PDP_CTX_RSP("004e", |
| 787 | "00000321", /* unmapped TEI ("001") */ |
| 788 | "abcd", /* unmapped seq ("6d31") */ |
| 789 | "01", |
| 790 | "00000002", /* mapped TEI from GGSN ("567") */ |
| 791 | "00000002", /* mapped TEI from GGSN ("765") */ |
| 792 | "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */ |
| 793 | "0004""7f000102" /* gtphub's address towards SGSNs (User) */ |
| 794 | ); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 795 | /* The response should go back to whichever port the request came from |
| 796 | * (unmapped by sequence nr) */ |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 797 | LVL2_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr, |
| 798 | &sgsn_sender, |
| 799 | gtp_resp_from_ggsn, |
| 800 | gtp_resp_to_sgsn)); |
| 801 | |
| 802 | return 1; |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static void test_create_pdp_ctx(void) |
| 806 | { |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 807 | LOG("test_create_pdp_ctx"); |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 808 | OSMO_ASSERT(setup_test_hub()); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 809 | |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 810 | OSMO_ASSERT(create_pdp_ctx()); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 811 | |
| 812 | struct gtphub_peer_port *ggsn_port = |
| 813 | gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL], |
Neels Hofmeyr | c227594 | 2015-11-10 22:07:04 +0100 | [diff] [blame] | 814 | &resolved_ggsn_addr); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 815 | OSMO_ASSERT(ggsn_port); |
| 816 | struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer; |
| 817 | /* now == 345; now + 30 == 375. |
| 818 | * seq mapping from above: |
| 819 | * 0xabcd == 43981 (sent in the packet) |
| 820 | * 0x6d31 == 27953 (harcoded seq mapping start val) */ |
| 821 | OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@375), ")); |
| 822 | |
| 823 | /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945. |
| 824 | * 0x00000321 == 801 (TEI from SGSN Ctrl) |
| 825 | * 0x00000123 == 291 (TEI from SGSN User) |
| 826 | * 0x00000765 == 1893 (TEI from GGSN Ctrl) |
| 827 | * 0x00000567 == 1383 (TEI from GGSN User) |
| 828 | * Mapped TEIs should be 1 and 2. */ |
| 829 | OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_CTRL], "(801->1@21945), (1893->2@21945), ")); |
| 830 | OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_USER], "(291->1@21945), (1383->2@21945), ")); |
| 831 | |
| 832 | gtphub_gc(hub, now + EXPIRE_ALL); |
| 833 | } |
| 834 | |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 835 | static void test_user_data(void) |
| 836 | { |
| 837 | LOG("test_user_data"); |
| 838 | |
| 839 | OSMO_ASSERT(setup_test_hub()); |
| 840 | |
| 841 | OSMO_ASSERT(create_pdp_ctx()); |
| 842 | |
| 843 | LOG("- user data starts"); |
| 844 | /* Now expect default port numbers for User. */ |
| 845 | resolve_to_ggsn("192.168.43.34", 2152); |
| 846 | resolve_to_sgsn("192.168.42.23", 2152); |
| 847 | |
| 848 | const char *u_from_ggsn = |
| 849 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \ |
| 850 | "ff" /* type 255: G-PDU */ |
| 851 | "0058" /* length: 88 + 8 octets == 96 */ |
| 852 | "00000001" /* mapped User TEI for SGSN from create_pdp_ctx() */ |
| 853 | "0070" /* seq */ |
| 854 | "0000" /* No extensions */ |
| 855 | /* User data (ICMP packet), 96 - 12 = 84 octets */ |
| 856 | "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8" |
| 857 | "4156000000007241010000000000101112131415161718191a1b1c1d1e1f" |
| 858 | "202122232425262728292a2b2c2d2e2f3031323334353637" |
| 859 | ; |
| 860 | const char *u_to_sgsn = |
| 861 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \ |
| 862 | "ff" /* type 255: G-PDU */ |
| 863 | "0058" /* length: 88 + 8 octets == 96 */ |
| 864 | "00000123" /* unmapped User TEI */ |
| 865 | "6d31" /* new mapped seq */ |
| 866 | "0000" |
| 867 | "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8" |
| 868 | "4156000000007241010000000000101112131415161718191a1b1c1d1e1f" |
| 869 | "202122232425262728292a2b2c2d2e2f3031323334353637" |
| 870 | ; |
| 871 | |
| 872 | /* This depends on create_pdp_ctx() sending resolved_sgsn_addr as GSN |
| 873 | * Address IEs in the GGSN's Create PDP Ctx Response. */ |
| 874 | OSMO_ASSERT(msg_from_ggsn_u(&ggsn_sender, |
| 875 | &resolved_sgsn_addr, |
| 876 | u_from_ggsn, |
| 877 | u_to_sgsn)); |
| 878 | |
| 879 | const char *u_from_sgsn = |
| 880 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \ |
| 881 | "ff" /* type 255: G-PDU */ |
| 882 | "0058" /* length: 88 + 8 octets == 96 */ |
| 883 | "00000002" /* mapped User TEI for GGSN from create_pdp_ctx() */ |
| 884 | "6d31" /* mapped seq */ |
| 885 | "0000" /* No extensions */ |
| 886 | /* User data (ICMP packet), 96 - 12 = 84 octets */ |
| 887 | "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8" |
| 888 | "4156000000007241010000000000101112131415161718191a1b1c1d1e1f" |
| 889 | "202122232425262728292a2b2c2d2e2f3031323334353637" |
| 890 | ; |
| 891 | const char *u_to_ggsn = |
| 892 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \ |
| 893 | "ff" /* type 255: G-PDU */ |
| 894 | "0058" /* length: 88 + 8 octets == 96 */ |
| 895 | "00000567" /* unmapped User TEI */ |
| 896 | "0070" /* unmapped seq */ |
| 897 | "0000" |
| 898 | "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8" |
| 899 | "4156000000007241010000000000101112131415161718191a1b1c1d1e1f" |
| 900 | "202122232425262728292a2b2c2d2e2f3031323334353637" |
| 901 | ; |
| 902 | |
| 903 | OSMO_ASSERT(msg_from_sgsn_u(&resolved_sgsn_addr, |
| 904 | &ggsn_sender, |
| 905 | u_from_sgsn, |
| 906 | u_to_ggsn)); |
| 907 | |
| 908 | gtphub_gc(hub, now + EXPIRE_ALL); |
| 909 | } |
| 910 | |
| 911 | |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 912 | static struct log_info_cat gtphub_categories[] = { |
| 913 | [DGTPHUB] = { |
| 914 | .name = "DGTPHUB", |
| 915 | .description = "GTP Hub", |
| 916 | .color = "\033[1;33m", |
| 917 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 918 | }, |
| 919 | }; |
| 920 | |
| 921 | static struct log_info info = { |
| 922 | .cat = gtphub_categories, |
| 923 | .num_cat = ARRAY_SIZE(gtphub_categories), |
| 924 | }; |
| 925 | |
| 926 | int main(int argc, char **argv) |
| 927 | { |
| 928 | osmo_init_logging(&info); |
| 929 | osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub"); |
| 930 | |
| 931 | test_nr_map_basic(); |
| 932 | test_expiry(); |
| 933 | test_echo(); |
| 934 | test_create_pdp_ctx(); |
Neels Hofmeyr | e921e32 | 2015-11-11 00:45:50 +0100 | [diff] [blame^] | 935 | test_user_data(); |
Neels Hofmeyr | c8a614d | 2015-09-24 17:32:30 +0200 | [diff] [blame] | 936 | printf("Done\n"); |
| 937 | |
| 938 | talloc_report_full(osmo_gtphub_ctx, stderr); |
| 939 | OSMO_ASSERT(talloc_total_blocks(osmo_gtphub_ctx) == 1); |
| 940 | return 0; |
| 941 | } |
| 942 | |