blob: 7839050cdaf184fbe78b9017188776f325a4cf15 [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];
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100402static uint8_t *reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200403
404static unsigned int msg(const char *hex)
405{
406 unsigned int l = osmo_hexparse(hex, buf, buf_len);
407 OSMO_ASSERT(l > 0);
408 return l;
409}
410
411/* Compare static buf to given string constant. The amount of bytes is obtained
412 * from parsing the GTP header in buf. hex must match an osmo_hexdump() of the
413 * desired message. Return 1 if size and content match. */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100414#define reply_is(MSG) _reply_is(MSG, __FILE__, __LINE__)
415static int _reply_is(const char *hex, const char *file, int line)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200416{
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100417 struct gtp1_header_long *h = (void*)reply_buf;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200418 int len = ntoh16(h->length) + 8;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100419 const char *dump = osmo_hexdump_nospc(reply_buf, len);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200420 int cmp = strcmp(dump, hex);
421
422 if (cmp != 0) {
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100423 printf("\n%s:%d: reply_is(): MISMATCH\n"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200424 " expecting:\n'%s'\n"
425 " got:\n'%s'\n\n",
426 file,
427 line,
428 hex, dump);
429 int i;
430 int l = strlen(hex);
431 int m = strlen(dump);
432 if (m < l)
433 l = m;
434 for (i = 0; i < l; i++) {
435 if (hex[i] != dump[i]) {
436 printf("First mismatch at position %d:\n"
437 " %s\n %s\n", i, hex + i, dump + i);
438 break;
439 }
440 }
441 }
442 return cmp == 0;
443}
444
445#define same_addr(GOT, EXPECTED) _same_addr((GOT),(EXPECTED), __FILE__, __LINE__)
446static int _same_addr(const struct osmo_sockaddr *got,
447 const struct osmo_sockaddr *expected,
448 const char *file, int line)
449{
450 int cmp = osmo_sockaddr_cmp(got, expected);
451 if (!cmp)
452 return 1;
453 char buf[256];
454 printf("\n%s:%d: addr_is(): MISMATCH\n"
455 " expecting: '%s'\n"
456 " got: '%s'\n\n",
457 file, line,
458 osmo_sockaddr_to_str(expected),
459 osmo_sockaddr_to_strb(got, buf, sizeof(buf)));
460 return 0;
461}
462
463static void test_echo(void)
464{
465 struct gtphub _hub;
466 struct gtphub *hub = &_hub;
467 time_t now = 123;
468
469 gtphub_init(hub);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100470 hub->restart_counter = 0x23;
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100471
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200472 const char *gtp_ping_from_sgsn =
473 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */
474 "01" /* type 01: Echo request */
475 "0004" /* length of 4 after header TEI */
476 "00000000" /* header TEI == 0 in Echo */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100477 "abcd" /* some 2 octet sequence nr */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200478 "0000" /* N-PDU 0, no extension header (why is this here?) */
479 ;
480
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100481 const char *gtp_pong_to_sgsn =
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200482 "32"
483 "02" /* type 02: Echo response */
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100484 "0006" /* length of 6 after header TEI */
485 "00000000" /* header TEI == 0 in Echo */
486 "abcd" /* same sequence nr */
487 "0000"
488 "0e23" /* Recovery with restart counter */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200489 ;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200490
491 struct osmo_sockaddr orig_sgsn_addr;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100492 OSMO_ASSERT(osmo_sockaddr_init_udp(&orig_sgsn_addr,
493 "192.168.42.23", 423) == 0);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200494 struct osmo_fd *ggsn_ofd = NULL;
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100495 struct osmo_sockaddr to_addr;
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200496 int send;
497 send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_sgsn_addr,
498 buf, msg(gtp_ping_from_sgsn), now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100499 &reply_buf, &ggsn_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200500 OSMO_ASSERT(send > 0);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100501 OSMO_ASSERT(to_addr.l);
502 OSMO_ASSERT(same_addr(&to_addr, &orig_sgsn_addr));
503 OSMO_ASSERT(reply_is(gtp_pong_to_sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200504
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100505 struct gtphub_peer_port *sgsn_port =
506 gtphub_port_find_sa(&hub->to_sgsns[GTPH_PLANE_CTRL],
507 &orig_sgsn_addr);
508 /* We don't record Echo peers. */
509 OSMO_ASSERT(!sgsn_port);
510
511 const char *gtp_ping_from_ggsn =
512 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */
513 "01" /* type 01: Echo request */
514 "0004" /* length of 4 after header TEI */
515 "00000000" /* header TEI == 0 in Echo */
516 "cdef" /* some 2 octet sequence nr */
517 "0000" /* N-PDU 0, no extension header (why is this here?) */
518 ;
519
520 const char *gtp_pong_to_ggsn =
521 "32"
522 "02" /* type 02: Echo response */
523 "0006" /* length of 6 after header TEI */
524 "00000000" /* header TEI == 0 in Echo */
525 "cdef" /* same sequence nr */
526 "0000"
527 "0e23" /* Recovery with restart counter */
528 ;
529
530 struct osmo_sockaddr orig_ggsn_addr;
531 OSMO_ASSERT(osmo_sockaddr_init_udp(&orig_ggsn_addr,
532 "192.168.24.32", 321) == 0);
533 struct osmo_fd *sgsn_ofd = NULL;
534 send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_ggsn_addr,
535 buf, msg(gtp_ping_from_ggsn), now,
536 &reply_buf, &sgsn_ofd, &to_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200537 OSMO_ASSERT(send > 0);
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100538 OSMO_ASSERT(same_addr(&to_addr, &orig_ggsn_addr));
539 OSMO_ASSERT(reply_is(gtp_pong_to_ggsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200540
541 struct gtphub_peer_port *ggsn_port =
542 gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL],
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100543 &orig_sgsn_addr);
544 /* We don't record Echo peers. */
545 OSMO_ASSERT(!ggsn_port);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200546
547 gtphub_gc(hub, now + EXPIRE_ALL);
548}
549
550static void test_create_pdp_ctx(void)
551{
552 struct gtphub _hub;
553 struct gtphub *hub = &_hub;
554 time_t now = 345;
555
556 gtphub_init(hub);
557
558 /* Tell this mock gtphub its local address for this test. */
559 OSMO_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_CTRL].local_addr,
560 "127.0.1.1") == 0);
561 OSMO_ASSERT(gsn_addr_from_str(&hub->to_sgsns[GTPH_PLANE_USER].local_addr,
562 "127.0.1.2") == 0);
563 OSMO_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_CTRL].local_addr,
564 "127.0.2.1") == 0);
565 OSMO_ASSERT(gsn_addr_from_str(&hub->to_ggsns[GTPH_PLANE_USER].local_addr,
566 "127.0.2.2") == 0);
567
568 /* This is copied from a packet that sgsnemu sends. */
569 const char *gtp_req_from_sgsn =
570 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */
571 "10" /* type 16: Create PDP Context Request */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100572 "0068" /* length = 8 + 104 */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200573 "00000000" /* No TEI yet */
574 "abcd" /* Sequence nr */
575 "00" /* N-PDU 0 */
576 "00" /* No extensions */
577 /* IEs */
578 "02" /* 2 = IMSI */
579 "42000121436587f9"
580 "0e" "60" /* 14: Recovery = 96 */
581 "0f01" /* 15: Selection mode = MS provided APN, subscription not verified*/
582 "10" /* 16: TEI Data I */
583 "00000123"
584 "11" /* 17: TEI Control Plane */
585 "00000321"
586 "1400" /* 20: NSAPI = 0*/
587 "1a" /* 26: Charging Characteristics */
588 "0800"
589 "80" /* 128: End User Address */
590 "0002" /* length = 2: empty PDP Address */
591 "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */
592 "83" /* 131: Access Point Name */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100593 "0009" /* length */
594 "08696e7465726e6574" /* "internet" */
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200595 "84" /* 132: Protocol Configuration Options */
596 "0015" /* length = 21 */
597 "80c0231101010011036d69670868656d6d656c6967"
598 "85" /* 133: GSN Address */
599 "0004" /* length */
600 "abcdef00"
601 "85" /* 133: GSN Address (second entry) */
602 "0004" /* length */
603 "fedcba00"
604 "86" /* 134: MS International PSTN/ISDN Number (MSISDN) */
605 "0007" /* length */
606 "916407123254f6" /* 1946702123456(f) */
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_req_to_ggsn =
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100614 "32" "10" "0068" "00000000"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200615 "6d31" /* mapped seq ("abcd") */
616 "00" "00" "02" "42000121436587f9" "0e60" "0f01"
617 "10" "00000001" /* mapped TEI Data I ("123") */
618 "11" "00000001" /* mapped TEI Control ("321") */
619 "1400" "1a" "0800" "80" "0002" "f121" "83"
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100620 "0009" "08696e7465726e6574" "84" "0015"
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200621 "80c0231101010011036d69670868656d6d656c6967" "85" "0004"
622 "7f000201" /* replaced with gtphub's address ggsn ctrl */
623 "85" "0004"
624 "7f000202" /* replaced with gtphub's address ggsn user */
625 "86" "0007" "916407123254f6"
626 "87" "0004" "00" "0b921f"
627 ;
628
629 const char *gtp_resp_from_ggsn =
630 "32"
631 "11" /* Create PDP Context Response */
632 "004e" /* length = 78 + 8 */
633 "00000001" /* destination TEI (sent in req above) */
634 "6d31" /* mapped seq */
635 "00" "00"
636 /* IEs */
637 "01" /* 1: Cause */
638 "80" /* value = 0b10000000 = response, no rejection. */
639 "08" /* 8: Reordering Required */
640 "00" /* not required. */
641 "0e" "01" /* 14: Recovery = 1 */
642 "10" /* 16: TEI Data I */
643 "00000567"
644 "11" /* 17: TEI Control */
645 "00000765"
646 "7f" /* 127: Charging ID */
647 "00000001"
648 "80" /* 128: End User Address */
649 "0006" /* length = 6 */
650 "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */
651 "7f000002"
652 "84" /* 132: Protocol Configuration Options */
653 "0014" /* len = 20 */
654 "8080211002000010810608080808830600000000"
655 "85" /* 133: GSN Address (Ctrl) */
656 "0004" /* length */
657 "7f000002"
658 "85" /* 133: GSN Address (User) */
659 "0004" /* length */
660 "7f000002"
661 "87" /* 135: Quality of Service (QoS) Profile */
662 "0004" /* length */
663 "00" /* priority */
664 "0b921f" /* QoS profile data */
665 ;
666
667 const char *gtp_resp_to_sgsn =
668 "32" "11" "004e"
669 "00000321" /* unmapped TEI ("001") */
670 "abcd" /* unmapped seq ("6d31") */
671 "00" "00" "01" "80" "08" "00" "0e" "01"
672 "10" "00000002" /* mapped TEI from GGSN ("567") */
673 "11" "00000002" /* mapped TEI from GGSN ("765") */
674 "7f" "00000001" "80" "0006" "f121" "7f000002" "84" "0014"
675 "8080211002000010810608080808830600000000"
676 "85" "0004"
677 "7f000101" /* gtphub's address towards SGSNs (Ctrl) */
678 "85" "0004"
679 "7f000102" /* gtphub's address towards SGSNs (User) */
680 "87" "0004" "00" "0b921f"
681 ;
682
683 /* Set the GGSN address that gtphub is forced to resolve to. */
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100684 const char *resolved_ggsn_str = "192.168.43.34";
685 resolved_ggsn_port = 434;
686 OSMO_ASSERT(gsn_addr_from_str(&resolved_ggsn_addr, resolved_ggsn_str)
687 == 0);
688
689 /* A sockaddr for comparing later */
690 struct osmo_sockaddr resolved_ggsn_sa;
691 OSMO_ASSERT(osmo_sockaddr_init_udp(&resolved_ggsn_sa,
692 resolved_ggsn_str,
693 resolved_ggsn_port)
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200694 == 0);
695
696 struct osmo_sockaddr orig_sgsn_addr;
697 OSMO_ASSERT(osmo_sockaddr_init(&orig_sgsn_addr,
698 AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP,
699 "192.168.42.23", 423) == 0);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100700
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200701 struct osmo_fd *ggsn_ofd = NULL;
702 struct osmo_sockaddr ggsn_addr;
703 int send;
704 send = gtphub_from_sgsns_handle_buf(hub, GTPH_PLANE_CTRL, &orig_sgsn_addr,
705 buf, msg(gtp_req_from_sgsn), now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100706 &reply_buf, &ggsn_ofd, &ggsn_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200707 OSMO_ASSERT(send > 0);
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100708 OSMO_ASSERT(same_addr(&ggsn_addr, &resolved_ggsn_sa));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100709 OSMO_ASSERT(reply_is(gtp_req_to_ggsn));
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100710 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200711
712 struct osmo_fd *sgsn_ofd;
713 struct osmo_sockaddr sgsn_addr;
714 send = gtphub_from_ggsns_handle_buf(hub, GTPH_PLANE_CTRL, &ggsn_addr,
715 buf, msg(gtp_resp_from_ggsn), now,
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100716 &reply_buf, &sgsn_ofd, &sgsn_addr);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200717 OSMO_ASSERT(send > 0);
718 OSMO_ASSERT(same_addr(&sgsn_addr, &orig_sgsn_addr));
Neels Hofmeyrbb3d6782015-11-09 15:12:25 +0100719 OSMO_ASSERT(reply_is(gtp_resp_to_sgsn));
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200720
721 struct gtphub_peer_port *ggsn_port =
722 gtphub_port_find_sa(&hub->to_ggsns[GTPH_PLANE_CTRL],
Neels Hofmeyr30f7bcb2015-11-08 20:34:47 +0100723 &resolved_ggsn_sa);
Neels Hofmeyrc8a614d2015-09-24 17:32:30 +0200724 OSMO_ASSERT(ggsn_port);
725 struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer;
726 /* now == 345; now + 30 == 375.
727 * seq mapping from above:
728 * 0xabcd == 43981 (sent in the packet)
729 * 0x6d31 == 27953 (harcoded seq mapping start val) */
730 OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@375), "));
731
732 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945.
733 * 0x00000321 == 801 (TEI from SGSN Ctrl)
734 * 0x00000123 == 291 (TEI from SGSN User)
735 * 0x00000765 == 1893 (TEI from GGSN Ctrl)
736 * 0x00000567 == 1383 (TEI from GGSN User)
737 * Mapped TEIs should be 1 and 2. */
738 OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_CTRL], "(801->1@21945), (1893->2@21945), "));
739 OSMO_ASSERT(nr_map_is(&hub->tei_map[GTPH_PLANE_USER], "(291->1@21945), (1383->2@21945), "));
740
741 gtphub_gc(hub, now + EXPIRE_ALL);
742}
743
744static struct log_info_cat gtphub_categories[] = {
745 [DGTPHUB] = {
746 .name = "DGTPHUB",
747 .description = "GTP Hub",
748 .color = "\033[1;33m",
749 .enabled = 1, .loglevel = LOGL_NOTICE,
750 },
751};
752
753static struct log_info info = {
754 .cat = gtphub_categories,
755 .num_cat = ARRAY_SIZE(gtphub_categories),
756};
757
758int main(int argc, char **argv)
759{
760 osmo_init_logging(&info);
761 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
762
763 test_nr_map_basic();
764 test_expiry();
765 test_echo();
766 test_create_pdp_ctx();
767 printf("Done\n");
768
769 talloc_report_full(osmo_gtphub_ctx, stderr);
770 OSMO_ASSERT(talloc_total_blocks(osmo_gtphub_ctx) == 1);
771 return 0;
772}
773