blob: d381b9b868d668a8a7466ee941d5068877a219cd [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
532 if (cmp != 0) {
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100533 printf("\n%s:%d: reply_is(): MISMATCH\n"
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200534 " expecting:\n'%s'\n"
535 " got:\n'%s'\n\n",
536 file,
537 line,
538 hex, dump);
539 int i;
540 int l = strlen(hex);
541 int m = strlen(dump);
542 if (m < l)
543 l = m;
544 for (i = 0; i < l; i++) {
545 if (hex[i] != dump[i]) {
546 printf("First mismatch at position %d:\n"
547 " %s\n %s\n", i, hex + i, dump + i);
548 break;
549 }
550 }
551 }
552 return cmp == 0;
553}
554
555#define same_addr(GOT, EXPECTED) _same_addr((GOT),(EXPECTED), __FILE__, __LINE__)
Alexander Couzens323aaf62020-07-18 16:45:25 +0200556static int _same_addr(const struct sgsn_sockaddr *got,
557 const struct sgsn_sockaddr *expected,
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200558 const char *file, int line)
559{
Alexander Couzens323aaf62020-07-18 16:45:25 +0200560 int cmp = sgsn_sockaddr_cmp(got, expected);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200561 if (!cmp)
562 return 1;
563 char buf[256];
564 printf("\n%s:%d: addr_is(): MISMATCH\n"
565 " expecting: '%s'\n"
566 " got: '%s'\n\n",
567 file, line,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200568 sgsn_sockaddr_to_str(expected),
569 sgsn_sockaddr_to_strb(got, buf, sizeof(buf)));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200570 return 0;
571}
572
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100573
574time_t now;
575static struct gtphub _hub;
576static struct gtphub *hub = &_hub;
577
578static int setup_test_hub()
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200579{
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100580 /* Not really needed, but to make 100% sure... */
581 ZERO_STRUCT(hub);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200582
583 gtphub_init(hub);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100584
585 /* Tell this mock gtphub its local address for this test. */
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100586 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100587 "127.0.1.1") == 0);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100588 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100589 "127.0.1.2") == 0);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100590 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100591 "127.0.2.1") == 0);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100592 LVL2_ASSERT(gsn_addr_from_str(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].local_addr,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100593 "127.0.2.2") == 0);
594
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100595 hub->restart_counter = 0x23;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100596 now = 345;
597 LVL2_ASSERT(send_from_sgsn("192.168.42.23", 423));
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100598 LVL2_ASSERT(resolve_to_ggsn("192.168.43.34", 2123));
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100599 LVL2_ASSERT(send_from_ggsn("192.168.43.34", 434));
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100600 LVL2_ASSERT(resolve_to_sgsn("192.168.42.23", 2123));
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100601
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100602#define GGSNS_CTRL_FD 1
603#define GGSNS_USER_FD 2
604#define SGSNS_CTRL_FD 3
605#define SGSNS_USER_FD 4
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100606 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL].ofd.priv_nr = GGSNS_CTRL_FD;
607 hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER].ofd.priv_nr = GGSNS_USER_FD;
608 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL].ofd.priv_nr = SGSNS_CTRL_FD;
609 hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER].ofd.priv_nr = SGSNS_USER_FD;
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100610
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100611 return 1;
612}
613
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100614static int clear_test_hub()
615{
616 /* expire all */
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100617 gtphub_gc(hub, now + (60 * GTPH_EXPIRE_SLOWLY_MINUTES) + 1);
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100618
619 int plane_idx;
620 plane_idx = GTPH_PLANE_CTRL;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100621 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_GGSN][plane_idx].peers));
622 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_SGSN][plane_idx].peers));
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100623 plane_idx = GTPH_PLANE_USER;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100624 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_GGSN][plane_idx].peers));
625 LVL2_ASSERT(llist_empty(&hub->to_gsns[GTPH_SIDE_SGSN][plane_idx].peers));
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100626
Neels Hofmeyr0fa507a2015-12-02 01:15:30 +0100627 LVL2_ASSERT(llist_empty(&hub->tunnels));
628 LVL2_ASSERT(llist_empty(&hub->pending_deletes));
629 LVL2_ASSERT(llist_empty(&hub->ggsn_lookups));
630 LVL2_ASSERT(llist_empty(&hub->resolved_ggsns));
631
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100632 gtphub_free(hub);
633 return 1;
634}
635
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100636static int tunnels_are(const char *expect)
637{
638 static char buf[4096];
639 char *pos = buf;
640 size_t len = sizeof(buf);
641 struct gtphub_tunnel *t;
642 llist_for_each_entry(t, &hub->tunnels, entry) {
643 size_t wrote = snprintf(pos, len, "%s @%d\n",
644 gtphub_tunnel_str(t),
645 (int)t->expiry_entry.expiry);
646 LVL2_ASSERT(wrote < len);
647 pos += wrote;
648 len -= wrote;
649 }
650 *pos = '\0';
651
652 if (strncmp(buf, expect, sizeof(buf)) != 0) {
653 fprintf(stderr, "FAILURE: tunnels_are() mismatches expected value:\n"
654 "EXPECTED:\n%s\n"
655 "IS:\n%s\n",
656 expect, buf);
657 LVL2_ASSERT("tunnels do not match expected listing.");
658 return 0;
659 }
660 return 1;
661}
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100662
663static void test_echo(void)
664{
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100665 LOG("test_echo");
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100666 OSMO_ASSERT(setup_test_hub());
667
668 now = 123;
Neels Hofmeyrf16657a2015-11-08 20:34:47 +0100669
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100670 struct osmo_fd *to_ofd;
Alexander Couzens323aaf62020-07-18 16:45:25 +0200671 struct sgsn_sockaddr to_addr;
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100672 struct gtphub_peer_port *pp;
673 int send;
674
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200675 const char *gtp_ping_from_sgsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100676 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200677 "01" /* type 01: Echo request */
678 "0004" /* length of 4 after header TEI */
679 "00000000" /* header TEI == 0 in Echo */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100680 "abcd" /* some 2 octet sequence nr */
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200681 "0000" /* N-PDU 0, no extension header (why is this here?) */
682 ;
683
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100684 const char *gtp_pong_to_sgsn =
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200685 "32"
686 "02" /* type 02: Echo response */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100687 "0006" /* length of 6 after header TEI */
688 "00000000" /* header TEI == 0 in Echo */
689 "abcd" /* same sequence nr */
690 "0000"
691 "0e23" /* Recovery with restart counter */
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200692 ;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200693
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100694 to_ofd = NULL;
695 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100696 send = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, GTPH_PLANE_CTRL,
697 &sgsn_sender, buf, msg(gtp_ping_from_sgsn),
698 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200699 OSMO_ASSERT(send > 0);
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100700 OSMO_ASSERT(to_addr.l);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100701 OSMO_ASSERT(same_addr(&to_addr, &sgsn_sender));
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100702 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == SGSNS_CTRL_FD));
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100703 OSMO_ASSERT(reply_is(gtp_pong_to_sgsn));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200704
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100705 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100706 &sgsn_sender);
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100707 /* We don't record Echo peers. */
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100708 OSMO_ASSERT(!pp);
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100709
710 const char *gtp_ping_from_ggsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100711 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100712 "01" /* type 01: Echo request */
713 "0004" /* length of 4 after header TEI */
714 "00000000" /* header TEI == 0 in Echo */
715 "cdef" /* some 2 octet sequence nr */
716 "0000" /* N-PDU 0, no extension header (why is this here?) */
717 ;
718
719 const char *gtp_pong_to_ggsn =
720 "32"
721 "02" /* type 02: Echo response */
722 "0006" /* length of 6 after header TEI */
723 "00000000" /* header TEI == 0 in Echo */
724 "cdef" /* same sequence nr */
725 "0000"
726 "0e23" /* Recovery with restart counter */
727 ;
728
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100729 to_ofd = NULL;
730 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100731 send = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, GTPH_PLANE_CTRL,
732 &ggsn_sender, buf, msg(gtp_ping_from_ggsn),
733 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200734 OSMO_ASSERT(send > 0);
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100735 OSMO_ASSERT(same_addr(&to_addr, &ggsn_sender));
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100736 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == GGSNS_CTRL_FD));
Neels Hofmeyra7c10152015-11-09 15:12:25 +0100737 OSMO_ASSERT(reply_is(gtp_pong_to_ggsn));
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200738
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100739 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100740 &sgsn_sender);
741 OSMO_ASSERT(!pp);
742
743
744 /* And all the same on the user plane. */
745
746 to_ofd = NULL;
747 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100748 send = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, GTPH_PLANE_USER,
749 &sgsn_sender, buf, msg(gtp_ping_from_sgsn),
750 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100751 OSMO_ASSERT(send > 0);
752 OSMO_ASSERT(to_addr.l);
753 OSMO_ASSERT(same_addr(&to_addr, &sgsn_sender));
754 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == SGSNS_USER_FD));
755 OSMO_ASSERT(reply_is(gtp_pong_to_sgsn));
756
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100757 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_SGSN][GTPH_PLANE_USER],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100758 &sgsn_sender);
759 OSMO_ASSERT(!pp);
760
761 to_ofd = NULL;
762 ZERO_STRUCT(&to_addr);
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100763 send = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, GTPH_PLANE_USER,
764 &ggsn_sender, buf, msg(gtp_ping_from_ggsn),
765 now, &reply_buf, &to_ofd, &to_addr);
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100766 OSMO_ASSERT(send > 0);
767 OSMO_ASSERT(same_addr(&to_addr, &ggsn_sender));
768 OSMO_ASSERT(to_ofd && (to_ofd->priv_nr == GGSNS_USER_FD));
769 OSMO_ASSERT(reply_is(gtp_pong_to_ggsn));
770
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100771 pp = gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_USER],
Neels Hofmeyr0b700e32015-11-20 00:57:05 +0100772 &sgsn_sender);
773 OSMO_ASSERT(!pp);
774
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200775
Neels Hofmeyr21d08732015-11-20 00:08:28 +0100776 OSMO_ASSERT(clear_test_hub());
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100777}
778
779
780#define MSG_PDP_CTX_REQ(len, seq, restart, imsi, tei_u, tei_c, apn, gsn_c, gsn_u) \
781 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \
782 "10" /* type 16: Create PDP Context Request */ \
783 len /* msg length = 8 + len (2 octets) */ \
784 "00000000" /* No TEI yet */ \
785 seq /* Sequence nr (2 octets) */ \
786 "00" /* N-PDU 0 */ \
787 "00" /* No extensions */ \
788 /* IEs */ \
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100789 "0e" restart /* 14: Recovery (restart counter: 1 octet) */ \
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100790 "02" /* 2 = IMSI */ \
791 imsi /* (8 octets) */ \
792 "0f01" /* 15: Selection mode = MS provided APN, subscription not verified*/ \
793 "10" /* 16: TEI Data I */ \
794 tei_u /* (4 octets) */ \
795 "11" /* 17: TEI Control Plane */ \
796 tei_c /* (4 octets) */ \
797 "1400" /* 20: NSAPI = 0*/ \
798 "1a" /* 26: Charging Characteristics */ \
799 "0800" \
800 "80" /* 128: End User Address */ \
801 "0002" /* length = 2: empty PDP Address */ \
802 "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ \
803 "83" /* 131: Access Point Name */ \
804 apn /* (2 octets length, N octets encoded APN-NI) */ \
805 "84" /* 132: Protocol Configuration Options */ \
806 "0015" /* length = 21 */ \
807 "80c0231101010011036d69670868656d6d656c6967" \
808 "85" /* 133: GSN Address */ \
809 gsn_c /* (2 octets length, N octets addr) */ \
810 "85" /* 133: GSN Address (second entry) */ \
811 gsn_u /* (2 octets length, N octets addr) */ \
812 "86" /* 134: MS International PSTN/ISDN Number (MSISDN) */ \
813 "0007" /* length */ \
814 "916407123254f6" /* 1946702123456(f) */ \
815 "87" /* 135: Quality of Service (QoS) Profile */ \
816 "0004" /* length */ \
817 "00" /* priority */ \
818 "0b921f" /* QoS profile data */
819
820#define MSG_PDP_CTX_RSP(len, tei_h, seq, restart, tei_u, tei_c, gsn_c, gsn_u) \
821 "32" \
822 "11" /* Create PDP Context Response */ \
823 len /* msg length = 8 + len (2 octets) */ \
824 tei_h /* destination TEI (sent in req above) */ \
825 seq /* mapped seq */ \
826 "00" "00" \
827 /* IEs */ \
828 "01" /* 1: Cause */ \
829 "80" /* value = 0b10000000 = response, no rejection. */ \
830 "08" /* 8: Reordering Required */ \
831 "00" /* not required. */ \
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100832 "0e" restart /* 14: Recovery */ \
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100833 "10" /* 16: TEI Data I */ \
834 tei_u \
835 "11" /* 17: TEI Control */ \
836 tei_c \
837 "7f" /* 127: Charging ID */ \
838 "00000001" \
839 "80" /* 128: End User Address */ \
840 "0006" /* length = 6 */ \
841 "f121" /* spare 0xf0, PDP organization 1, PDP type number 0x21 = 33 */ \
842 "7f000002" \
843 "84" /* 132: Protocol Configuration Options */ \
844 "0014" /* len = 20 */ \
845 "8080211002000010810608080808830600000000" \
846 "85" /* 133: GSN Address (Ctrl) */ \
847 gsn_c \
848 "85" /* 133: GSN Address (User) */ \
849 gsn_u \
850 "87" /* 135: Quality of Service (QoS) Profile */ \
851 "0004" /* length */ \
852 "00" /* priority */ \
853 "0b921f" /* QoS profile data */
854
855#define msg_from_sgsn_c(A,B,C,D) msg_from_sgsn(GTPH_PLANE_CTRL, A,B,C,D)
856#define msg_from_sgsn_u(A,B,C,D) msg_from_sgsn(GTPH_PLANE_USER, A,B,C,D)
857static int msg_from_sgsn(int plane_idx,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200858 struct sgsn_sockaddr *_sgsn_sender,
859 struct sgsn_sockaddr *ggsn_receiver,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100860 const char *hex_from_sgsn,
861 const char *hex_to_ggsn)
862{
863 struct osmo_fd *ggsn_ofd = NULL;
Alexander Couzens323aaf62020-07-18 16:45:25 +0200864 struct sgsn_sockaddr ggsn_addr;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100865 int send;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100866 send = gtphub_handle_buf(hub, GTPH_SIDE_SGSN, plane_idx, _sgsn_sender,
867 buf, msg(hex_from_sgsn), now,
868 &reply_buf, &ggsn_ofd, &ggsn_addr);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100869 LVL2_ASSERT(send > 0);
870 LVL2_ASSERT(same_addr(&ggsn_addr, ggsn_receiver));
871 LVL2_ASSERT(reply_is(hex_to_ggsn));
872 return 1;
873}
874
875#define msg_from_ggsn_c(A,B,C,D) msg_from_ggsn(GTPH_PLANE_CTRL, A,B,C,D)
876#define msg_from_ggsn_u(A,B,C,D) msg_from_ggsn(GTPH_PLANE_USER, A,B,C,D)
877static int msg_from_ggsn(int plane_idx,
Alexander Couzens323aaf62020-07-18 16:45:25 +0200878 struct sgsn_sockaddr *ggsn_sender,
879 struct sgsn_sockaddr *sgsn_receiver,
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100880 const char *msg_from_ggsn,
881 const char *msg_to_sgsn)
882{
883 struct osmo_fd *sgsn_ofd;
Alexander Couzens323aaf62020-07-18 16:45:25 +0200884 struct sgsn_sockaddr sgsn_addr;
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100885 int send;
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +0100886 send = gtphub_handle_buf(hub, GTPH_SIDE_GGSN, plane_idx, ggsn_sender,
887 buf, msg(msg_from_ggsn), now,
888 &reply_buf, &sgsn_ofd, &sgsn_addr);
Neels Hofmeyrf4906e52015-12-06 23:12:02 +0100889 if (*msg_to_sgsn) {
890 LVL2_ASSERT(send > 0);
891 LVL2_ASSERT(same_addr(&sgsn_addr, sgsn_receiver));
892 LVL2_ASSERT(reply_is(msg_to_sgsn));
893 }
894 else
895 LVL2_ASSERT(send == 0);
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100896 return 1;
897}
898
899static int create_pdp_ctx()
900{
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100901 const char *gtp_req_from_sgsn =
902 MSG_PDP_CTX_REQ("0068",
903 "abcd",
904 "60",
905 "42000121436587f9",
906 "00000123",
907 "00000321",
908 "0009""08696e7465726e6574", /* "(8)internet" */
909 "0004""c0a82a17", /* same as default sgsn_sender */
910 "0004""c0a82a17"
911 );
912 const char *gtp_req_to_ggsn =
913 MSG_PDP_CTX_REQ("0068",
914 "6d31", /* mapped seq ("abcd") */
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100915 "23",
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100916 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100917 "00000001", /* Data I: tunnel's TEI */
918 "00000001", /* Control: tunnel's TEI */
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100919 "0009""08696e7465726e6574",
920 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
921 "0004""7f000202" /* replaced with gtphub's ggsn user */
922 );
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100923
924 LVL2_ASSERT(msg_from_sgsn_c(&sgsn_sender,
925 &resolved_ggsn_addr,
926 gtp_req_from_sgsn,
927 gtp_req_to_ggsn));
928 LVL2_ASSERT(was_resolved_for("240010123456789", "internet"));
929
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100930 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100931 "TEI=1:"
932 " 192.168.42.23 (TEI C=321 U=123)"
933 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +0100934 " @21945\n"));
935
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100936 const char *gtp_resp_from_ggsn =
937 MSG_PDP_CTX_RSP("004e",
938 "00000001", /* destination TEI (sent in req above) */
939 "6d31", /* mapped seq */
940 "01", /* restart */
941 "00000567", /* TEI U */
942 "00000765", /* TEI C */
943 "0004""c0a82b22", /* GSN addresses */
944 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
945 );
946 const char *gtp_resp_to_sgsn =
947 MSG_PDP_CTX_RSP("004e",
948 "00000321", /* unmapped TEI ("001") */
949 "abcd", /* unmapped seq ("6d31") */
Neels Hofmeyr80ee3912015-11-26 22:19:22 +0100950 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100951 "00000001", /* mapped TEI from GGSN ("567") */
952 "00000001", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +0100953 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
954 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
955 );
Neels Hofmeyre6078bc2015-11-11 00:45:50 +0100956 /* The response should go back to whichever port the request came from
957 * (unmapped by sequence nr) */
Neels Hofmeyr43283a32015-11-10 22:07:04 +0100958 LVL2_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
959 &sgsn_sender,
960 gtp_resp_from_ggsn,
961 gtp_resp_to_sgsn));
962
963 return 1;
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200964}
965
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +0100966#define MSG_DEL_PDP_CTX_REQ(tei, seq) \
967 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \
968 "14" /* type 20: Delete PDP Context Request */ \
969 "0008" /* msg length = 8 + len (2 octets) */ \
970 tei /* TEI Ctrl */ \
971 seq /* Sequence nr (2 octets) */ \
972 "00" /* N-PDU 0 */ \
973 "00" /* No extensions */ \
974 /* IEs */ \
975 "13fe" /* 19: Teardown ind = 0 */ \
976 "1400" /* 20: NSAPI = 0*/ \
977
978#define MSG_DEL_PDP_CTX_RSP(tei, seq) \
979 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr. */ \
980 "15" /* type 21: Delete PDP Context Response */ \
981 "0006" /* msg length = 8 + len (2 octets) */ \
982 tei /* TEI Ctrl */ \
983 seq /* Sequence nr (2 octets) */ \
984 "00" /* N-PDU 0 */ \
985 "00" /* No extensions */ \
986 /* IEs */ \
987 "01" /* 1: Cause */ \
988 "80" /* value = 0b10000000 = response, no rejection. */ \
989
Neels Hofmeyr200aff62015-12-01 01:01:16 +0100990static int delete_pdp_ctx_from_sgsn(void)
Neels Hofmeyr9f796642015-09-24 17:32:30 +0200991{
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +0100992 now += GTPH_EXPIRE_QUICKLY_SECS + 1;
993 gtphub_gc(hub, now);
994
995 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +0100996 "TEI=1:"
997 " 192.168.42.23 (TEI C=321 U=123)"
998 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +0100999 " @21945\n"));
1000
1001 /* TEI Ctrl from above and next sequence after abcd. */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001002 const char *gtp_req_from_sgsn = MSG_DEL_PDP_CTX_REQ("00000001", "abce");
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001003 const char *gtp_req_to_ggsn = MSG_DEL_PDP_CTX_REQ("00000765", "6d32");
1004
1005 LVL2_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1006 &resolved_ggsn_addr,
1007 gtp_req_from_sgsn,
1008 gtp_req_to_ggsn));
1009
1010 /* 21945 + 31 = 21976 */
1011 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001012 "TEI=1:"
1013 " 192.168.42.23 (TEI C=321 U=123)"
1014 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001015 " @21976\n"));
1016
1017 const char *gtp_resp_from_ggsn =
1018 MSG_DEL_PDP_CTX_RSP("00000001", "6d32");
1019 const char *gtp_resp_to_sgsn =
1020 MSG_DEL_PDP_CTX_RSP("00000321", "abce");
1021
1022 /* The response should go back to whichever port the request came from
1023 * (unmapped by sequence nr) */
1024 LVL2_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1025 &sgsn_sender,
1026 gtp_resp_from_ggsn,
1027 gtp_resp_to_sgsn));
1028
1029 LVL2_ASSERT(tunnels_are(""));
1030
1031 return 1;
1032}
1033
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001034static int delete_pdp_ctx_from_ggsn(void)
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001035{
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001036 now += GTPH_EXPIRE_QUICKLY_SECS + 1;
1037 gtphub_gc(hub, now);
1038
1039 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001040 "TEI=1:"
1041 " 192.168.42.23 (TEI C=321 U=123)"
1042 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001043 " @21945\n"));
1044
1045 /* TEI Ctrl from above and next sequence after abcd. */
1046 const char *gtp_req_from_ggsn = MSG_DEL_PDP_CTX_REQ("00000001", "5432");
1047 const char *gtp_req_to_sgsn = MSG_DEL_PDP_CTX_REQ("00000321", "6d31");
1048
1049 LVL2_ASSERT(msg_from_ggsn_c(&ggsn_sender,
1050 &resolved_sgsn_addr,
1051 gtp_req_from_ggsn,
1052 gtp_req_to_sgsn));
1053
1054 /* 21945 + 31 = 21976 */
1055 LVL2_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001056 "TEI=1:"
1057 " 192.168.42.23 (TEI C=321 U=123)"
1058 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001059 " @21976\n"));
1060
1061 const char *gtp_resp_from_sgsn =
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001062 MSG_DEL_PDP_CTX_RSP("00000001", "6d31");
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001063 const char *gtp_resp_to_ggsn =
1064 MSG_DEL_PDP_CTX_RSP("00000765", "5432");
1065
1066 /* The response should go back to whichever port the request came from
1067 * (unmapped by sequence nr) */
1068 LVL2_ASSERT(msg_from_sgsn_c(&resolved_sgsn_addr,
1069 &ggsn_sender,
1070 gtp_resp_from_sgsn,
1071 gtp_resp_to_ggsn));
1072
1073 LVL2_ASSERT(tunnels_are(""));
1074
1075 return 1;
1076}
1077
1078static void test_one_pdp_ctx(int del_from_side)
1079{
1080 if (del_from_side == GTPH_SIDE_SGSN)
1081 LOG("test_one_pdp_ctx (del from SGSN)")
1082 else LOG("test_one_pdp_ctx (del from GGSN)");
Neels Hofmeyr43283a32015-11-10 22:07:04 +01001083 OSMO_ASSERT(setup_test_hub());
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001084
Neels Hofmeyr43283a32015-11-10 22:07:04 +01001085 OSMO_ASSERT(create_pdp_ctx());
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001086
1087 struct gtphub_peer_port *ggsn_port =
Neels Hofmeyr5da32ac2015-11-29 23:49:48 +01001088 gtphub_port_find_sa(&hub->to_gsns[GTPH_SIDE_GGSN][GTPH_PLANE_CTRL],
Neels Hofmeyr43283a32015-11-10 22:07:04 +01001089 &resolved_ggsn_addr);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001090 OSMO_ASSERT(ggsn_port);
1091 struct gtphub_peer *ggsn = ggsn_port->peer_addr->peer;
1092 /* now == 345; now + 30 == 375.
1093 * seq mapping from above:
1094 * 0xabcd == 43981 (sent in the packet)
1095 * 0x6d31 == 27953 (harcoded seq mapping start val) */
1096 OSMO_ASSERT(nr_map_is(&ggsn->seq_map, "(43981->27953@375), "));
1097
1098 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945.
1099 * 0x00000321 == 801 (TEI from SGSN Ctrl)
1100 * 0x00000123 == 291 (TEI from SGSN User)
1101 * 0x00000765 == 1893 (TEI from GGSN Ctrl)
1102 * 0x00000567 == 1383 (TEI from GGSN User)
1103 * Mapped TEIs should be 1 and 2. */
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001104 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001105 "TEI=1:"
1106 " 192.168.42.23 (TEI C=321 U=123)"
1107 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001108 " @21945\n"));
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001109
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001110 if (del_from_side == GTPH_SIDE_SGSN) {
1111 OSMO_ASSERT(delete_pdp_ctx_from_sgsn());
1112 } else {
1113 OSMO_ASSERT(delete_pdp_ctx_from_ggsn());
1114 }
Neels Hofmeyr3c6e0532015-12-01 00:23:45 +01001115 OSMO_ASSERT(tunnels_are(""));
1116
Neels Hofmeyr21d08732015-11-20 00:08:28 +01001117 OSMO_ASSERT(clear_test_hub());
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001118}
1119
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001120static void test_user_data(void)
1121{
1122 LOG("test_user_data");
1123
1124 OSMO_ASSERT(setup_test_hub());
1125
1126 OSMO_ASSERT(create_pdp_ctx());
1127
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001128 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945. */
1129 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001130 "TEI=1:"
1131 " 192.168.42.23 (TEI C=321 U=123)"
1132 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001133 " @21945\n"));
1134
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001135 LOG("- user data starts");
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001136 /* Now expect default port numbers for User plane. */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001137 resolve_to_ggsn("192.168.43.34", 2152);
1138 resolve_to_sgsn("192.168.42.23", 2152);
1139
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001140 /* 10 minutes later */
1141 now += 600;
1142
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001143 const char *u_from_ggsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001144 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001145 "ff" /* type 255: G-PDU */
1146 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001147 "00000001" /* mapped TEI for SGSN from create_pdp_ctx() */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001148 "0070" /* seq */
1149 "0000" /* No extensions */
1150 /* User data (ICMP packet), 96 - 12 = 84 octets */
1151 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1152 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1153 "202122232425262728292a2b2c2d2e2f3031323334353637"
1154 ;
1155 const char *u_to_sgsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001156 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001157 "ff" /* type 255: G-PDU */
1158 "0058" /* length: 88 + 8 octets == 96 */
1159 "00000123" /* unmapped User TEI */
1160 "6d31" /* new mapped seq */
1161 "0000"
1162 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1163 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1164 "202122232425262728292a2b2c2d2e2f3031323334353637"
1165 ;
1166
1167 /* This depends on create_pdp_ctx() sending resolved_sgsn_addr as GSN
1168 * Address IEs in the GGSN's Create PDP Ctx Response. */
1169 OSMO_ASSERT(msg_from_ggsn_u(&ggsn_sender,
1170 &resolved_sgsn_addr,
1171 u_from_ggsn,
1172 u_to_sgsn));
1173
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001174 /* Make sure the user plane messages have refreshed the TEI mapping
1175 * timeouts: 21945 + 600 == 22545. */
1176 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001177 "TEI=1:"
1178 " 192.168.42.23 (TEI C=321 U=123)"
1179 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001180 " @22545\n"));
1181
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001182 const char *u_from_sgsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001183 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001184 "ff" /* type 255: G-PDU */
1185 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001186 "00000001" /* mapped User TEI for GGSN from create_pdp_ctx() */
Neels Hofmeyrd4abc822015-12-03 14:19:08 +01001187 "1234" /* unknown seq */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001188 "0000" /* No extensions */
1189 /* User data (ICMP packet), 96 - 12 = 84 octets */
1190 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1191 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1192 "202122232425262728292a2b2c2d2e2f3031323334353637"
1193 ;
1194 const char *u_to_ggsn =
Neels Hofmeyr2a1d61f2015-11-16 14:52:05 +01001195 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001196 "ff" /* type 255: G-PDU */
1197 "0058" /* length: 88 + 8 octets == 96 */
1198 "00000567" /* unmapped User TEI */
Neels Hofmeyrd4abc822015-12-03 14:19:08 +01001199 "6d31" /* unmapped seq */
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001200 "0000"
1201 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1202 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1203 "202122232425262728292a2b2c2d2e2f3031323334353637"
1204 ;
1205
Neels Hofmeyrd4abc822015-12-03 14:19:08 +01001206 OSMO_ASSERT(msg_from_sgsn_u(&sgsn_sender,
1207 &resolved_ggsn_addr,
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001208 u_from_sgsn,
1209 u_to_ggsn));
1210
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001211 /* Make sure the user plane messages have refreshed the TEI mapping
1212 * timeouts: 21945 + 600 == 22545. Both timeouts refreshed: */
1213 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001214 "TEI=1:"
1215 " 192.168.42.23 (TEI C=321 U=123)"
1216 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001217 " @22545\n"));
1218
Neels Hofmeyr21d08732015-11-20 00:08:28 +01001219 OSMO_ASSERT(clear_test_hub());
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001220}
1221
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001222static void test_reused_tei(void)
1223{
1224 LOG("test_reused_tei");
1225
1226 OSMO_ASSERT(setup_test_hub());
1227
1228 OSMO_ASSERT(create_pdp_ctx());
1229
1230 const char *gtp_req_from_sgsn =
1231 MSG_PDP_CTX_REQ("0068",
1232 "abce", /* Next seq */
1233 "60",
1234 "42000121436587f9",
1235 "00000123", /* Same TEIs as before */
1236 "00000321",
1237 "0009""08696e7465726e6574", /* "(8)internet" */
1238 "0004""c0a82a17", /* same as default sgsn_sender */
1239 "0004""c0a82a17"
1240 );
1241 const char *gtp_req_to_ggsn =
1242 MSG_PDP_CTX_REQ("0068",
1243 "6d32", /* mapped seq ("abce") */
1244 "23",
1245 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001246 "00000002", /* mapped TEI Data I ("123") */
1247 "00000002", /* mapped TEI Control ("321") */
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001248 "0009""08696e7465726e6574",
1249 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1250 "0004""7f000202" /* replaced with gtphub's ggsn user */
1251 );
1252
1253 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1254 &resolved_ggsn_addr,
1255 gtp_req_from_sgsn,
1256 gtp_req_to_ggsn));
1257 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
1258
1259 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001260 "TEI=2:"
1261 " 192.168.42.23 (TEI C=321 U=123)"
1262 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001263 " @21945\n"));
1264
1265 const char *gtp_resp_from_ggsn =
1266 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001267 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001268 "6d32", /* mapped seq */
1269 "01", /* restart */
1270 "00000567", /* TEI U */
1271 "00000765", /* TEI C */
1272 "0004""c0a82b22", /* GSN addresses */
1273 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1274 );
1275 const char *gtp_resp_to_sgsn =
1276 MSG_PDP_CTX_RSP("004e",
1277 "00000321", /* unmapped TEI ("001") */
1278 "abce", /* unmapped seq ("6d32") */
1279 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001280 "00000002", /* mapped TEI from GGSN ("567") */
1281 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001282 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1283 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1284 );
1285 /* The response should go back to whichever port the request came from
1286 * (unmapped by sequence nr) */
1287 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1288 &sgsn_sender,
1289 gtp_resp_from_ggsn,
1290 gtp_resp_to_sgsn));
1291
1292 OSMO_ASSERT(clear_test_hub());
1293}
1294
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001295static void test_peer_restarted(void)
1296{
1297 LOG("test_peer_restarted");
1298
1299 OSMO_ASSERT(setup_test_hub());
1300
1301 OSMO_ASSERT(create_pdp_ctx());
1302
1303 now += 10;
1304
1305 const char *gtp_req_from_sgsn =
1306 MSG_PDP_CTX_REQ("0068",
1307 "1234", /* brand new seq */
1308 "61", /* DIFFERING restart counter */
1309 "42000121436587f9",
1310 "00000abc",
1311 "00000cba",
1312 "0009""08696e7465726e6574", /* "(8)internet" */
1313 "0004""c0a82a17", /* same as default sgsn_sender */
1314 "0004""c0a82a17"
1315 );
1316 const char *gtp_req_to_ggsn =
1317 MSG_PDP_CTX_REQ("0068",
Neels Hofmeyrfe436262015-12-02 15:44:34 +01001318 "6d33", /* mapped seq ("1234") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001319 "23",
1320 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001321 "00000002", /* mapped TEI Data I ("123") */
1322 "00000002", /* mapped TEI Control ("321") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001323 "0009""08696e7465726e6574",
1324 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1325 "0004""7f000202" /* replaced with gtphub's ggsn user */
1326 );
1327
1328 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1329 &resolved_ggsn_addr,
1330 gtp_req_from_sgsn,
1331 gtp_req_to_ggsn));
1332 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
1333
1334 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001335 "TEI=2:"
1336 " 192.168.42.23 (TEI C=cba U=abc)"
1337 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001338 " @21955\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001339 "TEI=1:"
1340 " (uninitialized) (TEI C=321 U=123)"
1341 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001342 " @21945\n"
1343 ));
1344
1345 const char *gtp_resp_from_ggsn =
1346 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001347 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyrfe436262015-12-02 15:44:34 +01001348 "6d33", /* mapped seq */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001349 "01", /* restart */
1350 "00000def", /* TEI U */
1351 "00000fde", /* TEI C */
1352 "0004""c0a82b22", /* GSN addresses */
1353 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1354 );
1355 const char *gtp_resp_to_sgsn =
1356 MSG_PDP_CTX_RSP("004e",
1357 "00000cba", /* unmapped TEI ("005") */
1358 "1234", /* unmapped seq ("6d32") */
1359 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001360 "00000002", /* mapped TEI from GGSN ("567") */
1361 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001362 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1363 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1364 );
1365 /* The response should go back to whichever port the request came from
1366 * (unmapped by sequence nr) */
1367 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1368 &sgsn_sender,
1369 gtp_resp_from_ggsn,
1370 gtp_resp_to_sgsn));
1371
1372 OSMO_ASSERT(clear_test_hub());
1373}
1374
1375static void test_peer_restarted_reusing_tei(void)
1376{
1377 LOG("test_peer_restarted_reusing_tei");
1378
1379 OSMO_ASSERT(setup_test_hub());
1380
1381 OSMO_ASSERT(create_pdp_ctx());
1382
1383 now += 10;
1384
1385 const char *gtp_req_from_sgsn =
1386 MSG_PDP_CTX_REQ("0068",
1387 "1234", /* brand new seq */
1388 "61", /* DIFFERING restart counter */
1389 "42000121436587f9",
1390 "00000123", /* SAME TEI */
1391 "00000321",
1392 "0009""08696e7465726e6574", /* "(8)internet" */
1393 "0004""c0a82a17", /* same as default sgsn_sender */
1394 "0004""c0a82a17"
1395 );
1396 const char *gtp_req_to_ggsn =
1397 MSG_PDP_CTX_REQ("0068",
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001398 "6d33", /* seq 6d31 + 2, after "out-of-band" Delete PDP Ctx
1399 due to differing restart counter. */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001400 "23",
1401 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001402 "00000002", /* mapped TEI Data I ("123") */
1403 "00000002", /* mapped TEI Control ("321") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001404 "0009""08696e7465726e6574",
1405 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1406 "0004""7f000202" /* replaced with gtphub's ggsn user */
1407 );
1408
1409 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1410 &resolved_ggsn_addr,
1411 gtp_req_from_sgsn,
1412 gtp_req_to_ggsn));
1413 OSMO_ASSERT(was_resolved_for("240010123456789", "internet"));
1414
1415 OSMO_ASSERT(tunnels_are(
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001416 "TEI=2:" /* being established after restart */
1417 " 192.168.42.23 (TEI C=321 U=123)"
1418 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
1419 " @21955\n"
1420 "TEI=1:" /* invalidated due to restart */
1421 " (uninitialized) (TEI C=321 U=123)"
1422 " <-> 192.168.43.34 (TEI C=765 U=567)"
1423 " @21945\n"
1424 ));
1425
1426 /* An "out-of-band" delete request should have been sent to the GGSN
1427 * (checked by expected log output in gtphub_test.ok), and the GGSN
1428 * will (usually) send a Delete Response like this: */
1429 const char *gtp_del_resp_from_ggsn =
1430 MSG_DEL_PDP_CTX_RSP("00000001", "6d32");
1431
1432 /* For this response (due to peer restart) we expect no forwarded
1433 * message. */
1434 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1435 &sgsn_sender,
1436 gtp_del_resp_from_ggsn,
1437 ""));
1438
1439 OSMO_ASSERT(tunnels_are(
1440 "TEI=2:" /* still being established after restart */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001441 " 192.168.42.23 (TEI C=321 U=123)"
1442 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001443 " @21955\n"
1444 ));
1445
1446 const char *gtp_resp_from_ggsn =
1447 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001448 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001449 "6d33", /* mapped seq */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001450 "01", /* restart */
1451 "00000def", /* TEI U */
1452 "00000fde", /* TEI C */
1453 "0004""c0a82b22", /* GSN addresses */
1454 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1455 );
1456 const char *gtp_resp_to_sgsn =
1457 MSG_PDP_CTX_RSP("004e",
1458 "00000321", /* unmapped TEI ("005") */
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001459 "1234", /* unmapped seq ("6d33") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001460 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001461 "00000002", /* mapped TEI from GGSN ("567") */
1462 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001463 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1464 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1465 );
1466 /* The response should go back to whichever port the request came from
1467 * (unmapped by sequence nr) */
1468 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1469 &sgsn_sender,
1470 gtp_resp_from_ggsn,
1471 gtp_resp_to_sgsn));
1472
Neels Hofmeyrf4906e52015-12-06 23:12:02 +01001473 OSMO_ASSERT(tunnels_are(
1474 "TEI=2:" /* still being established after restart */
1475 " 192.168.42.23 (TEI C=321 U=123)"
1476 " <-> 192.168.43.34 (TEI C=fde U=def)"
1477 " @21955\n"
1478 ));
1479
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001480 OSMO_ASSERT(clear_test_hub());
1481}
1482
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001483static void test_sgsn_behind_nat(void)
1484{
1485 LOG("test_user_data");
1486
1487 OSMO_ASSERT(setup_test_hub());
1488 hub->sgsn_use_sender = 1; /* <-- Main difference to test_user_data() */
1489 resolve_to_sgsn("192.168.42.23", 423); /* Same as sender */
1490
1491 OSMO_ASSERT(create_pdp_ctx());
1492
1493 /* now == 345; now + (6 * 60 * 60) == 21600 + 345 == 21945. */
1494 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001495 "TEI=1:"
1496 " 192.168.42.23 (TEI C=321 U=123)"
1497 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001498 " @21945\n"));
1499
1500 LOG("- user data starts");
1501 /* Now expect default port numbers for User plane -- except SGSN. */
1502 resolve_to_ggsn("192.168.43.34", 2152);
1503
1504 /* 10 minutes later */
1505 now += 600;
1506
1507 const char *u_from_ggsn =
1508 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1509 "ff" /* type 255: G-PDU */
1510 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001511 "00000001" /* mapped User TEI for SGSN from create_pdp_ctx() */
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001512 "0070" /* seq */
1513 "0000" /* No extensions */
1514 /* User data (ICMP packet), 96 - 12 = 84 octets */
1515 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1516 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1517 "202122232425262728292a2b2c2d2e2f3031323334353637"
1518 ;
1519 const char *u_to_sgsn =
1520 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1521 "ff" /* type 255: G-PDU */
1522 "0058" /* length: 88 + 8 octets == 96 */
1523 "00000123" /* unmapped User TEI */
1524 "6d31" /* new mapped seq */
1525 "0000"
1526 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1527 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1528 "202122232425262728292a2b2c2d2e2f3031323334353637"
1529 ;
1530
1531 /* This depends on create_pdp_ctx() sending resolved_sgsn_addr as GSN
1532 * Address IEs in the GGSN's Create PDP Ctx Response. */
1533 OSMO_ASSERT(msg_from_ggsn_u(&ggsn_sender,
1534 &resolved_sgsn_addr,
1535 u_from_ggsn,
1536 u_to_sgsn));
1537
1538 /* Make sure the user plane messages have refreshed the TEI mapping
1539 * timeouts: 21945 + 600 == 22545. */
1540 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001541 "TEI=1:"
1542 " 192.168.42.23 (TEI C=321 U=123)"
1543 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001544 " @22545\n"));
1545
1546 const char *u_from_sgsn =
1547 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1548 "ff" /* type 255: G-PDU */
1549 "0058" /* length: 88 + 8 octets == 96 */
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001550 "00000001" /* mapped User TEI for GGSN from create_pdp_ctx() */
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001551 "1234" /* unknown seq */
1552 "0000" /* No extensions */
1553 /* User data (ICMP packet), 96 - 12 = 84 octets */
1554 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1555 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1556 "202122232425262728292a2b2c2d2e2f3031323334353637"
1557 ;
1558 const char *u_to_ggsn =
1559 "32" /* 0b001'1 0010: version 1, protocol GTP, with seq nr */
1560 "ff" /* type 255: G-PDU */
1561 "0058" /* length: 88 + 8 octets == 96 */
1562 "00000567" /* unmapped User TEI */
1563 "6d31" /* unmapped seq */
1564 "0000"
1565 "45000054daee40004001f7890a172a010a172a02080060d23f590071e3f8"
1566 "4156000000007241010000000000101112131415161718191a1b1c1d1e1f"
1567 "202122232425262728292a2b2c2d2e2f3031323334353637"
1568 ;
1569
1570 OSMO_ASSERT(msg_from_sgsn_u(&sgsn_sender,
1571 &resolved_ggsn_addr,
1572 u_from_sgsn,
1573 u_to_ggsn));
1574
1575 /* Make sure the user plane messages have refreshed the TEI mapping
1576 * timeouts: 21945 + 600 == 22545. Both timeouts refreshed: */
1577 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001578 "TEI=1:"
1579 " 192.168.42.23 (TEI C=321 U=123)"
1580 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001581 " @22545\n"));
1582
1583 OSMO_ASSERT(clear_test_hub());
1584}
1585
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001586void test_parallel_context_creation(void)
1587{
1588 LOG("test_parallel_context_creation");
1589
1590 OSMO_ASSERT(setup_test_hub());
1591
1592 const char *gtp_req_from_sgsn1 =
1593 MSG_PDP_CTX_REQ("0068",
1594 "abcd",
1595 "60",
1596 "42000121436587f9",
1597 "00000123",
1598 "00000321",
1599 "0009""08696e7465726e6574", /* "(8)internet" */
1600 "0004""c0a82a17", /* same as default sgsn_sender */
1601 "0004""c0a82a17"
1602 );
1603 const char *gtp_req_to_ggsn1 =
1604 MSG_PDP_CTX_REQ("0068",
1605 "6d31", /* mapped seq ("abcd") */
1606 "23",
1607 "42000121436587f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001608 "00000001", /* mapped TEI Data I ("123") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001609 "00000001", /* mapped TEI Control ("321") */
1610 "0009""08696e7465726e6574",
1611 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1612 "0004""7f000202" /* replaced with gtphub's ggsn user */
1613 );
1614
1615 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1616 &resolved_ggsn_addr,
1617 gtp_req_from_sgsn1,
1618 gtp_req_to_ggsn1));
1619
1620 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001621 "TEI=1:"
1622 " 192.168.42.23 (TEI C=321 U=123)"
1623 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001624 " @21945\n"));
1625
1626 now ++;
1627
1628 const char *gtp_req_from_sgsn2 =
1629 MSG_PDP_CTX_REQ("0068",
1630 "abce",
1631 "60",
1632 "42000121436588f9",
1633 "00000124",
1634 "00000322",
1635 "0009""08696e7465726e6574", /* "(8)internet" */
1636 "0004""c0a82a17", /* same as default sgsn_sender */
1637 "0004""c0a82a17"
1638 );
1639 const char *gtp_req_to_ggsn2 =
1640 MSG_PDP_CTX_REQ("0068",
1641 "6d32", /* mapped seq ("abce") */
1642 "23",
1643 "42000121436588f9",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001644 "00000002", /* mapped TEI Data I ("124") */
1645 "00000002", /* mapped TEI Control ("322") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001646 "0009""08696e7465726e6574",
1647 "0004""7f000201", /* replaced with gtphub's ggsn ctrl */
1648 "0004""7f000202" /* replaced with gtphub's ggsn user */
1649 );
1650
1651 OSMO_ASSERT(msg_from_sgsn_c(&sgsn_sender,
1652 &resolved_ggsn_addr,
1653 gtp_req_from_sgsn2,
1654 gtp_req_to_ggsn2));
1655
1656 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001657 "TEI=2:"
1658 " 192.168.42.23 (TEI C=322 U=124)"
1659 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001660 " @21946\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001661 "TEI=1:"
1662 " 192.168.42.23 (TEI C=321 U=123)"
1663 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001664 " @21945\n"
1665 ));
1666
1667 now ++;
1668
1669 const char *gtp_resp_from_ggsn1 =
1670 MSG_PDP_CTX_RSP("004e",
1671 "00000001", /* destination TEI (sent in req above) */
1672 "6d31", /* mapped seq */
1673 "01", /* restart */
1674 "00000567", /* TEI U */
1675 "00000765", /* TEI C */
1676 "0004""c0a82b22", /* GSN addresses */
1677 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1678 );
1679 const char *gtp_resp_to_sgsn1 =
1680 MSG_PDP_CTX_RSP("004e",
1681 "00000321", /* unmapped TEI ("001") */
1682 "abcd", /* unmapped seq ("6d31") */
1683 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001684 "00000001", /* mapped TEI from GGSN ("567") */
1685 "00000001", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001686 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1687 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1688 );
1689
1690 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1691 &sgsn_sender,
1692 gtp_resp_from_ggsn1,
1693 gtp_resp_to_sgsn1));
1694
1695 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001696 "TEI=2:"
1697 " 192.168.42.23 (TEI C=322 U=124)"
1698 " <-> 192.168.43.34/(uninitialized) (TEI C=0 U=0)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001699 " @21946\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001700 "TEI=1:"
1701 " 192.168.42.23 (TEI C=321 U=123)"
1702 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001703 " @21947\n"
1704 ));
1705
1706 now ++;
1707
1708 const char *gtp_resp_from_ggsn2 =
1709 MSG_PDP_CTX_RSP("004e",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001710 "00000002", /* destination TEI (sent in req above) */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001711 "6d32", /* mapped seq */
1712 "01", /* restart */
1713 "00000568", /* TEI U */
1714 "00000766", /* TEI C */
1715 "0004""c0a82b22", /* GSN addresses */
1716 "0004""c0a82b22" /* (== resolved_ggsn_addr) */
1717 );
1718 const char *gtp_resp_to_sgsn2 =
1719 MSG_PDP_CTX_RSP("004e",
1720 "00000322", /* unmapped TEI ("001") */
1721 "abce", /* unmapped seq ("6d31") */
1722 "23",
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001723 "00000002", /* mapped TEI from GGSN ("567") */
1724 "00000002", /* mapped TEI from GGSN ("765") */
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001725 "0004""7f000101", /* gtphub's address towards SGSNs (Ctrl) */
1726 "0004""7f000102" /* gtphub's address towards SGSNs (User) */
1727 );
1728
1729 OSMO_ASSERT(msg_from_ggsn_c(&resolved_ggsn_addr,
1730 &sgsn_sender,
1731 gtp_resp_from_ggsn2,
1732 gtp_resp_to_sgsn2));
1733
1734 OSMO_ASSERT(tunnels_are(
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001735 "TEI=2:"
1736 " 192.168.42.23 (TEI C=322 U=124)"
1737 " <-> 192.168.43.34 (TEI C=766 U=568)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001738 " @21948\n"
Neels Hofmeyra2217ec2015-12-06 19:11:45 +01001739 "TEI=1:"
1740 " 192.168.42.23 (TEI C=321 U=123)"
1741 " <-> 192.168.43.34 (TEI C=765 U=567)"
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001742 " @21947\n"
1743 ));
1744
1745 OSMO_ASSERT(clear_test_hub());
1746}
1747
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001748
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001749static struct log_info_cat gtphub_categories[] = {
1750 [DGTPHUB] = {
1751 .name = "DGTPHUB",
1752 .description = "GTP Hub",
1753 .color = "\033[1;33m",
Neels Hofmeyr39da7112015-11-24 13:31:06 +01001754 .enabled = 1, .loglevel = LOGL_DEBUG,
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001755 },
1756};
1757
1758static struct log_info info = {
1759 .cat = gtphub_categories,
1760 .num_cat = ARRAY_SIZE(gtphub_categories),
1761};
1762
1763int main(int argc, char **argv)
1764{
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001765 osmo_gtphub_ctx = talloc_named_const(NULL, 0, "osmo_gtphub");
Neels Hofmeyr8de6b862018-04-16 00:57:10 +02001766 void *log_ctx = talloc_named_const(osmo_gtphub_ctx, 0, "log");
1767 osmo_init_logging2(log_ctx, &info);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001768
1769 test_nr_map_basic();
Neels Hofmeyr6a65a8f2015-11-17 14:30:37 +01001770 test_nr_map_wrap();
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001771 test_expiry();
1772 test_echo();
Neels Hofmeyr200aff62015-12-01 01:01:16 +01001773 test_one_pdp_ctx(GTPH_SIDE_SGSN);
1774 test_one_pdp_ctx(GTPH_SIDE_GGSN);
Neels Hofmeyre6078bc2015-11-11 00:45:50 +01001775 test_user_data();
Neels Hofmeyr3a342cf2015-12-02 14:18:26 +01001776 test_reused_tei();
Neels Hofmeyr6402bae2015-12-02 14:31:45 +01001777 test_peer_restarted();
1778 test_peer_restarted_reusing_tei();
Neels Hofmeyr5aba0ec2015-12-03 14:29:48 +01001779 test_sgsn_behind_nat();
Neels Hofmeyr81d75192015-12-06 19:02:43 +01001780 test_parallel_context_creation();
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001781 printf("Done\n");
1782
1783 talloc_report_full(osmo_gtphub_ctx, stderr);
Neels Hofmeyr8de6b862018-04-16 00:57:10 +02001784 talloc_free(log_ctx);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001785 OSMO_ASSERT(talloc_total_blocks(osmo_gtphub_ctx) == 1);
Neels Hofmeyr8de6b862018-04-16 00:57:10 +02001786 talloc_free(osmo_gtphub_ctx);
Neels Hofmeyr9f796642015-09-24 17:32:30 +02001787 return 0;
1788}
1789