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