blob: dab39775b216e34e8a43fb6540fc0c58ee7157d7 [file] [log] [blame]
Neels Hofmeyr9f796642015-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>
Neels Hofmeyr9f796642015-09-24 17:32:30 +020029#include <osmocom/core/application.h>
30
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020031#include <osmocom/sgsn/debug.h>
Neels Hofmeyr9f796642015-09-24 17:32:30 +020032
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020033#include <osmocom/sgsn/gtphub.h>
Neels Hofmeyr9f796642015-09-24 17:32:30 +020034#include <gtp.h>
35#include <gtpie.h>
36
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +010037#define ZERO_STRUCT(struct_pointer) memset(struct_pointer, '\0', \
38 sizeof(*(struct_pointer)))
Neels Hofmeyr43283a32015-11-10 22:07:04 +010039
40#define LVL2_ASSERT(exp) LVL2_ASSERT_R(exp, return 0)
41#define LVL2_ASSERT_R(exp, ret) \
42 if (!(exp)) { \
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +010043 fprintf(stderr, "LVL2 Assert failed %s %s:%d\n", #exp, \
44 __FILE__, __LINE__); \
Neels Hofmeyr43283a32015-11-10 22:07:04 +010045 osmo_generate_backtrace(); \
46 ret; \
47 }
48
Neels Hofmeyre6078bc2015-11-11 00:45:50 +010049/* Convenience makro, note: only within this C file. */
50#define LOG(label) \
Neels Hofmeyr39da7112015-11-24 13:31:06 +010051 { fprintf(stderr, "\n" label "\n"); \
Neels Hofmeyre6078bc2015-11-11 00:45:50 +010052 printf(label "\n"); }
53
Neels Hofmeyr9f796642015-09-24 17:32:30 +020054void gtphub_init(struct gtphub *hub);
Neels Hofmeyr21d08732015-11-20 00:08:28 +010055void gtphub_free(struct gtphub *hub);
Neels Hofmeyr9f796642015-09-24 17:32:30 +020056
Harald Welte2a968312020-04-20 19:47:19 +020057extern void *osmo_gtphub_ctx;
Neels Hofmeyr9f796642015-09-24 17:32:30 +020058
Neels Hofmeyr9f796642015-09-24 17:32:30 +020059static void nr_mapping_free(struct expiring_item *e)
60{
61 struct nr_mapping *m = container_of(e, struct nr_mapping,
62 expiry_entry);
63 nr_mapping_del(m);
64 talloc_free(m);
65}
66
67static struct nr_mapping *nr_mapping_alloc(void)
68{
69 struct nr_mapping *m;
70 m = talloc(osmo_gtphub_ctx, struct nr_mapping);
71 nr_mapping_init(m);
72 m->expiry_entry.del_cb = nr_mapping_free;
73 return m;
74}
75
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +010076static struct nr_mapping *nr_map_have(struct nr_map *map, void *origin,
77 nr_t orig, time_t now)
Neels Hofmeyr9f796642015-09-24 17:32:30 +020078{
79 struct nr_mapping *mapping;
80
81 mapping = nr_map_get(map, origin, orig);
82 if (!mapping) {
83 mapping = nr_mapping_alloc();
84 mapping->origin = origin;
85 mapping->orig = orig;
86 nr_map_add(map, mapping, now);
87 }
88
89 return mapping;
90}
91
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +010092static nr_t nr_map_verify(const struct nr_map *map, void *origin, nr_t orig,
93 nr_t expect_repl)
Neels Hofmeyr9f796642015-09-24 17:32:30 +020094{
95 struct nr_mapping *m;
96 m = nr_map_get(map, origin, orig);
97
98 if (!m) {
99 printf("mapping not found for %p %d\n", origin, orig);
100 return 0;
101 }
102
103 if (m->repl != expect_repl) {
104 printf("mapping found, but nr mismatches: expect %d, got %d\n",
105 (int)expect_repl, (int)m->repl);
106 return 0;
107 }
108
109 return 1;
110}
111
112static int nr_map_verify_inv(const struct nr_map *map, nr_t repl,
113 void *expect_origin, nr_t expect_orig)
114{
115 struct nr_mapping *m;
116 m = nr_map_get_inv(map, repl);
117 if (!m) {
118 printf("mapping not found for %d\n", (int)repl);
119 return 0;
120 }
121
122 if (m->origin != expect_origin) {
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100123 printf("mapping found, but origin mismatches:"
124 " expect %p, got %p\n",
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200125 expect_origin, m->origin);
126 return 0;
127 }
128
129 if (m->orig != expect_orig) {
130 printf("mapping found, but nr mismatches: expect %d, got %d\n",
131 (int)expect_orig, (int)m->orig);
132 return 0;
133 }
134
135 return 1;
136}
137
138
139static void test_nr_map_basic(void)
140{
141 struct nr_pool _pool;
142 struct nr_pool *pool = &_pool;
143 struct nr_map _map;
144 struct nr_map *map = &_map;
145
Neels Hofmeyr6a65a8f2015-11-17 14:30:37 +0100146 nr_pool_init(pool, 1, 1000);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200147 nr_map_init(map, pool, NULL);
148
149 OSMO_ASSERT(llist_empty(&map->mappings));
150
151#define TEST_N_HALF 100
152#define TEST_N (2*TEST_N_HALF)
153#define TEST_I 123
154 uint32_t i, check_i;
155 uint32_t m[TEST_N];
156 struct nr_mapping *mapping;
157
158 /* create half of TEST_N mappings from one origin */
159 void *origin1 = (void*)0x1234;
160 for (i = 0; i < TEST_N_HALF; i++) {
161 nr_t orig = TEST_I + i;
162 mapping = nr_map_have(map, origin1, orig, 0);
163 m[i] = mapping->repl;
164 OSMO_ASSERT(m[i] != 0);
Neels Hofmeyr56330132017-01-11 00:43:26 +0100165 OSMO_ASSERT(llist_count(&map->mappings) == (i+1));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200166 for (check_i = 0; check_i < i; check_i++)
167 OSMO_ASSERT(m[check_i] != m[i]);
168 }
Neels Hofmeyr56330132017-01-11 00:43:26 +0100169 OSMO_ASSERT(llist_count(&map->mappings) == TEST_N_HALF);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200170
171 /* create another TEST_N mappings with the same original numbers, but
172 * from a different origin */
173 void *origin2 = (void*)0x5678;
174 for (i = 0; i < TEST_N_HALF; i++) {
175 int i2 = TEST_N_HALF + i;
176 nr_t orig = TEST_I + i;
177 mapping = nr_map_have(map, origin2, orig, 0);
178 m[i2] = mapping->repl;
179 OSMO_ASSERT(m[i2] != 0);
Neels Hofmeyr56330132017-01-11 00:43:26 +0100180 OSMO_ASSERT(llist_count(&map->mappings) == (i2+1));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200181 for (check_i = 0; check_i < i2; check_i++)
182 OSMO_ASSERT(m[check_i] != m[i2]);
183 }
Neels Hofmeyr56330132017-01-11 00:43:26 +0100184 OSMO_ASSERT(llist_count(&map->mappings) == TEST_N);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200185
186 /* verify mappings */
187 for (i = 0; i < TEST_N_HALF; i++) {
188 nr_t orig = TEST_I + i;
189 {
190 OSMO_ASSERT(nr_map_verify(map, origin1, orig, m[i]));
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100191 OSMO_ASSERT(nr_map_verify_inv(map, m[i], origin1,
192 orig));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200193 }
194 {
195 int i2 = TEST_N_HALF + i;
196 OSMO_ASSERT(nr_map_verify(map, origin2, orig, m[i2]));
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100197 OSMO_ASSERT(nr_map_verify_inv(map, m[i2], origin2,
198 orig));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200199 }
200 }
201
202 /* remove all mappings */
203 for (i = 0; i < TEST_N_HALF; i++) {
Neels Hofmeyr56330132017-01-11 00:43:26 +0100204 OSMO_ASSERT(llist_count(&map->mappings) == (TEST_N - 2*i));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200205
206 nr_t orig = TEST_I + i;
207 nr_mapping_del(nr_map_get(map, origin1, orig));
208 nr_mapping_del(nr_map_get(map, origin2, orig));
209 }
210 OSMO_ASSERT(llist_empty(&map->mappings));
211#undef TEST_N
212#undef TEST_I
213}
214
215static int nr_map_is(struct nr_map *map, const char *str)
216{
217 static char buf[4096];
218 char *pos = buf;
219 size_t len = sizeof(buf);
220 struct nr_mapping *m;
221 llist_for_each_entry(m, &map->mappings, entry) {
Neels Hofmeyr767804d2015-11-17 14:24:46 +0100222 size_t wrote = snprintf(pos, len, "(%u->%u@%d), ",
223 m->orig,
224 m->repl,
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200225 (int)m->expiry_entry.expiry);
226 OSMO_ASSERT(wrote < len);
227 pos += wrote;
228 len -= wrote;
229 }
230 *pos = '\0';
231
232 if (strncmp(buf, str, sizeof(buf)) != 0) {
233 printf("FAILURE: nr_map_is() mismatches expected value:\n"
234 "expected: \"%s\"\n"
235 "is: \"%s\"\n",
236 str, buf);
237 return 0;
238 }
239 return 1;
240}
241
Neels Hofmeyr6a65a8f2015-11-17 14:30:37 +0100242static int test_nr_map_wrap_with(nr_t nr_min, nr_t nr_max, nr_t repl_last,
243 nr_t orig_start, int orig_n,
244 const char *expect)
245{
246 struct nr_pool _pool;
247 struct nr_pool *pool = &_pool;
248 struct nr_map _map;
249 struct nr_map *map = &_map;
250
251 nr_pool_init(pool, nr_min, nr_max);
252 nr_map_init(map, pool, NULL);
253
254 pool->last_nr = repl_last;
255
256 void *origin = (void*)0x1234;
257
258 int i;
259 for (i = 0; i < orig_n; i++)
260 LVL2_ASSERT(nr_map_have(map, origin, orig_start + i, 0));
261
262 LVL2_ASSERT(nr_map_is(map, expect));
263
264 nr_map_clear(map);
265 return 1;
266}
267
268static void test_nr_map_wrap(void)
269{
270 OSMO_ASSERT(test_nr_map_wrap_with(
271 0, UINT_MAX, UINT_MAX - 2,
272 1, 5,
273 "(1->4294967294@0), "
274 "(2->4294967295@0), "
275 "(3->0@0), "
276 "(4->1@0), "
277 "(5->2@0), "
278 ));
279 OSMO_ASSERT(test_nr_map_wrap_with(
280 5, 10, 8,
281 1, 5,
282 "(1->9@0), (2->10@0), (3->5@0), (4->6@0), (5->7@0), "
283 ));
284}
285
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200286static void test_expiry(void)
287{
288 struct expiry expiry;
289 struct nr_pool pool;
290 struct nr_map map;
291 int i;
292
293 expiry_init(&expiry, 30);
Neels Hofmeyr6a65a8f2015-11-17 14:30:37 +0100294 nr_pool_init(&pool, 1, 1000);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200295 nr_map_init(&map, &pool, &expiry);
296 OSMO_ASSERT(nr_map_is(&map, ""));
297
298 /* tick on empty map */
299 OSMO_ASSERT(expiry_tick(&expiry, 10000) == 0);
300 OSMO_ASSERT(nr_map_is(&map, ""));
301
302#define MAP1 \
303 "(10->1@10040), " \
304 ""
305
306#define MAP2 \
307 "(20->2@10050), " \
308 "(21->3@10051), " \
309 "(22->4@10052), " \
310 "(23->5@10053), " \
311 "(24->6@10054), " \
312 "(25->7@10055), " \
313 "(26->8@10056), " \
314 "(27->9@10057), " \
315 ""
316
317#define MAP3 \
318 "(420->10@10072), " \
319 "(421->11@10072), " \
320 "(422->12@10072), " \
321 "(423->13@10072), " \
322 "(424->14@10072), " \
323 "(425->15@10072), " \
324 "(426->16@10072), " \
325 "(427->17@10072), " \
326 ""
327
328 /* add mapping at time 10010. */
329 nr_map_have(&map, 0, 10, 10010);
330 OSMO_ASSERT(nr_map_is(&map, MAP1));
331
332 /* tick on unexpired item. */
333 OSMO_ASSERT(expiry_tick(&expiry, 10010) == 0);
334 OSMO_ASSERT(expiry_tick(&expiry, 10011) == 0);
335 OSMO_ASSERT(nr_map_is(&map, MAP1));
336
337 /* Spread mappings at 10020, 10021, ... 10027. */
338 for (i = 0; i < 8; i++)
339 nr_map_have(&map, 0, 20 + i, 10020 + i);
340 OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2));
341
342 /* tick on unexpired items. */
343 OSMO_ASSERT(expiry_tick(&expiry, 10030) == 0);
344 OSMO_ASSERT(expiry_tick(&expiry, 10039) == 0);
345 OSMO_ASSERT(nr_map_is(&map, MAP1 MAP2));
346
347 /* expire the first item (from 10010). */
348 OSMO_ASSERT(expiry_tick(&expiry, 10010 + 30) == 1);
349 OSMO_ASSERT(nr_map_is(&map, MAP2));
350
351 /* again nothing to expire */
352 OSMO_ASSERT(expiry_tick(&expiry, 10041) == 0);
353 OSMO_ASSERT(nr_map_is(&map, MAP2));
354
355 /* Mappings all at the same time. */
356 for (i = 0; i < 8; i++)
357 nr_map_have(&map, 0, 420 + i, 10042);
358 OSMO_ASSERT(nr_map_is(&map, MAP2 MAP3));
359
360 /* Eight to expire, were added further above to be chronologically
361 * correct, at 10020..10027. */
362 OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 8);
363 OSMO_ASSERT(nr_map_is(&map, MAP3));
364
365 /* again nothing to expire */
366 OSMO_ASSERT(expiry_tick(&expiry, 10027 + 30) == 0);
367 OSMO_ASSERT(nr_map_is(&map, MAP3));
368
369 /* Eight to expire, from 10042. Now at 10042 + 30: */
370 OSMO_ASSERT(expiry_tick(&expiry, 10042 + 30) == 8);
371 OSMO_ASSERT(nr_map_is(&map, ""));
372
373#undef MAP1
374#undef MAP2
375#undef MAP3
376}
377
Harald Welte02884f22016-04-20 17:50:17 +0200378char resolve_ggsn_got_imsi[GSM23003_IMSI_MAX_DIGITS+1];
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100379char resolve_ggsn_got_ni[GSM_APN_LENGTH];
380
Alexander Couzens323aaf62020-07-18 16:45:25 +0200381struct sgsn_sockaddr resolved_ggsn_addr;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100382static int resolve_to_ggsn(const char *addr, uint16_t port)
383{
Alexander Couzens323aaf62020-07-18 16:45:25 +0200384 LVL2_ASSERT(sgsn_sockaddr_init_udp(&resolved_ggsn_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100385 addr, port)
386 == 0);
387 return 1;
388}
389
Alexander Couzens323aaf62020-07-18 16:45:25 +0200390struct sgsn_sockaddr resolved_sgsn_addr;
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100391static int resolve_to_sgsn(const char *addr, uint16_t port)
392{
Alexander Couzens323aaf62020-07-18 16:45:25 +0200393 LVL2_ASSERT(sgsn_sockaddr_init_udp(&resolved_sgsn_addr,
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100394 addr, port)
395 == 0);
396 return 1;
397}
398
Alexander Couzens323aaf62020-07-18 16:45:25 +0200399struct sgsn_sockaddr sgsn_sender;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100400static int send_from_sgsn(const char *addr, uint16_t port)
401{
Alexander Couzens323aaf62020-07-18 16:45:25 +0200402 LVL2_ASSERT(sgsn_sockaddr_init_udp(&sgsn_sender,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100403 addr, port)
404 == 0);
405 return 1;
406}
407
Alexander Couzens323aaf62020-07-18 16:45:25 +0200408struct sgsn_sockaddr ggsn_sender;
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100409static int send_from_ggsn(const char *addr, uint16_t port)
410{
Alexander Couzens323aaf62020-07-18 16:45:25 +0200411 LVL2_ASSERT(sgsn_sockaddr_init_udp(&ggsn_sender,
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100412 addr, port)
413 == 0);
414 return 1;
415}
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200416
417
418/* override, requires '-Wl,--wrap=gtphub_resolve_ggsn_addr' */
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100419struct gtphub_peer_port *__real_gtphub_resolve_ggsn_addr(struct gtphub *hub,
420 const char *imsi_str,
421 const char *apn_ni_str);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200422
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100423struct gtphub_peer_port *__wrap_gtphub_resolve_ggsn_addr(struct gtphub *hub,
424 const char *imsi_str,
425 const char *apn_ni_str)
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200426{
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100427 struct gsn_addr resolved_gsna;
428 uint16_t resolved_port;
429
430 OSMO_ASSERT(gsn_addr_from_sockaddr(&resolved_gsna, &resolved_port,
431 &resolved_ggsn_addr) == 0);
432
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100433 struct gtphub_peer_port *pp;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100434 pp = gtphub_port_have(hub, &hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100435 &resolved_gsna, resolved_port);
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100436 printf("- __wrap_gtphub_resolve_ggsn_addr():\n"
437 " returning GGSN addr from imsi %s ni %s: %s\n",
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100438 imsi_str, apn_ni_str, gtphub_port_str(pp));
439
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +0100440 if (!imsi_str)
441 imsi_str = "(null)";
442 osmo_strlcpy(resolve_ggsn_got_imsi, imsi_str,
443 sizeof(resolve_ggsn_got_imsi));
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100444
Neels Hofmeyr5644c2b2017-01-13 03:12:08 +0100445 if (!apn_ni_str)
446 apn_ni_str = "(null)";
447 osmo_strlcpy(resolve_ggsn_got_ni, apn_ni_str,
448 sizeof(resolve_ggsn_got_ni));
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100449
450 return pp;
451}
452
453#define was_resolved_for(IMSI,NI) _was_resolved_for(IMSI, NI, __FILE__, __LINE__)
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100454static int _was_resolved_for(const char *imsi, const char *ni, const char
455 *file, int line)
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100456{
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100457 int cmp0 = strncmp(imsi, resolve_ggsn_got_imsi,
458 sizeof(resolve_ggsn_got_imsi));
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100459
460 if (cmp0 != 0) {
461 printf("\n%s:%d: was_resolved_for(): MISMATCH for IMSI\n"
462 " expecting: '%s'\n"
463 " got: '%s'\n\n",
464 file,
465 line,
466 imsi, resolve_ggsn_got_imsi);
467 }
468
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100469 int cmp1 = strncmp(ni, resolve_ggsn_got_ni,
470 sizeof(resolve_ggsn_got_ni));
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100471 if (cmp1 != 0) {
472 printf("\n%s:%d: was_resolved_for(): MISMATCH for NI\n"
473 " expecting: '%s'\n"
474 " got: '%s'\n\n",
475 file,
476 line,
477 ni, resolve_ggsn_got_ni);
478 }
479
480 return (cmp0 == 0) && (cmp1 == 0);
481}
482
483/* override, requires '-Wl,--wrap=gtphub_ares_init' */
484int __real_gtphub_ares_init(struct gtphub *hub);
485
486int __wrap_gtphub_ares_init(struct gtphub *hub)
487{
488 /* Do nothing. */
489 return 0;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200490}
491
Neels Hofmeyrf6fe2122015-12-02 15:43:10 +0100492/* override, requires '-Wl,--wrap=gtphub_write' */
493int __real_gtphub_write(const struct osmo_fd *to,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200494 const struct sgsn_sockaddr *to_addr,
Neels Hofmeyrf6fe2122015-12-02 15:43:10 +0100495 const uint8_t *buf, size_t buf_len);
496
497int __wrap_gtphub_write(const struct osmo_fd *to,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200498 const struct sgsn_sockaddr *to_addr,
Neels Hofmeyrf6fe2122015-12-02 15:43:10 +0100499 const uint8_t *buf, size_t buf_len)
500{
501 printf("Out-of-band gtphub_write(%d):\n"
502 "to %s\n"
503 "%s\n",
504 (int)buf_len,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200505 sgsn_sockaddr_to_str(to_addr),
Neels Hofmeyrf6fe2122015-12-02 15:43:10 +0100506 osmo_hexdump(buf, buf_len));
507 return 0;
508}
509
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200510#define buf_len 1024
511static uint8_t buf[buf_len];
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100512static uint8_t *reply_buf;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200513
514static unsigned int msg(const char *hex)
515{
516 unsigned int l = osmo_hexparse(hex, buf, buf_len);
517 OSMO_ASSERT(l > 0);
518 return l;
519}
520
521/* Compare static buf to given string constant. The amount of bytes is obtained
522 * from parsing the GTP header in buf. hex must match an osmo_hexdump() of the
523 * desired message. Return 1 if size and content match. */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100524#define reply_is(MSG) _reply_is(MSG, __FILE__, __LINE__)
525static int _reply_is(const char *hex, const char *file, int line)
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200526{
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100527 struct gtp1_header_long *h = (void*)reply_buf;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200528 int len = ntoh16(h->length) + 8;
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100529 const char *dump = osmo_hexdump_nospc(reply_buf, len);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200530 int cmp = strcmp(dump, hex);
531
Alexander Couzens016a68b2020-10-02 00:13:42 +0200532 OSMO_ASSERT(dump && hex);
533
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200534 if (cmp != 0) {
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100535 printf("\n%s:%d: reply_is(): MISMATCH\n"
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200536 " expecting:\n'%s'\n"
537 " got:\n'%s'\n\n",
538 file,
539 line,
540 hex, dump);
541 int i;
542 int l = strlen(hex);
543 int m = strlen(dump);
544 if (m < l)
545 l = m;
546 for (i = 0; i < l; i++) {
547 if (hex[i] != dump[i]) {
548 printf("First mismatch at position %d:\n"
549 " %s\n %s\n", i, hex + i, dump + i);
550 break;
551 }
552 }
553 }
554 return cmp == 0;
555}
556
557#define same_addr(GOT, EXPECTED) _same_addr((GOT),(EXPECTED), __FILE__, __LINE__)
Alexander Couzens323aaf62020-07-18 16:45:25 +0200558static int _same_addr(const struct sgsn_sockaddr *got,
559 const struct sgsn_sockaddr *expected,
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200560 const char *file, int line)
561{
Alexander Couzens323aaf62020-07-18 16:45:25 +0200562 int cmp = sgsn_sockaddr_cmp(got, expected);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200563 if (!cmp)
564 return 1;
565 char buf[256];
566 printf("\n%s:%d: addr_is(): MISMATCH\n"
567 " expecting: '%s'\n"
568 " got: '%s'\n\n",
569 file, line,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200570 sgsn_sockaddr_to_str(expected),
571 sgsn_sockaddr_to_strb(got, buf, sizeof(buf)));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200572 return 0;
573}
574
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100575
576time_t now;
577static struct gtphub _hub;
578static struct gtphub *hub = &_hub;
579
580static int setup_test_hub()
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200581{
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100582 /* Not really needed, but to make 100% sure... */
583 ZERO_STRUCT(hub);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200584
585 gtphub_init(hub);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100586
587 /* Tell this mock gtphub its local address for this test. */
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100588 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100589 "127.0.1.1") == 0);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100590 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100591 "127.0.1.2") == 0);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100592 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100593 "127.0.2.1") == 0);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100594 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100595 "127.0.2.2") == 0);
596
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100597 hub->restart_counter = 0x23;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100598 now = 345;
599 LVL2_ASSERT(send_from_sgsn("192.168.42.23", 423));
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100600 LVL2_ASSERT(resolve_to_ggsn("192.168.43.34", 2123));
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100601 LVL2_ASSERT(send_from_ggsn("192.168.43.34", 434));
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100602 LVL2_ASSERT(resolve_to_sgsn("192.168.42.23", 2123));
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100603
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100604#define GGSNS_CTRL_FD 1
605#define GGSNS_USER_FD 2
606#define SGSNS_CTRL_FD 3
607#define SGSNS_USER_FD 4
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100608 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].ofd.priv_nr = GGSNS_CTRL_FD;
609 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].ofd.priv_nr = GGSNS_USER_FD;
610 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].ofd.priv_nr = SGSNS_CTRL_FD;
611 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].ofd.priv_nr = SGSNS_USER_FD;
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100612
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100613 return 1;
614}
615
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100616static int clear_test_hub()
617{
618 /* expire all */
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100619 gtphub_gc(hub, now + (60 * GTPH_EXPIRE_SLOWLY_MINUTES) + 1);
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100620
621 int plane_idx;
622 plane_idx = GTPH_PLANE_CTRL;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100623 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_GGSN][plane_idx].peers));
624 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_SGSN][plane_idx].peers));
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100625 plane_idx = GTPH_PLANE_USER;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100626 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_GGSN][plane_idx].peers));
627 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_SGSN][plane_idx].peers));
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100628
Neels Hofmeyr0fa507a2015-12-02 01:15:30 +0100629 LVL2_ASSERT(llist_empty(&hub->tunnels));
630 LVL2_ASSERT(llist_empty(&hub->pending_deletes));
631 LVL2_ASSERT(llist_empty(&hub->ggsn_lookups));
632 LVL2_ASSERT(llist_empty(&hub->resolved_ggsns));
633
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100634 gtphub_free(hub);
635 return 1;
636}
637
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100638static int tunnels_are(const char *expect)
639{
640 static char buf[4096];
641 char *pos = buf;
642 size_t len = sizeof(buf);
643 struct gtphub_tunnel *t;
644 llist_for_each_entry(t, &hub->tunnels, entry) {
645 size_t wrote = snprintf(pos, len, "%s @%d\n",
646 gtphub_tunnel_str(t),
647 (int)t->expiry_entry.expiry);
648 LVL2_ASSERT(wrote < len);
649 pos += wrote;
650 len -= wrote;
651 }
652 *pos = '\0';
653
654 if (strncmp(buf, expect, sizeof(buf)) != 0) {
655 fprintf(stderr, "FAILURE: tunnels_are() mismatches expected value:\n"
656 "EXPECTED:\n%s\n"
657 "IS:\n%s\n",
658 expect, buf);
659 LVL2_ASSERT("tunnels do not match expected listing.");
660 return 0;
661 }
662 return 1;
663}
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100664
665static void test_echo(void)
666{
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100667 LOG("test_echo");
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100668 OSMO_ASSERT(setup_test_hub());
669
670 now = 123;
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100671
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100672 struct osmo_fd *to_ofd;
Alexander Couzens323aaf62020-07-18 16:45:25 +0200673 struct sgsn_sockaddr to_addr;
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100674 struct gtphub_peer_port *pp;
675 int send;
676
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200677 const char *gtp_ping_from_sgsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100678 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200679 "01" /* type 01: Echo request */
680 "0004" /* length of 4 after header TEI */
681 "00000000" /* header TEI == 0 in Echo */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100682 "abcd" /* some 2 octet sequence nr */
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200683 "0000" /* N-PDU 0, no extension header (why is this here?) */
684 ;
685
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100686 const char *gtp_pong_to_sgsn =
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200687 "32"
688 "02" /* type 02: Echo response */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100689 "0006" /* length of 6 after header TEI */
690 "00000000" /* header TEI == 0 in Echo */
691 "abcd" /* same sequence nr */
692 "0000"
693 "0e23" /* Recovery with restart counter */
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200694 ;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200695
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100696 to_ofd = NULL;
697 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100698 send = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, GTPH_PLANE_CTRL,
699 &sgsn_sender, buf, msg(gtp_ping_from_sgsn),
700 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200701 OSMO_ASSERT(send > 0);
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100702 OSMO_ASSERT(to_addr.l);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100703 OSMO_ASSERT(same_addr(&to_addr, &sgsn_sender));
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100704 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == SGSNS_CTRL_FD));
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100705 OSMO_ASSERT(reply_is(gtp_pong_to_sgsn));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200706
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100707 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100708 &sgsn_sender);
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100709 /* We don't record Echo peers. */
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100710 OSMO_ASSERT(!pp);
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100711
712 const char *gtp_ping_from_ggsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100713 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100714 "01" /* type 01: Echo request */
715 "0004" /* length of 4 after header TEI */
716 "00000000" /* header TEI == 0 in Echo */
717 "cdef" /* some 2 octet sequence nr */
718 "0000" /* N-PDU 0, no extension header (why is this here?) */
719 ;
720
721 const char *gtp_pong_to_ggsn =
722 "32"
723 "02" /* type 02: Echo response */
724 "0006" /* length of 6 after header TEI */
725 "00000000" /* header TEI == 0 in Echo */
726 "cdef" /* same sequence nr */
727 "0000"
728 "0e23" /* Recovery with restart counter */
729 ;
730
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100731 to_ofd = NULL;
732 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100733 send = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, GTPH_PLANE_CTRL,
734 &ggsn_sender, buf, msg(gtp_ping_from_ggsn),
735 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200736 OSMO_ASSERT(send > 0);
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100737 OSMO_ASSERT(same_addr(&to_addr, &ggsn_sender));
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100738 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == GGSNS_CTRL_FD));
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100739 OSMO_ASSERT(reply_is(gtp_pong_to_ggsn));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200740
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100741 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100742 &sgsn_sender);
743 OSMO_ASSERT(!pp);
744
745
746 /* And all the same on the user plane. */
747
748 to_ofd = NULL;
749 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100750 send = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, GTPH_PLANE_USER,
751 &sgsn_sender, buf, msg(gtp_ping_from_sgsn),
752 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100753 OSMO_ASSERT(send > 0);
754 OSMO_ASSERT(to_addr.l);
755 OSMO_ASSERT(same_addr(&to_addr, &sgsn_sender));
756 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == SGSNS_USER_FD));
757 OSMO_ASSERT(reply_is(gtp_pong_to_sgsn));
758
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100759 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100760 &sgsn_sender);
761 OSMO_ASSERT(!pp);
762
763 to_ofd = NULL;
764 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100765 send = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, GTPH_PLANE_USER,
766 &ggsn_sender, buf, msg(gtp_ping_from_ggsn),
767 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100768 OSMO_ASSERT(send > 0);
769 OSMO_ASSERT(same_addr(&to_addr, &ggsn_sender));
770 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == GGSNS_USER_FD));
771 OSMO_ASSERT(reply_is(gtp_pong_to_ggsn));
772
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100773 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100774 &sgsn_sender);
775 OSMO_ASSERT(!pp);
776
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200777
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100778 OSMO_ASSERT(clear_test_hub());
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100779}
780
781
782#define MSG_PDP_CTX_REQ(len, seq, restart, imsi, tei_u, tei_c, apn, gsn_c, gsn_u) \
783 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \
784 "10" /* type 16: Create PDP Context Request */ \
785 len /* msg length = 8 + len (2 octets) */ \
786 "00000000" /* No TEI yet */ \
787 seq /* Sequence nr (2 octets) */ \
788 "00" /* N-PDU 0 */ \
789 "00" /* No extensions */ \
790 /* IEs */ \
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100791 "0e" restart /* 14: Recovery (restart counter: 1 octet) */ \
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100792 "02" /* 2 = IMSI */ \
793 imsi /* (8 octets) */ \
794 "0f01" /* 15: Selection mode = MS provided APN, subscription not verified*/ \
795 "10" /* 16: TEI Data I */ \
796 tei_u /* (4 octets) */ \
797 "11" /* 17: TEI Control Plane */ \
798 tei_c /* (4 octets) */ \
799 "1400" /* 20: NSAPI = 0*/ \
800 "1a" /* 26: Charging Characteristics */ \
801 "0800" \
802 "80" /* 128: End User Address */ \
803 "0002" /* length = 2: empty PDP Address */ \
804 "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ \
805 "83" /* 131: Access Point Name */ \
806 apn /* (2 octets length, N octets encoded APN-NI) */ \
807 "84" /* 132: Protocol Configuration Options */ \
808 "0015" /* length = 21 */ \
809 "80c0231101010011036d69670868656d6d656c6967" \
810 "85" /* 133: GSN Address */ \
811 gsn_c /* (2 octets length, N octets addr) */ \
812 "85" /* 133: GSN Address (second entry) */ \
813 gsn_u /* (2 octets length, N octets addr) */ \
814 "86" /* 134: MS International PSTN/ISDN Number (MSISDN) */ \
815 "0007" /* length */ \
816 "916407123254f6" /* 1946702123456(f) */ \
817 "87" /* 135: Quality of Service (QoS) Profile */ \
818 "0004" /* length */ \
819 "00" /* priority */ \
820 "0b921f" /* QoS profile data */
821
822#define MSG_PDP_CTX_RSP(len, tei_h, seq, restart, tei_u, tei_c, gsn_c, gsn_u) \
823 "32" \
824 "11" /* Create PDP Context Response */ \
825 len /* msg length = 8 + len (2 octets) */ \
826 tei_h /* destination TEI (sent in req above) */ \
827 seq /* mapped seq */ \
828 "00" "00" \
829 /* IEs */ \
830 "01" /* 1: Cause */ \
831 "80" /* value = 0b10000000 = response, no rejection. */ \
832 "08" /* 8: Reordering Required */ \
833 "00" /* not required. */ \
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100834 "0e" restart /* 14: Recovery */ \
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100835 "10" /* 16: TEI Data I */ \
836 tei_u \
837 "11" /* 17: TEI Control */ \
838 tei_c \
839 "7f" /* 127: Charging ID */ \
840 "00000001" \
841 "80" /* 128: End User Address */ \
842 "0006" /* length = 6 */ \
843 "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ \
844 "7f000002" \
845 "84" /* 132: Protocol Configuration Options */ \
846 "0014" /* len = 20 */ \
847 "8080211002000010810608080808830600000000" \
848 "85" /* 133: GSN Address (Ctrl) */ \
849 gsn_c \
850 "85" /* 133: GSN Address (User) */ \
851 gsn_u \
852 "87" /* 135: Quality of Service (QoS) Profile */ \
853 "0004" /* length */ \
854 "00" /* priority */ \
855 "0b921f" /* QoS profile data */
856
857#define msg_from_sgsn_c(A,B,C,D) msg_from_sgsn(GTPH_PLANE_CTRL, A,B,C,D)
858#define msg_from_sgsn_u(A,B,C,D) msg_from_sgsn(GTPH_PLANE_USER, A,B,C,D)
859static int msg_from_sgsn(int plane_idx,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200860 struct sgsn_sockaddr *_sgsn_sender,
861 struct sgsn_sockaddr *ggsn_receiver,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100862 const char *hex_from_sgsn,
863 const char *hex_to_ggsn)
864{
865 struct osmo_fd *ggsn_ofd = NULL;
Alexander Couzens323aaf62020-07-18 16:45:25 +0200866 struct sgsn_sockaddr ggsn_addr;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100867 int send;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100868 send = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, _sgsn_sender,
869 buf, msg(hex_from_sgsn), now,
870 &reply_buf, &ggsn_ofd, &ggsn_addr);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100871 LVL2_ASSERT(send > 0);
872 LVL2_ASSERT(same_addr(&ggsn_addr, ggsn_receiver));
873 LVL2_ASSERT(reply_is(hex_to_ggsn));
874 return 1;
875}
876
877#define msg_from_ggsn_c(A,B,C,D) msg_from_ggsn(GTPH_PLANE_CTRL, A,B,C,D)
878#define msg_from_ggsn_u(A,B,C,D) msg_from_ggsn(GTPH_PLANE_USER, A,B,C,D)
879static int msg_from_ggsn(int plane_idx,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200880 struct sgsn_sockaddr *ggsn_sender,
881 struct sgsn_sockaddr *sgsn_receiver,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100882 const char *msg_from_ggsn,
883 const char *msg_to_sgsn)
884{
885 struct osmo_fd *sgsn_ofd;
Alexander Couzens323aaf62020-07-18 16:45:25 +0200886 struct sgsn_sockaddr sgsn_addr;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100887 int send;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100888 send = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, ggsn_sender,
889 buf, msg(msg_from_ggsn), now,
890 &reply_buf, &sgsn_ofd, &sgsn_addr);
Neels Hofmeyrf4906e52015-12-06 23:12:02 +0100891 if (*msg_to_sgsn) {
892 LVL2_ASSERT(send > 0);
893 LVL2_ASSERT(same_addr(&sgsn_addr, sgsn_receiver));
894 LVL2_ASSERT(reply_is(msg_to_sgsn));
895 }
896 else
897 LVL2_ASSERT(send == 0);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100898 return 1;
899}
900
901static int create_pdp_ctx()
902{
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100903 const char *gtp_req_from_sgsn =
904 MSG_PDP_CTX_REQ("0068",
905 "abcd",
906 "60",
907 "42000121436587f9",
908 "00000123",
909 "00000321",
910 "0009""08696e7465726e6574", /* "(8)internet" */
911 "0004""c0a82a17", /* same as default sgsn_sender */
912 "0004""c0a82a17"
913 );
914 const char *gtp_req_to_ggsn =
915 MSG_PDP_CTX_REQ("0068",
916 "6d31", /* mapped seq ("abcd") */
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100917 "23",
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100918 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100919 "00000001", /* Data I: tunnel's TEI */
920 "00000001", /* Control: tunnel's TEI */
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100921 "0009""08696e7465726e6574",
922 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
923 "0004""7f000202" /* replaced with gtphub's ggsn user */
924 );
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100925
926 LVL2_ASSERT(msg_from_sgsn_c(&sgsn_sender,
927 &resolved_ggsn_addr,
928 gtp_req_from_sgsn,
929 gtp_req_to_ggsn));
930 LVL2_ASSERT(was_resolved_for("240010123456789", "internet"));
931
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100932 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100933 "TEI=1:"
934 " 192.168.42.23 (TEI C=321 U=123)"
935 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100936 " @21945\n"));
937
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100938 const char *gtp_resp_from_ggsn =
939 MSG_PDP_CTX_RSP("004e",
940 "00000001", /* destination TEI (sent in req above) */
941 "6d31", /* mapped seq */
942 "01", /* restart */
943 "00000567", /* TEI U */
944 "00000765", /* TEI C */
945 "0004""c0a82b22", /* GSN addresses */
946 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
947 );
948 const char *gtp_resp_to_sgsn =
949 MSG_PDP_CTX_RSP("004e",
950 "00000321", /* unmapped TEI ("001") */
951 "abcd", /* unmapped seq ("6d31") */
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100952 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100953 "00000001", /* mapped TEI from GGSN ("567") */
954 "00000001", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100955 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
956 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
957 );
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100958 /* The response should go back to whichever port the request came from
959 * (unmapped by sequence nr) */
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100960 LVL2_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
961 &sgsn_sender,
962 gtp_resp_from_ggsn,
963 gtp_resp_to_sgsn));
964
965 return 1;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200966}
967
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +0100968#define MSG_DEL_PDP_CTX_REQ(tei, seq) \
969 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \
970 "14" /* type 20: Delete PDP Context Request */ \
971 "0008" /* msg length = 8 + len (2 octets) */ \
972 tei /* TEI Ctrl */ \
973 seq /* Sequence nr (2 octets) */ \
974 "00" /* N-PDU 0 */ \
975 "00" /* No extensions */ \
976 /* IEs */ \
977 "13fe" /* 19: Teardown ind = 0 */ \
978 "1400" /* 20: NSAPI = 0*/ \
979
980#define MSG_DEL_PDP_CTX_RSP(tei, seq) \
981 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \
982 "15" /* type 21: Delete PDP Context Response */ \
983 "0006" /* msg length = 8 + len (2 octets) */ \
984 tei /* TEI Ctrl */ \
985 seq /* Sequence nr (2 octets) */ \
986 "00" /* N-PDU 0 */ \
987 "00" /* No extensions */ \
988 /* IEs */ \
989 "01" /* 1: Cause */ \
990 "80" /* value = 0b10000000 = response, no rejection. */ \
991
Neels Hofmeyr200aff62015-12-01 01:01:16 +0100992static int delete_pdp_ctx_from_sgsn(void)
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200993{
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +0100994 now += GTPH_EXPIRE_QUICKLY_SECS + 1;
995 gtphub_gc(hub, now);
996
997 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100998 "TEI=1:"
999 " 192.168.42.23 (TEI C=321 U=123)"
1000 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001001 " @21945\n"));
1002
1003 /* TEI Ctrl from above and next sequence after abcd. */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001004 const char *gtp_req_from_sgsn = MSG_DEL_PDP_CTX_REQ("00000001", "abce");
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001005 const char *gtp_req_to_ggsn = MSG_DEL_PDP_CTX_REQ("00000765", "6d32");
1006
1007 LVL2_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1008 &resolved_ggsn_addr,
1009 gtp_req_from_sgsn,
1010 gtp_req_to_ggsn));
1011
1012 /* 21945 + 31 = 21976 */
1013 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001014 "TEI=1:"
1015 " 192.168.42.23 (TEI C=321 U=123)"
1016 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001017 " @21976\n"));
1018
1019 const char *gtp_resp_from_ggsn =
1020 MSG_DEL_PDP_CTX_RSP("00000001", "6d32");
1021 const char *gtp_resp_to_sgsn =
1022 MSG_DEL_PDP_CTX_RSP("00000321", "abce");
1023
1024 /* The response should go back to whichever port the request came from
1025 * (unmapped by sequence nr) */
1026 LVL2_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1027 &sgsn_sender,
1028 gtp_resp_from_ggsn,
1029 gtp_resp_to_sgsn));
1030
1031 LVL2_ASSERT(tunnels_are(""));
1032
1033 return 1;
1034}
1035
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001036static int delete_pdp_ctx_from_ggsn(void)
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001037{
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001038 now += GTPH_EXPIRE_QUICKLY_SECS + 1;
1039 gtphub_gc(hub, now);
1040
1041 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001042 "TEI=1:"
1043 " 192.168.42.23 (TEI C=321 U=123)"
1044 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001045 " @21945\n"));
1046
1047 /* TEI Ctrl from above and next sequence after abcd. */
1048 const char *gtp_req_from_ggsn = MSG_DEL_PDP_CTX_REQ("00000001", "5432");
1049 const char *gtp_req_to_sgsn = MSG_DEL_PDP_CTX_REQ("00000321", "6d31");
1050
1051 LVL2_ASSERT(msg_from_ggsn_c(&ggsn_sender,
1052 &resolved_sgsn_addr,
1053 gtp_req_from_ggsn,
1054 gtp_req_to_sgsn));
1055
1056 /* 21945 + 31 = 21976 */
1057 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001058 "TEI=1:"
1059 " 192.168.42.23 (TEI C=321 U=123)"
1060 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001061 " @21976\n"));
1062
1063 const char *gtp_resp_from_sgsn =
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001064 MSG_DEL_PDP_CTX_RSP("00000001", "6d31");
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001065 const char *gtp_resp_to_ggsn =
1066 MSG_DEL_PDP_CTX_RSP("00000765", "5432");
1067
1068 /* The response should go back to whichever port the request came from
1069 * (unmapped by sequence nr) */
1070 LVL2_ASSERT(msg_from_sgsn_c(&resolved_sgsn_addr,
1071 &ggsn_sender,
1072 gtp_resp_from_sgsn,
1073 gtp_resp_to_ggsn));
1074
1075 LVL2_ASSERT(tunnels_are(""));
1076
1077 return 1;
1078}
1079
1080static void test_one_pdp_ctx(int del_from_side)
1081{
1082 if (del_from_side == GTPH_SIDE_SGSN)
1083 LOG("test_one_pdp_ctx (del from SGSN)")
1084 else LOG("test_one_pdp_ctx (del from GGSN)");
Neels Hofmeyr43283a32015-11-10 22:07:04 +01001085 OSMO_ASSERT(setup_test_hub());
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001086
Neels Hofmeyr43283a32015-11-10 22:07:04 +01001087 OSMO_ASSERT(create_pdp_ctx());
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001088
1089 struct gtphub_peer_port *ggsn_port =
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +01001090 gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr43283a32015-11-10 22:07:04 +01001091 &resolved_ggsn_addr);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001092 OSMO_ASSERT(ggsn_port);
1093 struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer;
1094 /* now == 345; now + 30 == 375.
1095 * seq mapping from above:
1096 * 0xabcd == 43981 (sent in the packet)
1097 * 0x6d31 == 27953 (harcoded seq mapping start val) */
1098 OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@375), "));
1099
1100 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945.
1101 * 0x00000321 == 801 (TEI from SGSN Ctrl)
1102 * 0x00000123 == 291 (TEI from SGSN User)
1103 * 0x00000765 == 1893 (TEI from GGSN Ctrl)
1104 * 0x00000567 == 1383 (TEI from GGSN User)
1105 * Mapped TEIs should be 1 and 2. */
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001106 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001107 "TEI=1:"
1108 " 192.168.42.23 (TEI C=321 U=123)"
1109 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001110 " @21945\n"));
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001111
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001112 if (del_from_side == GTPH_SIDE_SGSN) {
1113 OSMO_ASSERT(delete_pdp_ctx_from_sgsn());
1114 } else {
1115 OSMO_ASSERT(delete_pdp_ctx_from_ggsn());
1116 }
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001117 OSMO_ASSERT(tunnels_are(""));
1118
Neels Hofmeyr21d08732015-11-20 00:08:28 +01001119 OSMO_ASSERT(clear_test_hub());
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001120}
1121
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001122static void test_user_data(void)
1123{
1124 LOG("test_user_data");
1125
1126 OSMO_ASSERT(setup_test_hub());
1127
1128 OSMO_ASSERT(create_pdp_ctx());
1129
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001130 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945. */
1131 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001132 "TEI=1:"
1133 " 192.168.42.23 (TEI C=321 U=123)"
1134 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001135 " @21945\n"));
1136
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001137 LOG("- user data starts");
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001138 /* Now expect default port numbers for User plane. */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001139 resolve_to_ggsn("192.168.43.34", 2152);
1140 resolve_to_sgsn("192.168.42.23", 2152);
1141
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001142 /* 10 minutes later */
1143 now += 600;
1144
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001145 const char *u_from_ggsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001146 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001147 "ff" /* type 255: G-PDU */
1148 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001149 "00000001" /* mapped TEI for SGSN from create_pdp_ctx() */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001150 "0070" /* seq */
1151 "0000" /* No extensions */
1152 /* User data (ICMP packet), 96 - 12 = 84 octets */
1153 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1154 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1155 "202122232425262728292a2b2c2d2e2f3031323334353637"
1156 ;
1157 const char *u_to_sgsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001158 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001159 "ff" /* type 255: G-PDU */
1160 "0058" /* length: 88 + 8 octets == 96 */
1161 "00000123" /* unmapped User TEI */
1162 "6d31" /* new mapped seq */
1163 "0000"
1164 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1165 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1166 "202122232425262728292a2b2c2d2e2f3031323334353637"
1167 ;
1168
1169 /* This depends on create_pdp_ctx() sending resolved_sgsn_addr as GSN
1170 * Address IEs in the GGSN's Create PDP Ctx Response. */
1171 OSMO_ASSERT(msg_from_ggsn_u(&ggsn_sender,
1172 &resolved_sgsn_addr,
1173 u_from_ggsn,
1174 u_to_sgsn));
1175
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001176 /* Make sure the user plane messages have refreshed the TEI mapping
1177 * timeouts: 21945 + 600 == 22545. */
1178 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001179 "TEI=1:"
1180 " 192.168.42.23 (TEI C=321 U=123)"
1181 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001182 " @22545\n"));
1183
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001184 const char *u_from_sgsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001185 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001186 "ff" /* type 255: G-PDU */
1187 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001188 "00000001" /* mapped User TEI for GGSN from create_pdp_ctx() */
Neels Hofmeyrd4abc822015-12-03 14:19:08 +01001189 "1234" /* unknown seq */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001190 "0000" /* No extensions */
1191 /* User data (ICMP packet), 96 - 12 = 84 octets */
1192 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1193 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1194 "202122232425262728292a2b2c2d2e2f3031323334353637"
1195 ;
1196 const char *u_to_ggsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001197 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001198 "ff" /* type 255: G-PDU */
1199 "0058" /* length: 88 + 8 octets == 96 */
1200 "00000567" /* unmapped User TEI */
Neels Hofmeyrd4abc822015-12-03 14:19:08 +01001201 "6d31" /* unmapped seq */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001202 "0000"
1203 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1204 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1205 "202122232425262728292a2b2c2d2e2f3031323334353637"
1206 ;
1207
Neels Hofmeyrd4abc822015-12-03 14:19:08 +01001208 OSMO_ASSERT(msg_from_sgsn_u(&sgsn_sender,
1209 &resolved_ggsn_addr,
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001210 u_from_sgsn,
1211 u_to_ggsn));
1212
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001213 /* Make sure the user plane messages have refreshed the TEI mapping
1214 * timeouts: 21945 + 600 == 22545. Both timeouts refreshed: */
1215 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001216 "TEI=1:"
1217 " 192.168.42.23 (TEI C=321 U=123)"
1218 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001219 " @22545\n"));
1220
Neels Hofmeyr21d08732015-11-20 00:08:28 +01001221 OSMO_ASSERT(clear_test_hub());
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001222}
1223
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001224static void test_reused_tei(void)
1225{
1226 LOG("test_reused_tei");
1227
1228 OSMO_ASSERT(setup_test_hub());
1229
1230 OSMO_ASSERT(create_pdp_ctx());
1231
1232 const char *gtp_req_from_sgsn =
1233 MSG_PDP_CTX_REQ("0068",
1234 "abce", /* Next seq */
1235 "60",
1236 "42000121436587f9",
1237 "00000123", /* Same TEIs as before */
1238 "00000321",
1239 "0009""08696e7465726e6574", /* "(8)internet" */
1240 "0004""c0a82a17", /* same as default sgsn_sender */
1241 "0004""c0a82a17"
1242 );
1243 const char *gtp_req_to_ggsn =
1244 MSG_PDP_CTX_REQ("0068",
1245 "6d32", /* mapped seq ("abce") */
1246 "23",
1247 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001248 "00000002", /* mapped TEI Data I ("123") */
1249 "00000002", /* mapped TEI Control ("321") */
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001250 "0009""08696e7465726e6574",
1251 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1252 "0004""7f000202" /* replaced with gtphub's ggsn user */
1253 );
1254
1255 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1256 &resolved_ggsn_addr,
1257 gtp_req_from_sgsn,
1258 gtp_req_to_ggsn));
1259 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
1260
1261 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001262 "TEI=2:"
1263 " 192.168.42.23 (TEI C=321 U=123)"
1264 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001265 " @21945\n"));
1266
1267 const char *gtp_resp_from_ggsn =
1268 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001269 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001270 "6d32", /* mapped seq */
1271 "01", /* restart */
1272 "00000567", /* TEI U */
1273 "00000765", /* TEI C */
1274 "0004""c0a82b22", /* GSN addresses */
1275 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1276 );
1277 const char *gtp_resp_to_sgsn =
1278 MSG_PDP_CTX_RSP("004e",
1279 "00000321", /* unmapped TEI ("001") */
1280 "abce", /* unmapped seq ("6d32") */
1281 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001282 "00000002", /* mapped TEI from GGSN ("567") */
1283 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001284 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1285 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1286 );
1287 /* The response should go back to whichever port the request came from
1288 * (unmapped by sequence nr) */
1289 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1290 &sgsn_sender,
1291 gtp_resp_from_ggsn,
1292 gtp_resp_to_sgsn));
1293
1294 OSMO_ASSERT(clear_test_hub());
1295}
1296
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001297static void test_peer_restarted(void)
1298{
1299 LOG("test_peer_restarted");
1300
1301 OSMO_ASSERT(setup_test_hub());
1302
1303 OSMO_ASSERT(create_pdp_ctx());
1304
1305 now += 10;
1306
1307 const char *gtp_req_from_sgsn =
1308 MSG_PDP_CTX_REQ("0068",
1309 "1234", /* brand new seq */
1310 "61", /* DIFFERING restart counter */
1311 "42000121436587f9",
1312 "00000abc",
1313 "00000cba",
1314 "0009""08696e7465726e6574", /* "(8)internet" */
1315 "0004""c0a82a17", /* same as default sgsn_sender */
1316 "0004""c0a82a17"
1317 );
1318 const char *gtp_req_to_ggsn =
1319 MSG_PDP_CTX_REQ("0068",
Neels Hofmeyrfe436262015-12-02 15:44:34 +01001320 "6d33", /* mapped seq ("1234") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001321 "23",
1322 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001323 "00000002", /* mapped TEI Data I ("123") */
1324 "00000002", /* mapped TEI Control ("321") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001325 "0009""08696e7465726e6574",
1326 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1327 "0004""7f000202" /* replaced with gtphub's ggsn user */
1328 );
1329
1330 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1331 &resolved_ggsn_addr,
1332 gtp_req_from_sgsn,
1333 gtp_req_to_ggsn));
1334 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
1335
1336 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001337 "TEI=2:"
1338 " 192.168.42.23 (TEI C=cba U=abc)"
1339 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001340 " @21955\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001341 "TEI=1:"
1342 " (uninitialized) (TEI C=321 U=123)"
1343 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001344 " @21945\n"
1345 ));
1346
1347 const char *gtp_resp_from_ggsn =
1348 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001349 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyrfe436262015-12-02 15:44:34 +01001350 "6d33", /* mapped seq */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001351 "01", /* restart */
1352 "00000def", /* TEI U */
1353 "00000fde", /* TEI C */
1354 "0004""c0a82b22", /* GSN addresses */
1355 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1356 );
1357 const char *gtp_resp_to_sgsn =
1358 MSG_PDP_CTX_RSP("004e",
1359 "00000cba", /* unmapped TEI ("005") */
1360 "1234", /* unmapped seq ("6d32") */
1361 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001362 "00000002", /* mapped TEI from GGSN ("567") */
1363 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001364 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1365 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1366 );
1367 /* The response should go back to whichever port the request came from
1368 * (unmapped by sequence nr) */
1369 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1370 &sgsn_sender,
1371 gtp_resp_from_ggsn,
1372 gtp_resp_to_sgsn));
1373
1374 OSMO_ASSERT(clear_test_hub());
1375}
1376
1377static void test_peer_restarted_reusing_tei(void)
1378{
1379 LOG("test_peer_restarted_reusing_tei");
1380
1381 OSMO_ASSERT(setup_test_hub());
1382
1383 OSMO_ASSERT(create_pdp_ctx());
1384
1385 now += 10;
1386
1387 const char *gtp_req_from_sgsn =
1388 MSG_PDP_CTX_REQ("0068",
1389 "1234", /* brand new seq */
1390 "61", /* DIFFERING restart counter */
1391 "42000121436587f9",
1392 "00000123", /* SAME TEI */
1393 "00000321",
1394 "0009""08696e7465726e6574", /* "(8)internet" */
1395 "0004""c0a82a17", /* same as default sgsn_sender */
1396 "0004""c0a82a17"
1397 );
1398 const char *gtp_req_to_ggsn =
1399 MSG_PDP_CTX_REQ("0068",
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001400 "6d33", /* seq 6d31 + 2, after "out-of-band" Delete PDP Ctx
1401 due to differing restart counter. */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001402 "23",
1403 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001404 "00000002", /* mapped TEI Data I ("123") */
1405 "00000002", /* mapped TEI Control ("321") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001406 "0009""08696e7465726e6574",
1407 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1408 "0004""7f000202" /* replaced with gtphub's ggsn user */
1409 );
1410
1411 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1412 &resolved_ggsn_addr,
1413 gtp_req_from_sgsn,
1414 gtp_req_to_ggsn));
1415 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
1416
1417 OSMO_ASSERT(tunnels_are(
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001418 "TEI=2:" /* being established after restart */
1419 " 192.168.42.23 (TEI C=321 U=123)"
1420 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
1421 " @21955\n"
1422 "TEI=1:" /* invalidated due to restart */
1423 " (uninitialized) (TEI C=321 U=123)"
1424 " <-> 192.168.43.34 (TEI C=765 U=567)"
1425 " @21945\n"
1426 ));
1427
1428 /* An "out-of-band" delete request should have been sent to the GGSN
1429 * (checked by expected log output in gtphub_test.ok), and the GGSN
1430 * will (usually) send a Delete Response like this: */
1431 const char *gtp_del_resp_from_ggsn =
1432 MSG_DEL_PDP_CTX_RSP("00000001", "6d32");
1433
1434 /* For this response (due to peer restart) we expect no forwarded
1435 * message. */
1436 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1437 &sgsn_sender,
1438 gtp_del_resp_from_ggsn,
1439 ""));
1440
1441 OSMO_ASSERT(tunnels_are(
1442 "TEI=2:" /* still being established after restart */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001443 " 192.168.42.23 (TEI C=321 U=123)"
1444 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001445 " @21955\n"
1446 ));
1447
1448 const char *gtp_resp_from_ggsn =
1449 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001450 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001451 "6d33", /* mapped seq */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001452 "01", /* restart */
1453 "00000def", /* TEI U */
1454 "00000fde", /* TEI C */
1455 "0004""c0a82b22", /* GSN addresses */
1456 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1457 );
1458 const char *gtp_resp_to_sgsn =
1459 MSG_PDP_CTX_RSP("004e",
1460 "00000321", /* unmapped TEI ("005") */
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001461 "1234", /* unmapped seq ("6d33") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001462 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001463 "00000002", /* mapped TEI from GGSN ("567") */
1464 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001465 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1466 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1467 );
1468 /* The response should go back to whichever port the request came from
1469 * (unmapped by sequence nr) */
1470 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1471 &sgsn_sender,
1472 gtp_resp_from_ggsn,
1473 gtp_resp_to_sgsn));
1474
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001475 OSMO_ASSERT(tunnels_are(
1476 "TEI=2:" /* still being established after restart */
1477 " 192.168.42.23 (TEI C=321 U=123)"
1478 " <-> 192.168.43.34 (TEI C=fde U=def)"
1479 " @21955\n"
1480 ));
1481
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001482 OSMO_ASSERT(clear_test_hub());
1483}
1484
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001485static void test_sgsn_behind_nat(void)
1486{
1487 LOG("test_user_data");
1488
1489 OSMO_ASSERT(setup_test_hub());
1490 hub->sgsn_use_sender = 1; /* <-- Main difference to test_user_data() */
1491 resolve_to_sgsn("192.168.42.23", 423); /* Same as sender */
1492
1493 OSMO_ASSERT(create_pdp_ctx());
1494
1495 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945. */
1496 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001497 "TEI=1:"
1498 " 192.168.42.23 (TEI C=321 U=123)"
1499 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001500 " @21945\n"));
1501
1502 LOG("- user data starts");
1503 /* Now expect default port numbers for User plane -- except SGSN. */
1504 resolve_to_ggsn("192.168.43.34", 2152);
1505
1506 /* 10 minutes later */
1507 now += 600;
1508
1509 const char *u_from_ggsn =
1510 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1511 "ff" /* type 255: G-PDU */
1512 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001513 "00000001" /* mapped User TEI for SGSN from create_pdp_ctx() */
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001514 "0070" /* seq */
1515 "0000" /* No extensions */
1516 /* User data (ICMP packet), 96 - 12 = 84 octets */
1517 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1518 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1519 "202122232425262728292a2b2c2d2e2f3031323334353637"
1520 ;
1521 const char *u_to_sgsn =
1522 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1523 "ff" /* type 255: G-PDU */
1524 "0058" /* length: 88 + 8 octets == 96 */
1525 "00000123" /* unmapped User TEI */
1526 "6d31" /* new mapped seq */
1527 "0000"
1528 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1529 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1530 "202122232425262728292a2b2c2d2e2f3031323334353637"
1531 ;
1532
1533 /* This depends on create_pdp_ctx() sending resolved_sgsn_addr as GSN
1534 * Address IEs in the GGSN's Create PDP Ctx Response. */
1535 OSMO_ASSERT(msg_from_ggsn_u(&ggsn_sender,
1536 &resolved_sgsn_addr,
1537 u_from_ggsn,
1538 u_to_sgsn));
1539
1540 /* Make sure the user plane messages have refreshed the TEI mapping
1541 * timeouts: 21945 + 600 == 22545. */
1542 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001543 "TEI=1:"
1544 " 192.168.42.23 (TEI C=321 U=123)"
1545 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001546 " @22545\n"));
1547
1548 const char *u_from_sgsn =
1549 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1550 "ff" /* type 255: G-PDU */
1551 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001552 "00000001" /* mapped User TEI for GGSN from create_pdp_ctx() */
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001553 "1234" /* unknown seq */
1554 "0000" /* No extensions */
1555 /* User data (ICMP packet), 96 - 12 = 84 octets */
1556 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1557 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1558 "202122232425262728292a2b2c2d2e2f3031323334353637"
1559 ;
1560 const char *u_to_ggsn =
1561 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1562 "ff" /* type 255: G-PDU */
1563 "0058" /* length: 88 + 8 octets == 96 */
1564 "00000567" /* unmapped User TEI */
1565 "6d31" /* unmapped seq */
1566 "0000"
1567 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1568 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1569 "202122232425262728292a2b2c2d2e2f3031323334353637"
1570 ;
1571
1572 OSMO_ASSERT(msg_from_sgsn_u(&sgsn_sender,
1573 &resolved_ggsn_addr,
1574 u_from_sgsn,
1575 u_to_ggsn));
1576
1577 /* Make sure the user plane messages have refreshed the TEI mapping
1578 * timeouts: 21945 + 600 == 22545. Both timeouts refreshed: */
1579 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001580 "TEI=1:"
1581 " 192.168.42.23 (TEI C=321 U=123)"
1582 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001583 " @22545\n"));
1584
1585 OSMO_ASSERT(clear_test_hub());
1586}
1587
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001588void test_parallel_context_creation(void)
1589{
1590 LOG("test_parallel_context_creation");
1591
1592 OSMO_ASSERT(setup_test_hub());
1593
1594 const char *gtp_req_from_sgsn1 =
1595 MSG_PDP_CTX_REQ("0068",
1596 "abcd",
1597 "60",
1598 "42000121436587f9",
1599 "00000123",
1600 "00000321",
1601 "0009""08696e7465726e6574", /* "(8)internet" */
1602 "0004""c0a82a17", /* same as default sgsn_sender */
1603 "0004""c0a82a17"
1604 );
1605 const char *gtp_req_to_ggsn1 =
1606 MSG_PDP_CTX_REQ("0068",
1607 "6d31", /* mapped seq ("abcd") */
1608 "23",
1609 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001610 "00000001", /* mapped TEI Data I ("123") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001611 "00000001", /* mapped TEI Control ("321") */
1612 "0009""08696e7465726e6574",
1613 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1614 "0004""7f000202" /* replaced with gtphub's ggsn user */
1615 );
1616
1617 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1618 &resolved_ggsn_addr,
1619 gtp_req_from_sgsn1,
1620 gtp_req_to_ggsn1));
1621
1622 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001623 "TEI=1:"
1624 " 192.168.42.23 (TEI C=321 U=123)"
1625 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001626 " @21945\n"));
1627
1628 now ++;
1629
1630 const char *gtp_req_from_sgsn2 =
1631 MSG_PDP_CTX_REQ("0068",
1632 "abce",
1633 "60",
1634 "42000121436588f9",
1635 "00000124",
1636 "00000322",
1637 "0009""08696e7465726e6574", /* "(8)internet" */
1638 "0004""c0a82a17", /* same as default sgsn_sender */
1639 "0004""c0a82a17"
1640 );
1641 const char *gtp_req_to_ggsn2 =
1642 MSG_PDP_CTX_REQ("0068",
1643 "6d32", /* mapped seq ("abce") */
1644 "23",
1645 "42000121436588f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001646 "00000002", /* mapped TEI Data I ("124") */
1647 "00000002", /* mapped TEI Control ("322") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001648 "0009""08696e7465726e6574",
1649 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1650 "0004""7f000202" /* replaced with gtphub's ggsn user */
1651 );
1652
1653 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1654 &resolved_ggsn_addr,
1655 gtp_req_from_sgsn2,
1656 gtp_req_to_ggsn2));
1657
1658 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001659 "TEI=2:"
1660 " 192.168.42.23 (TEI C=322 U=124)"
1661 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001662 " @21946\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001663 "TEI=1:"
1664 " 192.168.42.23 (TEI C=321 U=123)"
1665 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001666 " @21945\n"
1667 ));
1668
1669 now ++;
1670
1671 const char *gtp_resp_from_ggsn1 =
1672 MSG_PDP_CTX_RSP("004e",
1673 "00000001", /* destination TEI (sent in req above) */
1674 "6d31", /* mapped seq */
1675 "01", /* restart */
1676 "00000567", /* TEI U */
1677 "00000765", /* TEI C */
1678 "0004""c0a82b22", /* GSN addresses */
1679 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1680 );
1681 const char *gtp_resp_to_sgsn1 =
1682 MSG_PDP_CTX_RSP("004e",
1683 "00000321", /* unmapped TEI ("001") */
1684 "abcd", /* unmapped seq ("6d31") */
1685 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001686 "00000001", /* mapped TEI from GGSN ("567") */
1687 "00000001", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001688 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1689 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1690 );
1691
1692 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1693 &sgsn_sender,
1694 gtp_resp_from_ggsn1,
1695 gtp_resp_to_sgsn1));
1696
1697 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001698 "TEI=2:"
1699 " 192.168.42.23 (TEI C=322 U=124)"
1700 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001701 " @21946\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001702 "TEI=1:"
1703 " 192.168.42.23 (TEI C=321 U=123)"
1704 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001705 " @21947\n"
1706 ));
1707
1708 now ++;
1709
1710 const char *gtp_resp_from_ggsn2 =
1711 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001712 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001713 "6d32", /* mapped seq */
1714 "01", /* restart */
1715 "00000568", /* TEI U */
1716 "00000766", /* TEI C */
1717 "0004""c0a82b22", /* GSN addresses */
1718 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1719 );
1720 const char *gtp_resp_to_sgsn2 =
1721 MSG_PDP_CTX_RSP("004e",
1722 "00000322", /* unmapped TEI ("001") */
1723 "abce", /* unmapped seq ("6d31") */
1724 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001725 "00000002", /* mapped TEI from GGSN ("567") */
1726 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001727 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1728 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1729 );
1730
1731 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1732 &sgsn_sender,
1733 gtp_resp_from_ggsn2,
1734 gtp_resp_to_sgsn2));
1735
1736 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001737 "TEI=2:"
1738 " 192.168.42.23 (TEI C=322 U=124)"
1739 " <-> 192.168.43.34 (TEI C=766 U=568)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001740 " @21948\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001741 "TEI=1:"
1742 " 192.168.42.23 (TEI C=321 U=123)"
1743 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001744 " @21947\n"
1745 ));
1746
1747 OSMO_ASSERT(clear_test_hub());
1748}
1749
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001750
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001751static struct log_info_cat gtphub_categories[] = {
1752 [DGTPHUB] = {
1753 .name = "DGTPHUB",
1754 .description = "GTP Hub",
1755 .color = "\033[1;33m",
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001756 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001757 },
1758};
1759
1760static struct log_info info = {
1761 .cat = gtphub_categories,
1762 .num_cat = ARRAY_SIZE(gtphub_categories),
1763};
1764
1765int main(int argc, char **argv)
1766{
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001767 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
Neels Hofmeyr8de6b862018-04-16 00:57:10 +02001768 void *log_ctx = talloc_named_const(osmo_gtphub_ctx, 0, "log");
1769 osmo_init_logging2(log_ctx, &info);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001770
1771 test_nr_map_basic();
Neels Hofmeyr6a65a8f2015-11-17 14:30:37 +01001772 test_nr_map_wrap();
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001773 test_expiry();
1774 test_echo();
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001775 test_one_pdp_ctx(GTPH_SIDE_SGSN);
1776 test_one_pdp_ctx(GTPH_SIDE_GGSN);
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001777 test_user_data();
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001778 test_reused_tei();
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001779 test_peer_restarted();
1780 test_peer_restarted_reusing_tei();
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001781 test_sgsn_behind_nat();
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001782 test_parallel_context_creation();
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001783 printf("Done\n");
1784
1785 talloc_report_full(osmo_gtphub_ctx, stderr);
Neels Hofmeyr8de6b862018-04-16 00:57:10 +02001786 talloc_free(log_ctx);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001787 OSMO_ASSERT(talloc_total_blocks(osmo_gtphub_ctx) == 1);
Neels Hofmeyr8de6b862018-04-16 00:57:10 +02001788 talloc_free(osmo_gtphub_ctx);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001789 return 0;
1790}
1791