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 | |
| 40 | /* Make non-public API accessible */ |
| 41 | |
| 42 | void gtphub_init(struct gtphub *hub); |
| 43 | |
| 44 | void *osmo_gtphub_ctx; |
| 45 | |
| 46 | /* TODO copied from libosmo-abis/src/subchan_demux.c, remove dup */ |
| 47 | static int llist_len(struct llist_head *head) |
| 48 | { |
| 49 | struct llist_head *entry; |
| 50 | int i = 0; |
| 51 | |
| 52 | llist_for_each(entry, head) |
| 53 | i++; |
| 54 | |
| 55 | return i; |
| 56 | } |
| 57 | |
| 58 | static void nr_mapping_free(struct expiring_item *e) |
| 59 | { |
| 60 | struct nr_mapping *m = container_of(e, struct nr_mapping, |
| 61 | expiry_entry); |
| 62 | nr_mapping_del(m); |
| 63 | talloc_free(m); |
| 64 | } |
| 65 | |
| 66 | static struct nr_mapping *nr_mapping_alloc(void) |
| 67 | { |
| 68 | struct nr_mapping *m; |
| 69 | m = talloc(osmo_gtphub_ctx, struct nr_mapping); |
| 70 | nr_mapping_init(m); |
| 71 | m->expiry_entry.del_cb = nr_mapping_free; |
| 72 | return m; |
| 73 | } |
| 74 | |
| 75 | static struct nr_mapping *nr_map_have(struct nr_map *map, void *origin, nr_t orig, time_t now) |
| 76 | { |
| 77 | struct nr_mapping *mapping; |
| 78 | |
| 79 | mapping = nr_map_get(map, origin, orig); |
| 80 | if (!mapping) { |
| 81 | mapping = nr_mapping_alloc(); |
| 82 | mapping->origin = origin; |
| 83 | mapping->orig = orig; |
| 84 | nr_map_add(map, mapping, now); |
| 85 | } |
| 86 | |
| 87 | return mapping; |
| 88 | } |
| 89 | |
| 90 | static nr_t nr_map_verify(const struct nr_map *map, void *origin, nr_t orig, nr_t expect_repl) |
| 91 | { |
| 92 | struct nr_mapping *m; |
| 93 | m = nr_map_get(map, origin, orig); |
| 94 | |
| 95 | if (!m) { |
| 96 | printf("mapping not found for %p %d\n", origin, orig); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | if (m->repl != expect_repl) { |
| 101 | printf("mapping found, but nr mismatches: expect %d, got %d\n", |
| 102 | (int)expect_repl, (int)m->repl); |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | return 1; |
| 107 | } |
| 108 | |
| 109 | static int nr_map_verify_inv(const struct nr_map *map, nr_t repl, |
| 110 | void *expect_origin, nr_t expect_orig) |
| 111 | { |
| 112 | struct nr_mapping *m; |
| 113 | m = nr_map_get_inv(map, repl); |
| 114 | if (!m) { |
| 115 | printf("mapping not found for %d\n", (int)repl); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | if (m->origin != expect_origin) { |
| 120 | printf("mapping found, but origin mismatches: expect %p, got %p\n", |
| 121 | expect_origin, m->origin); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | if (m->orig != expect_orig) { |
| 126 | printf("mapping found, but nr mismatches: expect %d, got %d\n", |
| 127 | (int)expect_orig, (int)m->orig); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static void test_nr_map_basic(void) |
| 136 | { |
| 137 | struct nr_pool _pool; |
| 138 | struct nr_pool *pool = &_pool; |
| 139 | struct nr_map _map; |
| 140 | struct nr_map *map = &_map; |
| 141 | |
| 142 | nr_pool_init(pool); |
| 143 | nr_map_init(map, pool, NULL); |
| 144 | |
| 145 | OSMO_ASSERT(llist_empty(&map->mappings)); |
| 146 | |
| 147 | #define TEST_N_HALF 100 |
| 148 | #define TEST_N (2*TEST_N_HALF) |
| 149 | #define TEST_I 123 |
| 150 | uint32_t i, check_i; |
| 151 | uint32_t m[TEST_N]; |
| 152 | struct nr_mapping *mapping; |
| 153 | |
| 154 | /* create half of TEST_N mappings from one origin */ |
| 155 | void *origin1 = (void*)0x1234; |
| 156 | for (i = 0; i < TEST_N_HALF; i++) { |
| 157 | nr_t orig = TEST_I + i; |
| 158 | mapping = nr_map_have(map, origin1, orig, 0); |
| 159 | m[i] = mapping->repl; |
| 160 | OSMO_ASSERT(m[i] != 0); |
| 161 | OSMO_ASSERT(llist_len(&map->mappings) == (i+1)); |
| 162 | for (check_i = 0; check_i < i; check_i++) |
| 163 | OSMO_ASSERT(m[check_i] != m[i]); |
| 164 | } |
| 165 | OSMO_ASSERT(llist_len(&map->mappings) == TEST_N_HALF); |
| 166 | |
| 167 | /* create another TEST_N mappings with the same original numbers, but |
| 168 | * from a different origin */ |
| 169 | void *origin2 = (void*)0x5678; |
| 170 | for (i = 0; i < TEST_N_HALF; i++) { |
| 171 | int i2 = TEST_N_HALF + i; |
| 172 | nr_t orig = TEST_I + i; |
| 173 | mapping = nr_map_have(map, origin2, orig, 0); |
| 174 | m[i2] = mapping->repl; |
| 175 | OSMO_ASSERT(m[i2] != 0); |
| 176 | OSMO_ASSERT(llist_len(&map->mappings) == (i2+1)); |
| 177 | for (check_i = 0; check_i < i2; check_i++) |
| 178 | OSMO_ASSERT(m[check_i] != m[i2]); |
| 179 | } |
| 180 | OSMO_ASSERT(llist_len(&map->mappings) == TEST_N); |
| 181 | |
| 182 | /* verify mappings */ |
| 183 | for (i = 0; i < TEST_N_HALF; i++) { |
| 184 | nr_t orig = TEST_I + i; |
| 185 | { |
| 186 | OSMO_ASSERT(nr_map_verify(map, origin1, orig, m[i])); |
| 187 | OSMO_ASSERT(nr_map_verify_inv(map, m[i], origin1, orig)); |
| 188 | } |
| 189 | { |
| 190 | int i2 = TEST_N_HALF + i; |
| 191 | OSMO_ASSERT(nr_map_verify(map, origin2, orig, m[i2])); |
| 192 | OSMO_ASSERT(nr_map_verify_inv(map, m[i2], origin2, orig)); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* remove all mappings */ |
| 197 | for (i = 0; i < TEST_N_HALF; i++) { |
| 198 | OSMO_ASSERT(llist_len(&map->mappings) == (TEST_N - 2*i)); |
| 199 | |
| 200 | nr_t orig = TEST_I + i; |
| 201 | nr_mapping_del(nr_map_get(map, origin1, orig)); |
| 202 | nr_mapping_del(nr_map_get(map, origin2, orig)); |
| 203 | } |
| 204 | OSMO_ASSERT(llist_empty(&map->mappings)); |
| 205 | #undef TEST_N |
| 206 | #undef TEST_I |
| 207 | } |
| 208 | |
| 209 | static int nr_map_is(struct nr_map *map, const char *str) |
| 210 | { |
| 211 | static char buf[4096]; |
| 212 | char *pos = buf; |
| 213 | size_t len = sizeof(buf); |
| 214 | struct nr_mapping *m; |
| 215 | llist_for_each_entry(m, &map->mappings, entry) { |
| 216 | size_t wrote = snprintf(pos, len, "(%d->%d@%d), ", |
| 217 | (int)m->orig, |
| 218 | (int)m->repl, |
| 219 | (int)m->expiry_entry.expiry); |
| 220 | OSMO_ASSERT(wrote < len); |
| 221 | pos += wrote; |
| 222 | len -= wrote; |
| 223 | } |
| 224 | *pos = '\0'; |
| 225 | |
| 226 | if (strncmp(buf, str, sizeof(buf)) != 0) { |
| 227 | printf("FAILURE: nr_map_is() mismatches expected value:\n" |
| 228 | "expected: \"%s\"\n" |
| 229 | "is: \"%s\"\n", |
| 230 | str, buf); |
| 231 | return 0; |
| 232 | } |
| 233 | return 1; |
| 234 | } |
| 235 | |
| 236 | static void test_expiry(void) |
| 237 | { |
| 238 | struct expiry expiry; |
| 239 | struct nr_pool pool; |
| 240 | struct nr_map map; |
| 241 | int i; |
| 242 | |
| 243 | expiry_init(&expiry, 30); |
| 244 | nr_pool_init(&pool); |
| 245 | nr_map_init(&map, &pool, &expiry); |
| 246 | OSMO_ASSERT(nr_map_is(&map, "")); |
| 247 | |
| 248 | /* tick on empty map */ |
| 249 | OSMO_ASSERT(expiry_tick(&expiry, 10000) == 0); |
| 250 | OSMO_ASSERT(nr_map_is(&map, "")); |
| 251 | |
| 252 | #define MAP1 \ |
| 253 | "(10->1@10040), " \ |
| 254 | "" |
| 255 | |
| 256 | #define MAP2 \ |
| 257 | "(20->2@10050), " \ |
| 258 | "(21->3@10051), " \ |
| 259 | "(22->4@10052), " \ |
| 260 | "(23->5@10053), " \ |
| 261 | "(24->6@10054), " \ |
| 262 | "(25->7@10055), " \ |
| 263 | "(26->8@10056), " \ |
| 264 | "(27->9@10057), " \ |
| 265 | "" |
| 266 | |
| 267 | #define MAP3 \ |
| 268 | "(420->10@10072), " \ |
| 269 | "(421->11@10072), " \ |
| 270 | "(422->12@10072), " \ |
| 271 | "(423->13@10072), " \ |
| 272 | "(424->14@10072), " \ |
| 273 | "(425->15@10072), " \ |
| 274 | "(426->16@10072), " \ |
| 275 | "(427->17@10072), " \ |
| 276 | "" |
| 277 | |
| 278 | /* add mapping at time 10010. */ |
| 279 | nr_map_have(&map, 0, 10, 10010); |
| 280 | OSMO_ASSERT(nr_map_is(&map, MAP1)); |
| 281 | |
| 282 | /* tick on unexpired item. */ |
| 283 | OSMO_ASSERT(expiry_tick(&expiry, 10010) == 0); |
| 284 | OSMO_ASSERT(expiry_tick(&expiry, 10011) == 0); |
| 285 | OSMO_ASSERT(nr_map_is(&map, MAP1)); |
| 286 | |
| 287 | /* Spread mappings at 10020, 10021, ... 10027. */ |
| 288 | for (i = 0; i < 8; i++) |
| 289 | nr_map_have(&map, 0, 20 + i, 10020 + i); |
| 290 | OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2)); |
| 291 | |
| 292 | /* tick on unexpired items. */ |
| 293 | OSMO_ASSERT(expiry_tick(&expiry, 10030) == 0); |
| 294 | OSMO_ASSERT(expiry_tick(&expiry, 10039) == 0); |
| 295 | OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2)); |
| 296 | |
| 297 | /* expire the first item (from 10010). */ |
| 298 | OSMO_ASSERT(expiry_tick(&expiry, 10010 + 30) == 1); |
| 299 | OSMO_ASSERT(nr_map_is(&map, MAP2)); |
| 300 | |
| 301 | /* again nothing to expire */ |
| 302 | OSMO_ASSERT(expiry_tick(&expiry, 10041) == 0); |
| 303 | OSMO_ASSERT(nr_map_is(&map, MAP2)); |
| 304 | |
| 305 | /* Mappings all at the same time. */ |
| 306 | for (i = 0; i < 8; i++) |
| 307 | nr_map_have(&map, 0, 420 + i, 10042); |
| 308 | OSMO_ASSERT(nr_map_is(&map, MAP2 MAP3)); |
| 309 | |
| 310 | /* Eight to expire, were added further above to be chronologically |
| 311 | * correct, at 10020..10027. */ |
| 312 | OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 8); |
| 313 | OSMO_ASSERT(nr_map_is(&map, MAP3)); |
| 314 | |
| 315 | /* again nothing to expire */ |
| 316 | OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 0); |
| 317 | OSMO_ASSERT(nr_map_is(&map, MAP3)); |
| 318 | |
| 319 | /* Eight to expire, from 10042. Now at 10042 + 30: */ |
| 320 | OSMO_ASSERT(expiry_tick(&expiry, 10042 + 30) == 8); |
| 321 | OSMO_ASSERT(nr_map_is(&map, "")); |
| 322 | |
| 323 | #undef MAP1 |
| 324 | #undef MAP2 |
| 325 | #undef MAP3 |
| 326 | } |
| 327 | |
| 328 | |
| 329 | |
| 330 | /* override, requires '-Wl,--wrap=gtphub_resolve_ggsn_addr' */ |
| 331 | int __real_gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 332 | struct osmo_sockaddr *result, |
| 333 | struct gtp_packet_desc *p); |
| 334 | |
| 335 | struct osmo_sockaddr resolved_ggsn_addr = {.l = 0}; |
| 336 | int __wrap_gtphub_resolve_ggsn_addr(struct gtphub *hub, |
| 337 | struct osmo_sockaddr *result, |
| 338 | struct gtp_packet_desc *p) |
| 339 | { |
| 340 | osmo_sockaddr_copy(result, &resolved_ggsn_addr); |
| 341 | printf("Wrap: returning GGSN addr: %s\n", |
| 342 | osmo_sockaddr_to_str(result)); |
| 343 | return (resolved_ggsn_addr.l != 0)? 0 : -1; |
| 344 | } |
| 345 | |
| 346 | #define buf_len 1024 |
| 347 | static uint8_t buf[buf_len]; |
| 348 | |
| 349 | static unsigned int msg(const char *hex) |
| 350 | { |
| 351 | unsigned int l = osmo_hexparse(hex, buf, buf_len); |
| 352 | OSMO_ASSERT(l > 0); |
| 353 | return l; |
| 354 | } |
| 355 | |
| 356 | /* Compare static buf to given string constant. The amount of bytes is obtained |
| 357 | * from parsing the GTP header in buf. hex must match an osmo_hexdump() of the |
| 358 | * desired message. Return 1 if size and content match. */ |
| 359 | #define msg_is(MSG) _msg_is(MSG, __FILE__, __LINE__) |
| 360 | static int _msg_is(const char *hex, const char *file, int line) |
| 361 | { |
| 362 | struct gtp1_header_long *h = (void*)buf; |
| 363 | int len = ntoh16(h->length) + 8; |
| 364 | const char *dump = osmo_hexdump_nospc(buf, len); |
| 365 | int cmp = strcmp(dump, hex); |
| 366 | |
| 367 | if (cmp != 0) { |
| 368 | printf("\n%s:%d: msg_is(): MISMATCH\n" |
| 369 | " expecting:\n'%s'\n" |
| 370 | " got:\n'%s'\n\n", |
| 371 | file, |
| 372 | line, |
| 373 | hex, dump); |
| 374 | int i; |
| 375 | int l = strlen(hex); |
| 376 | int m = strlen(dump); |
| 377 | if (m < l) |
| 378 | l = m; |
| 379 | for (i = 0; i < l; i++) { |
| 380 | if (hex[i] != dump[i]) { |
| 381 | printf("First mismatch at position %d:\n" |
| 382 | " %s\n %s\n", i, hex + i, dump + i); |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | return cmp == 0; |
| 388 | } |
| 389 | |
| 390 | #define same_addr(GOT, EXPECTED) _same_addr((GOT),(EXPECTED), __FILE__, __LINE__) |
| 391 | static int _same_addr(const struct osmo_sockaddr *got, |
| 392 | const struct osmo_sockaddr *expected, |
| 393 | const char *file, int line) |
| 394 | { |
| 395 | int cmp = osmo_sockaddr_cmp(got, expected); |
| 396 | if (!cmp) |
| 397 | return 1; |
| 398 | char buf[256]; |
| 399 | printf("\n%s:%d: addr_is(): MISMATCH\n" |
| 400 | " expecting: '%s'\n" |
| 401 | " got: '%s'\n\n", |
| 402 | file, line, |
| 403 | osmo_sockaddr_to_str(expected), |
| 404 | osmo_sockaddr_to_strb(got, buf, sizeof(buf))); |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | static void test_echo(void) |
| 409 | { |
| 410 | struct gtphub _hub; |
| 411 | struct gtphub *hub = &_hub; |
| 412 | time_t now = 123; |
| 413 | |
| 414 | gtphub_init(hub); |
| 415 | |
| 416 | const char *gtp_ping_from_sgsn = |
| 417 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ |
| 418 | "01" /* type 01: Echo request */ |
| 419 | "0004" /* length of 4 after header TEI */ |
| 420 | "00000000" /* header TEI == 0 in Echo */ |
| 421 | "abcd" /* some 16 octet sequence nr */ |
| 422 | "0000" /* N-PDU 0, no extension header (why is this here?) */ |
| 423 | ; |
| 424 | |
| 425 | /* Same with mapped sequence number */ |
| 426 | const char *gtp_ping_to_ggsn = |
| 427 | "32" "01" "0004" "00000000" |
| 428 | "6d31" /* mapped seq */ |
| 429 | "00" "00"; |
| 430 | |
| 431 | const char *gtp_pong_from_ggsn = |
| 432 | "32" |
| 433 | "02" /* type 02: Echo response */ |
| 434 | "0006" /* len */ |
| 435 | "00000000" /* tei */ |
| 436 | "6d31" /* mapped seq */ |
| 437 | "0000" /* ext */ |
| 438 | "0e01" /* 0e: Recovery, val == 1 */ |
| 439 | ; |
| 440 | /* Same with unmapped sequence number */ |
| 441 | const char *gtp_pong_to_sgsn = |
| 442 | "32" "02" "0006" "00000000" |
| 443 | "abcd" /* unmapped seq */ |
| 444 | "00" "00" "0e01"; |
| 445 | |
| 446 | /* Set the GGSN address that gtphub is forced to resolve to. */ |
| 447 | OSMO_ASSERT(osmo_sockaddr_init_udp(&resolved_ggsn_addr, |
| 448 | "192.168.43.34", 434) |
| 449 | == 0); |
| 450 | |
| 451 | /* according to spec, we'd always send to port 2123 instead... |
| 452 | struct osmo_sockaddr ggsn_standard_port; |
| 453 | OSMO_ASSERT(osmo_sockaddr_init_udp(&ggsn_standard_port, |
| 454 | "192.168.43.34", 2123) |
| 455 | == 0); |
| 456 | */ |
| 457 | |
| 458 | struct osmo_sockaddr orig_sgsn_addr; |
| 459 | OSMO_ASSERT(osmo_sockaddr_init(&orig_sgsn_addr, |
| 460 | AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, |
| 461 | "192.168.42.23", 423) == 0); |
| 462 | struct osmo_fd *ggsn_ofd = NULL; |
| 463 | struct osmo_sockaddr ggsn_addr; |
| 464 | int send; |
| 465 | send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_sgsn_addr, |
| 466 | buf, msg(gtp_ping_from_sgsn), now, |
| 467 | &ggsn_ofd, &ggsn_addr); |
| 468 | OSMO_ASSERT(send > 0); |
| 469 | OSMO_ASSERT(ggsn_addr.l); |
| 470 | OSMO_ASSERT(same_addr(&ggsn_addr, &resolved_ggsn_addr)); |
| 471 | OSMO_ASSERT(msg_is(gtp_ping_to_ggsn)); |
| 472 | |
| 473 | struct osmo_fd *sgsn_ofd; |
| 474 | struct osmo_sockaddr sgsn_addr; |
| 475 | send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &ggsn_addr, |
| 476 | buf, msg(gtp_pong_from_ggsn), now, |
| 477 | &sgsn_ofd, &sgsn_addr); |
| 478 | OSMO_ASSERT(send > 0); |
| 479 | OSMO_ASSERT(same_addr(&sgsn_addr, &orig_sgsn_addr)); |
| 480 | OSMO_ASSERT(msg_is(gtp_pong_to_sgsn)); |
| 481 | |
| 482 | struct gtphub_peer_port *ggsn_port = |
| 483 | gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL], |
| 484 | &resolved_ggsn_addr); |
| 485 | OSMO_ASSERT(ggsn_port); |
| 486 | struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer; |
| 487 | /* now == 123; now + 30 == 153. */ |
| 488 | OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@153), ")); |
| 489 | |
| 490 | OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_CTRL], "")); |
| 491 | OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_USER], "")); |
| 492 | |
| 493 | gtphub_gc(hub, now + EXPIRE_ALL); |
| 494 | } |
| 495 | |
| 496 | static void test_create_pdp_ctx(void) |
| 497 | { |
| 498 | struct gtphub _hub; |
| 499 | struct gtphub *hub = &_hub; |
| 500 | time_t now = 345; |
| 501 | |
| 502 | gtphub_init(hub); |
| 503 | |
| 504 | /* Tell this mock gtphub its local address for this test. */ |
| 505 | OSMO_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_CTRL].local_addr, |
| 506 | "127.0.1.1") == 0); |
| 507 | OSMO_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_USER].local_addr, |
| 508 | "127.0.1.2") == 0); |
| 509 | OSMO_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_CTRL].local_addr, |
| 510 | "127.0.2.1") == 0); |
| 511 | OSMO_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_USER].local_addr, |
| 512 | "127.0.2.2") == 0); |
| 513 | |
| 514 | /* This is copied from a packet that sgsnemu sends. */ |
| 515 | const char *gtp_req_from_sgsn = |
| 516 | "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ |
| 517 | "10" /* type 16: Create PDP Context Request */ |
| 518 | "0067" /* length = 8 + 103 */ |
| 519 | "00000000" /* No TEI yet */ |
| 520 | "abcd" /* Sequence nr */ |
| 521 | "00" /* N-PDU 0 */ |
| 522 | "00" /* No extensions */ |
| 523 | /* IEs */ |
| 524 | "02" /* 2 = IMSI */ |
| 525 | "42000121436587f9" |
| 526 | "0e" "60" /* 14: Recovery = 96 */ |
| 527 | "0f01" /* 15: Selection mode = MS provided APN, subscription not verified*/ |
| 528 | "10" /* 16: TEI Data I */ |
| 529 | "00000123" |
| 530 | "11" /* 17: TEI Control Plane */ |
| 531 | "00000321" |
| 532 | "1400" /* 20: NSAPI = 0*/ |
| 533 | "1a" /* 26: Charging Characteristics */ |
| 534 | "0800" |
| 535 | "80" /* 128: End User Address */ |
| 536 | "0002" /* length = 2: empty PDP Address */ |
| 537 | "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ |
| 538 | "83" /* 131: Access Point Name */ |
| 539 | "0008" /* length = 8 */ |
| 540 | "696e7465726e6574" /* "internet" */ |
| 541 | "84" /* 132: Protocol Configuration Options */ |
| 542 | "0015" /* length = 21 */ |
| 543 | "80c0231101010011036d69670868656d6d656c6967" |
| 544 | "85" /* 133: GSN Address */ |
| 545 | "0004" /* length */ |
| 546 | "abcdef00" |
| 547 | "85" /* 133: GSN Address (second entry) */ |
| 548 | "0004" /* length */ |
| 549 | "fedcba00" |
| 550 | "86" /* 134: MS International PSTN/ISDN Number (MSISDN) */ |
| 551 | "0007" /* length */ |
| 552 | "916407123254f6" /* 1946702123456(f) */ |
| 553 | "87" /* 135: Quality of Service (QoS) Profile */ |
| 554 | "0004" /* length */ |
| 555 | "00" /* priority */ |
| 556 | "0b921f" /* QoS profile data */ |
| 557 | ; |
| 558 | |
| 559 | const char *gtp_req_to_ggsn = |
| 560 | "32" "10" "0067" "00000000" |
| 561 | "6d31" /* mapped seq ("abcd") */ |
| 562 | "00" "00" "02" "42000121436587f9" "0e60" "0f01" |
| 563 | "10" "00000001" /* mapped TEI Data I ("123") */ |
| 564 | "11" "00000001" /* mapped TEI Control ("321") */ |
| 565 | "1400" "1a" "0800" "80" "0002" "f121" "83" |
| 566 | "0008" "696e7465726e6574" "84" "0015" |
| 567 | "80c0231101010011036d69670868656d6d656c6967" "85" "0004" |
| 568 | "7f000201" /* replaced with gtphub's address ggsn ctrl */ |
| 569 | "85" "0004" |
| 570 | "7f000202" /* replaced with gtphub's address ggsn user */ |
| 571 | "86" "0007" "916407123254f6" |
| 572 | "87" "0004" "00" "0b921f" |
| 573 | ; |
| 574 | |
| 575 | const char *gtp_resp_from_ggsn = |
| 576 | "32" |
| 577 | "11" /* Create PDP Context Response */ |
| 578 | "004e" /* length = 78 + 8 */ |
| 579 | "00000001" /* destination TEI (sent in req above) */ |
| 580 | "6d31" /* mapped seq */ |
| 581 | "00" "00" |
| 582 | /* IEs */ |
| 583 | "01" /* 1: Cause */ |
| 584 | "80" /* value = 0b10000000 = response, no rejection. */ |
| 585 | "08" /* 8: Reordering Required */ |
| 586 | "00" /* not required. */ |
| 587 | "0e" "01" /* 14: Recovery = 1 */ |
| 588 | "10" /* 16: TEI Data I */ |
| 589 | "00000567" |
| 590 | "11" /* 17: TEI Control */ |
| 591 | "00000765" |
| 592 | "7f" /* 127: Charging ID */ |
| 593 | "00000001" |
| 594 | "80" /* 128: End User Address */ |
| 595 | "0006" /* length = 6 */ |
| 596 | "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ |
| 597 | "7f000002" |
| 598 | "84" /* 132: Protocol Configuration Options */ |
| 599 | "0014" /* len = 20 */ |
| 600 | "8080211002000010810608080808830600000000" |
| 601 | "85" /* 133: GSN Address (Ctrl) */ |
| 602 | "0004" /* length */ |
| 603 | "7f000002" |
| 604 | "85" /* 133: GSN Address (User) */ |
| 605 | "0004" /* length */ |
| 606 | "7f000002" |
| 607 | "87" /* 135: Quality of Service (QoS) Profile */ |
| 608 | "0004" /* length */ |
| 609 | "00" /* priority */ |
| 610 | "0b921f" /* QoS profile data */ |
| 611 | ; |
| 612 | |
| 613 | const char *gtp_resp_to_sgsn = |
| 614 | "32" "11" "004e" |
| 615 | "00000321" /* unmapped TEI ("001") */ |
| 616 | "abcd" /* unmapped seq ("6d31") */ |
| 617 | "00" "00" "01" "80" "08" "00" "0e" "01" |
| 618 | "10" "00000002" /* mapped TEI from GGSN ("567") */ |
| 619 | "11" "00000002" /* mapped TEI from GGSN ("765") */ |
| 620 | "7f" "00000001" "80" "0006" "f121" "7f000002" "84" "0014" |
| 621 | "8080211002000010810608080808830600000000" |
| 622 | "85" "0004" |
| 623 | "7f000101" /* gtphub's address towards SGSNs (Ctrl) */ |
| 624 | "85" "0004" |
| 625 | "7f000102" /* gtphub's address towards SGSNs (User) */ |
| 626 | "87" "0004" "00" "0b921f" |
| 627 | ; |
| 628 | |
| 629 | /* Set the GGSN address that gtphub is forced to resolve to. */ |
| 630 | OSMO_ASSERT(osmo_sockaddr_init_udp(&resolved_ggsn_addr, |
| 631 | "192.168.43.34", 434) |
| 632 | == 0); |
| 633 | |
| 634 | struct osmo_sockaddr orig_sgsn_addr; |
| 635 | OSMO_ASSERT(osmo_sockaddr_init(&orig_sgsn_addr, |
| 636 | AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, |
| 637 | "192.168.42.23", 423) == 0); |
| 638 | struct osmo_fd *ggsn_ofd = NULL; |
| 639 | struct osmo_sockaddr ggsn_addr; |
| 640 | int send; |
| 641 | send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_sgsn_addr, |
| 642 | buf, msg(gtp_req_from_sgsn), now, |
| 643 | &ggsn_ofd, &ggsn_addr); |
| 644 | OSMO_ASSERT(send > 0); |
| 645 | OSMO_ASSERT(same_addr(&ggsn_addr, &resolved_ggsn_addr)); |
| 646 | OSMO_ASSERT(msg_is(gtp_req_to_ggsn)); |
| 647 | |
| 648 | struct osmo_fd *sgsn_ofd; |
| 649 | struct osmo_sockaddr sgsn_addr; |
| 650 | send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &ggsn_addr, |
| 651 | buf, msg(gtp_resp_from_ggsn), now, |
| 652 | &sgsn_ofd, &sgsn_addr); |
| 653 | OSMO_ASSERT(send > 0); |
| 654 | OSMO_ASSERT(same_addr(&sgsn_addr, &orig_sgsn_addr)); |
| 655 | OSMO_ASSERT(msg_is(gtp_resp_to_sgsn)); |
| 656 | |
| 657 | struct gtphub_peer_port *ggsn_port = |
| 658 | gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL], |
| 659 | &resolved_ggsn_addr); |
| 660 | OSMO_ASSERT(ggsn_port); |
| 661 | struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer; |
| 662 | /* now == 345; now + 30 == 375. |
| 663 | * seq mapping from above: |
| 664 | * 0xabcd == 43981 (sent in the packet) |
| 665 | * 0x6d31 == 27953 (harcoded seq mapping start val) */ |
| 666 | OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@375), ")); |
| 667 | |
| 668 | /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945. |
| 669 | * 0x00000321 == 801 (TEI from SGSN Ctrl) |
| 670 | * 0x00000123 == 291 (TEI from SGSN User) |
| 671 | * 0x00000765 == 1893 (TEI from GGSN Ctrl) |
| 672 | * 0x00000567 == 1383 (TEI from GGSN User) |
| 673 | * Mapped TEIs should be 1 and 2. */ |
| 674 | OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_CTRL], "(801->1@21945), (1893->2@21945), ")); |
| 675 | OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_USER], "(291->1@21945), (1383->2@21945), ")); |
| 676 | |
| 677 | gtphub_gc(hub, now + EXPIRE_ALL); |
| 678 | } |
| 679 | |
| 680 | static struct log_info_cat gtphub_categories[] = { |
| 681 | [DGTPHUB] = { |
| 682 | .name = "DGTPHUB", |
| 683 | .description = "GTP Hub", |
| 684 | .color = "\033[1;33m", |
| 685 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 686 | }, |
| 687 | }; |
| 688 | |
| 689 | static struct log_info info = { |
| 690 | .cat = gtphub_categories, |
| 691 | .num_cat = ARRAY_SIZE(gtphub_categories), |
| 692 | }; |
| 693 | |
| 694 | int main(int argc, char **argv) |
| 695 | { |
| 696 | osmo_init_logging(&info); |
| 697 | osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub"); |
| 698 | |
| 699 | test_nr_map_basic(); |
| 700 | test_expiry(); |
| 701 | test_echo(); |
| 702 | test_create_pdp_ctx(); |
| 703 | printf("Done\n"); |
| 704 | |
| 705 | talloc_report_full(osmo_gtphub_ctx, stderr); |
| 706 | OSMO_ASSERT(talloc_total_blocks(osmo_gtphub_ctx) == 1); |
| 707 | return 0; |
| 708 | } |
| 709 | |