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