blob: 0b3a76c4698635b5dfabf0372ac9b3f1fe4fa840 [file] [log] [blame]
Jacob Erlbeckdcce1962013-10-08 12:04:43 +02001/* test routines for NS connection handling
2 * (C) 2013 by sysmocom s.f.m.c. GmbH
3 * Author: Jacob Erlbeck <jerlbeck@sysmocom.de>
4 */
5
6#undef _GNU_SOURCE
7#define _GNU_SOURCE
8
9#include <stdio.h>
10#include <stdlib.h>
Jacob Erlbecka52ba012013-10-24 01:33:21 +020011#include <unistd.h>
Jacob Erlbeckdcce1962013-10-08 12:04:43 +020012#include <stdint.h>
13#include <string.h>
14#include <getopt.h>
15#include <dlfcn.h>
16#include <sys/types.h>
17#include <sys/socket.h>
18
19#include <osmocom/core/msgb.h>
20#include <osmocom/core/application.h>
21#include <osmocom/core/utils.h>
22#include <osmocom/core/logging.h>
23#include <osmocom/core/talloc.h>
Jacob Erlbeck34fc4702013-10-09 11:27:04 +020024#include <osmocom/core/signal.h>
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +020025#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckdcce1962013-10-08 12:04:43 +020026#include <osmocom/gprs/gprs_msgb.h>
27#include <osmocom/gprs/gprs_ns.h>
28#include <osmocom/gprs/gprs_bssgp.h>
29
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +020030#define REMOTE_BSS_ADDR 0x01020304
31#define REMOTE_SGSN_ADDR 0x05060708
32
33#define SGSN_NSEI 0x0100
34
Jacob Erlbeck2381fde2014-10-07 13:59:30 +020035static int sent_pdu_type = 0;
36
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +020037static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text,
38 struct sockaddr_in *peer, const unsigned char* data,
39 size_t data_len);
40
41static void send_ns_reset(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
42 enum ns_cause cause, uint16_t nsvci, uint16_t nsei)
43{
44 /* GPRS Network Service, PDU type: NS_RESET,
45 */
46 unsigned char msg[12] = {
47 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
48 0x04, 0x82, 0x11, 0x22
49 };
50
51 msg[3] = cause;
52 msg[6] = nsvci / 256;
53 msg[7] = nsvci % 256;
54 msg[10] = nsei / 256;
55 msg[11] = nsei % 256;
56
57 gprs_process_message(nsi, "RESET", src_addr, msg, sizeof(msg));
58}
59
60static void send_ns_reset_ack(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
61 uint16_t nsvci, uint16_t nsei)
62{
63 /* GPRS Network Service, PDU type: NS_RESET_ACK,
64 */
65 unsigned char msg[9] = {
66 0x03, 0x01, 0x82, 0x11, 0x22,
67 0x04, 0x82, 0x11, 0x22
68 };
69
70 msg[3] = nsvci / 256;
71 msg[4] = nsvci % 256;
72 msg[7] = nsei / 256;
73 msg[8] = nsei % 256;
74
75 gprs_process_message(nsi, "RESET_ACK", src_addr, msg, sizeof(msg));
76}
77
78static void send_ns_alive(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr)
79{
80 /* GPRS Network Service, PDU type: NS_ALIVE */
81 unsigned char msg[1] = {
82 0x0a
83 };
84
85 gprs_process_message(nsi, "ALIVE", src_addr, msg, sizeof(msg));
86}
87
88static void send_ns_alive_ack(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr)
89{
90 /* GPRS Network Service, PDU type: NS_ALIVE_ACK */
91 unsigned char msg[1] = {
92 0x0b
93 };
94
95 gprs_process_message(nsi, "ALIVE_ACK", src_addr, msg, sizeof(msg));
96}
97
98static void send_ns_unblock(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr)
99{
100 /* GPRS Network Service, PDU type: NS_UNBLOCK */
101 unsigned char msg[1] = {
102 0x06
103 };
104
105 gprs_process_message(nsi, "UNBLOCK", src_addr, msg, sizeof(msg));
106}
107
108static void send_ns_unblock_ack(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr)
109{
110 /* GPRS Network Service, PDU type: NS_UNBLOCK_ACK */
111 unsigned char msg[1] = {
112 0x07
113 };
114
115 gprs_process_message(nsi, "UNBLOCK_ACK", src_addr, msg, sizeof(msg));
116}
117
118static void send_ns_unitdata(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
119 uint16_t nsbvci,
120 const unsigned char *bssgp_msg, size_t bssgp_msg_size)
121{
122 /* GPRS Network Service, PDU type: NS_UNITDATA */
123 unsigned char msg[4096] = {
124 0x00, 0x00, 0x00, 0x00
125 };
126
127 OSMO_ASSERT(bssgp_msg_size <= sizeof(msg) - 4);
128
129 msg[2] = nsbvci / 256;
130 msg[3] = nsbvci % 256;
131 memcpy(msg + 4, bssgp_msg, bssgp_msg_size);
132
133 gprs_process_message(nsi, "UNITDATA", src_addr, msg, bssgp_msg_size + 4);
134}
135
136static void send_bssgp_reset(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
137 uint16_t bvci)
138{
139 /* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0
140 * BSSGP RESET */
141 unsigned char msg[22] = {
142 0x22, 0x04, 0x82, 0x4a,
143 0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20,
144 0x30, 0x40, 0x50, 0x60, 0x10, 0x00
145 };
146
147 msg[3] = bvci / 256;
148 msg[4] = bvci % 256;
149
150 send_ns_unitdata(nsi, src_addr, 0, msg, sizeof(msg));
151}
152
153static void setup_ns(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
154 uint16_t nsvci, uint16_t nsei)
155{
156 printf("Setup NS-VC: remote 0x%08x:%d, "
157 "NSVCI 0x%04x(%d), NSEI 0x%04x(%d)\n\n",
158 ntohl(src_addr->sin_addr.s_addr), ntohs(src_addr->sin_port),
159 nsvci, nsvci, nsei, nsei);
160
161 send_ns_reset(nsi, src_addr, NS_CAUSE_OM_INTERVENTION, nsvci, nsei);
162 send_ns_alive(nsi, src_addr);
163 send_ns_unblock(nsi, src_addr);
164 send_ns_alive_ack(nsi, src_addr);
165}
166
167static void setup_bssgp(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
168 uint16_t bvci) __attribute__((__unused__));
169
170static void setup_bssgp(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr,
171 uint16_t bvci)
172{
173 printf("Setup BSSGP: remote 0x%08x:%d, "
174 "BVCI 0x%04x(%d)\n\n",
175 ntohl(src_addr->sin_addr.s_addr), ntohs(src_addr->sin_port),
176 bvci, bvci);
177
178 send_bssgp_reset(nsi, src_addr, bvci);
179}
180
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200181/* GPRS Network Service, PDU type: NS_RESET,
182 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x1122
183 */
184static const unsigned char gprs_ns_reset[12] = {
185 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
186 0x04, 0x82, 0x11, 0x22
187};
188
189/* GPRS Network Service, PDU type: NS_RESET,
190 * Cause: O&M intervention, NS VCI: 0x3344, NSEI 0x1122
191 */
192static const unsigned char gprs_ns_reset_vci2[12] = {
193 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x33, 0x44,
194 0x04, 0x82, 0x11, 0x22
195};
196
197/* GPRS Network Service, PDU type: NS_RESET,
198 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x3344
199 */
200static const unsigned char gprs_ns_reset_nsei2[12] = {
201 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
202 0x04, 0x82, 0x33, 0x44
203};
204
205/* GPRS Network Service, PDU type: NS_ALIVE */
206static const unsigned char gprs_ns_alive[1] = {
207 0x0a
208};
209
210/* GPRS Network Service, PDU type: NS_STATUS,
211 * Cause: PDU not compatible with the protocol state
212 * PDU: NS_ALIVE
213 */
214static const unsigned char gprs_ns_status_invalid_alive[7] = {
215 0x08, 0x00, 0x81, 0x0a, 0x02, 0x81, 0x0a
216};
217
218/* GPRS Network Service, PDU type: NS_ALIVE_ACK */
219static const unsigned char gprs_ns_alive_ack[1] = {
220 0x0b
221};
222
223/* GPRS Network Service, PDU type: NS_UNBLOCK */
224static const unsigned char gprs_ns_unblock[1] = {
225 0x06
226};
227
228
229/* GPRS Network Service, PDU type: NS_STATUS,
230 * Cause: PDU not compatible with the protocol state
231 * PDU: NS_RESET_ACK, NS VCI: 0x1122, NSEI 0x1122
232 */
233static const unsigned char gprs_ns_status_invalid_reset_ack[15] = {
234 0x08, 0x00, 0x81, 0x0a, 0x02, 0x89, 0x03, 0x01,
235 0x82, 0x11, 0x22, 0x04, 0x82, 0x11, 0x22
236};
237
238/* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0 */
239static const unsigned char gprs_bssgp_reset[22] = {
240 0x00, 0x00, 0x00, 0x00, 0x22, 0x04, 0x82, 0x4a,
241 0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20,
242 0x30, 0x40, 0x50, 0x60, 0x10, 0x00
243};
244
245int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
246 struct sockaddr_in *saddr, enum gprs_ns_ll ll);
247
248/* override */
249int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
250 struct msgb *msg, uint16_t bvci)
251{
252 printf("CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n",
253 event, msgb_bssgp_len(msg), bvci,
254 osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg)));
255 return 0;
256}
257
258/* override */
259ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
260 const struct sockaddr *dest_addr, socklen_t addrlen)
261{
262 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
263 const struct sockaddr *, socklen_t);
264 static sendto_t real_sendto = NULL;
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200265 uint32_t dest_host = htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200266
267 if (!real_sendto)
268 real_sendto = dlsym(RTLD_NEXT, "sendto");
269
Jacob Erlbeck2381fde2014-10-07 13:59:30 +0200270 sent_pdu_type = len > 0 ? ((uint8_t *)buf)[0] : -1;
271
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200272 if (dest_host == REMOTE_BSS_ADDR)
273 printf("MESSAGE to BSS, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len));
274 else if (dest_host == REMOTE_SGSN_ADDR)
275 printf("MESSAGE to SGSN, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len));
276 else
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200277 return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
278
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200279 return len;
280}
281
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200282/* override */
283int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg)
284{
285 typedef int (*gprs_ns_sendmsg_t)(struct gprs_ns_inst *nsi, struct msgb *msg);
286 static gprs_ns_sendmsg_t real_gprs_ns_sendmsg = NULL;
287 uint16_t bvci = msgb_bvci(msg);
288 uint16_t nsei = msgb_nsei(msg);
289
290 unsigned char *buf = msg->data;
291 size_t len = msg->len;
292
293 if (!real_gprs_ns_sendmsg)
294 real_gprs_ns_sendmsg = dlsym(RTLD_NEXT, "gprs_ns_sendmsg");
295
296 if (nsei == SGSN_NSEI)
297 printf("NS UNITDATA MESSAGE to SGSN, BVCI 0x%04x, msg length %d\n%s\n\n",
298 bvci, len, osmo_hexdump(buf, len));
299 else
300 printf("NS UNITDATA MESSAGE to BSS, BVCI 0x%04x, msg length %d\n%s\n\n",
301 bvci, len, osmo_hexdump(buf, len));
302
303 return real_gprs_ns_sendmsg(nsi, msg);
304}
305
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200306static void dump_rate_ctr_group(FILE *stream, const char *prefix,
307 struct rate_ctr_group *ctrg)
308{
309 unsigned int i;
310
311 for (i = 0; i < ctrg->desc->num_ctr; i++) {
312 struct rate_ctr *ctr = &ctrg->ctr[i];
313 if (ctr->current && !strchr(ctrg->desc->ctr_desc[i].name, '.'))
314 fprintf(stream, " %s%s: %llu%s",
315 prefix, ctrg->desc->ctr_desc[i].description,
316 (long long)ctr->current,
317 "\n");
318 };
319}
320
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200321/* Signal handler for signals from NS layer */
322static int test_signal(unsigned int subsys, unsigned int signal,
323 void *handler_data, void *signal_data)
324{
325 struct ns_signal_data *nssd = signal_data;
326
327 if (subsys != SS_L_NS)
328 return 0;
329
330 switch (signal) {
331 case S_NS_RESET:
332 printf("==> got signal NS_RESET, NS-VC 0x%04x/%s\n",
333 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200334 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200335 break;
336
337 case S_NS_ALIVE_EXP:
338 printf("==> got signal NS_ALIVE_EXP, NS-VC 0x%04x/%s\n",
339 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200340 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200341 break;
342
343 case S_NS_BLOCK:
344 printf("==> got signal NS_BLOCK, NS-VC 0x%04x/%s\n",
345 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200346 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200347 break;
348
349 case S_NS_UNBLOCK:
350 printf("==> got signal NS_UNBLOCK, NS-VC 0x%04x/%s\n",
351 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200352 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200353 break;
354
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200355 case S_NS_REPLACED:
356 printf("==> got signal NS_REPLACED: 0x%04x/%s",
357 nssd->nsvc->nsvci,
358 gprs_ns_ll_str(nssd->nsvc));
359 printf(" -> 0x%04x/%s\n",
360 nssd->old_nsvc->nsvci,
361 gprs_ns_ll_str(nssd->old_nsvc));
362 break;
363
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200364 case S_NS_MISMATCH:
365 printf("==> got signal NS_MISMATCH: 0x%04x/%s pdu=%d, ie=%d\n",
366 nssd->nsvc->nsvci, gprs_ns_ll_str(nssd->nsvc),
367 nssd->pdu_type, nssd->ie_type);
368 break;
369
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200370 default:
371 printf("==> got signal %d, NS-VC 0x%04x/%s\n", signal,
372 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200373 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200374 break;
375 }
376
377 return 0;
378}
379
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200380static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text, struct sockaddr_in *peer, const unsigned char* data, size_t data_len)
381{
382 struct msgb *msg;
383 int ret;
384 if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) {
385 fprintf(stderr, "message too long: %d\n", data_len);
386 return -1;
387 }
388
389 msg = gprs_ns_msgb_alloc();
390 memmove(msg->data, data, data_len);
391 msg->l2h = msg->data;
392 msgb_put(msg, data_len);
393
394 printf("PROCESSING %s from 0x%08x:%d\n%s\n\n",
395 text, ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port),
396 osmo_hexdump(data, data_len));
397
398 ret = gprs_ns_rcvmsg(nsi, msg, peer, GPRS_NS_LL_UDP);
399
400 printf("result (%s) = %d\n\n", text, ret);
401
402 msgb_free(msg);
403
404 return ret;
405}
406
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200407static int gprs_send_message(struct gprs_ns_inst *nsi, const char *text,
408 uint16_t nsei, uint16_t bvci,
409 const unsigned char* data, size_t data_len)
410{
411 struct msgb *msg;
412 int ret;
413 if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) {
414 fprintf(stderr, "message too long: %d\n", data_len);
415 return -1;
416 }
417
418 msg = gprs_ns_msgb_alloc();
419 memmove(msg->data, data, data_len);
420 msg->l2h = msg->data;
421 msgb_put(msg, data_len);
422
423 msgb_nsei(msg) = nsei;
424 msgb_bvci(msg) = bvci;
425
426 printf("SENDING %s to NSEI 0x%04x, BVCI 0x%04x\n", text, nsei, bvci);
427
428 ret = gprs_ns_sendmsg(nsi, msg);
429
430 printf("result (%s) = %d\n\n", text, ret);
431
432 return ret;
433}
434
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200435static void gprs_dump_nsi(struct gprs_ns_inst *nsi)
436{
437 struct gprs_nsvc *nsvc;
438
439 printf("Current NS-VCIs:\n");
440 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
441 struct sockaddr_in *peer = &(nsvc->ip.bts_addr);
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200442 printf(" VCI 0x%04x, NSEI 0x%04x, peer 0x%08x:%d%s%s%s\n",
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200443 nsvc->nsvci, nsvc->nsei,
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200444 ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port),
445 nsvc->state & NSE_S_BLOCKED ? ", blocked" : "",
Jacob Erlbeck54b8b2d2013-10-24 01:33:25 +0200446 nsvc->state & NSE_S_ALIVE ? "" : ", dead",
447 nsvc->nsvci_is_valid ? "" : ", invalid VCI"
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200448 );
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200449 dump_rate_ctr_group(stdout, " ", nsvc->ctrg);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200450 }
451 printf("\n");
452}
453
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200454static void test_nsvc()
455{
456 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
457 struct sockaddr_in peer[1] = {{0},};
458 struct gprs_nsvc *nsvc;
459 int i;
460
461 peer[0].sin_family = AF_INET;
462 peer[0].sin_port = htons(1111);
463 peer[0].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
464
465 for (i=0; i<4; ++i) {
466 printf("--- Create via RESET (round %d) ---\n\n", i);
467
468 send_ns_reset(nsi, &peer[0], NS_CAUSE_OM_INTERVENTION,
469 0x1001, 0x1000);
470 gprs_dump_nsi(nsi);
471
472 printf("--- Delete nsvc object (round %d)---\n\n", i);
473
474 nsvc = gprs_nsvc_by_nsvci(nsi, 0x1001);
475 OSMO_ASSERT(nsvc != NULL);
476 gprs_nsvc_delete(nsvc);
477
478 gprs_dump_nsi(nsi);
479 }
480
481 gprs_ns_destroy(nsi);
482 nsi = NULL;
483
484 printf("--- Process timers ---\n\n");
485 /* wait for rate_ctr_timer expiry */
486 usleep(1100000);
487 /* ensure termination */
488 alarm(2);
489 osmo_timers_update();
490 alarm(0);
491}
492
Jacob Erlbecke7c42712013-10-28 13:29:10 +0100493static void test_ignored_messages()
494{
495 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
496 struct sockaddr_in peer[1] = {{0},};
497
498 peer[0].sin_family = AF_INET;
499 peer[0].sin_port = htons(1111);
500 peer[0].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
501
502 printf("--- Send unexpected NS STATUS (should not be answered)---\n\n");
503 /* Do not respond, see 3GPP TS 08.16, 7.5.1 */
504 gprs_process_message(nsi, "STATUS", &peer[0],
505 gprs_ns_status_invalid_alive,
506 sizeof(gprs_ns_status_invalid_alive));
507
508 printf("--- Send unexpected NS ALIVE ACK (should not be answered)---\n\n");
509 /* Ignore this, see 3GPP TS 08.16, 7.4.1 */
510 send_ns_alive_ack(nsi, &peer[0]);
511
512 printf("--- Send unexpected NS RESET ACK (should not be answered)---\n\n");
513 /* Ignore this, see 3GPP TS 08.16, 7.3.1 */
514 send_ns_reset_ack(nsi, &peer[0], 0xe001, 0xe000);
515
516 gprs_ns_destroy(nsi);
517 nsi = NULL;
518}
519
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200520static void test_bss_port_changes()
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200521{
522 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
523 struct sockaddr_in peer[4] = {{0},};
524
525 peer[0].sin_family = AF_INET;
526 peer[0].sin_port = htons(1111);
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200527 peer[0].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200528 peer[1].sin_family = AF_INET;
529 peer[1].sin_port = htons(2222);
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200530 peer[1].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200531 peer[2].sin_family = AF_INET;
532 peer[2].sin_port = htons(3333);
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200533 peer[2].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200534 peer[3].sin_family = AF_INET;
535 peer[3].sin_port = htons(4444);
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200536 peer[3].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200537
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200538 printf("--- Setup, send BSSGP RESET ---\n\n");
539
540 setup_ns(nsi, &peer[0], 0x1122, 0x1122);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200541 gprs_dump_nsi(nsi);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200542 gprs_process_message(nsi, "BSSGP RESET", &peer[0],
543 gprs_bssgp_reset, sizeof(gprs_bssgp_reset));
544
545 printf("--- Peer port changes, RESET, message remains unchanged ---\n\n");
546
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200547 send_ns_reset(nsi, &peer[1], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x1122);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200548 gprs_dump_nsi(nsi);
549
550 printf("--- Peer port changes, RESET, VCI changes ---\n\n");
551
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200552 send_ns_reset(nsi, &peer[2], NS_CAUSE_OM_INTERVENTION, 0x3344, 0x1122);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200553 gprs_dump_nsi(nsi);
554
555 printf("--- Peer port changes, RESET, NSEI changes ---\n\n");
556
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200557 send_ns_reset(nsi, &peer[3], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x3344);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200558 gprs_dump_nsi(nsi);
559
560 printf("--- Peer port 3333, RESET, VCI is changed back ---\n\n");
561
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200562 send_ns_reset(nsi, &peer[2], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x1122);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200563 gprs_dump_nsi(nsi);
564
565 printf("--- Peer port 4444, RESET, NSEI is changed back ---\n\n");
566
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200567 send_ns_reset(nsi, &peer[3], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x1122);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200568 gprs_dump_nsi(nsi);
569
570 gprs_ns_destroy(nsi);
571 nsi = NULL;
572}
573
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200574static void test_bss_reset_ack()
575{
576 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
577 struct sockaddr_in peer[4] = {{0},};
578 struct gprs_nsvc *nsvc;
579 struct sockaddr_in *nse[4];
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200580
581 peer[0].sin_family = AF_INET;
582 peer[0].sin_port = htons(1111);
583 peer[0].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
584 peer[1].sin_family = AF_INET;
585 peer[1].sin_port = htons(2222);
586 peer[1].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
587 peer[2].sin_family = AF_INET;
588 peer[2].sin_port = htons(3333);
589 peer[2].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
590 peer[3].sin_family = AF_INET;
591 peer[3].sin_port = htons(4444);
592 peer[3].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR);
593
594 nse[0] = &peer[0];
595 nse[1] = &peer[1];
596
597 printf("--- Setup VC 1 BSS -> SGSN ---\n\n");
598
599 setup_ns(nsi, nse[0], 0x1001, 0x1000);
600 gprs_dump_nsi(nsi);
601
602 printf("--- Setup VC 2 BSS -> SGSN ---\n\n");
603
604 setup_ns(nsi, nse[1], 0x2001, 0x2000);
605 gprs_dump_nsi(nsi);
606
607 printf("--- Setup VC 1 SGSN -> BSS ---\n\n");
608
609 nsvc = gprs_nsvc_by_nsvci(nsi, 0x1001);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200610 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200611 send_ns_reset_ack(nsi, nse[0], 0x1001, 0x1000);
612 gprs_dump_nsi(nsi);
613
614 printf("--- Exchange NSEI 1 + 2 links ---\n\n");
615
616 nse[1] = &peer[0];
617 nse[0] = &peer[1];
618
619 printf("--- Setup VC 2 SGSN -> BSS (hits NSEI 1) ---\n\n");
620
621 nsvc = gprs_nsvc_by_nsvci(nsi, 0x2001);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200622 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200623 send_ns_reset_ack(nsi, nse[0], 0x1001, 0x1000);
624 gprs_dump_nsi(nsi);
625
626 printf("--- Setup VC 2 SGSN -> BSS (hits NSEI 2) ---\n\n");
627
628 nsvc = gprs_nsvc_by_nsvci(nsi, 0x2001);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200629 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200630
631 printf("--- Setup VC 1 SGSN -> BSS (hits NSEI 1) ---\n\n");
632
633 nsvc = gprs_nsvc_by_nsvci(nsi, 0x1001);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200634 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200635 send_ns_reset_ack(nsi, nse[0], 0x1001, 0x1000);
636 gprs_dump_nsi(nsi);
637
638 printf("--- Setup VC 2 BSS -> SGSN ---\n\n");
639
640 setup_ns(nsi, nse[1], 0x2001, 0x2000);
641 gprs_dump_nsi(nsi);
642
643 /* Test 3GPP TS 08.16, 7.3.1, 3rd paragraph. */
644 /* This is not rejected because the NSEI has been
645 * assigned dynamically and not by configuration.
646 * This is not strictly spec conformant. */
647
648 printf("--- RESET with invalid NSEI, BSS -> SGSN ---\n\n");
649
650 send_ns_reset(nsi, nse[0], NS_CAUSE_OM_INTERVENTION,
651 0x1001, 0xf000);
652 gprs_dump_nsi(nsi);
653
654 /* Test 3GPP TS 08.16, 7.3.1, 2nd paragraph. */
655 /* This is not rejected because the NSEI has been
656 * assigned dynamically and not by configuration.
657 * This is not strictly spec conformant. */
658
659 printf("--- RESET with invalid NSVCI, BSS -> SGSN ---\n\n");
660
661 send_ns_reset(nsi, nse[0], NS_CAUSE_OM_INTERVENTION,
662 0xf001, 0x1000);
663 gprs_dump_nsi(nsi);
664
665 printf("--- RESET with old NSEI, NSVCI, BSS -> SGSN ---\n\n");
666
667 send_ns_reset(nsi, nse[0], NS_CAUSE_OM_INTERVENTION,
668 0x1001, 0x1000);
669 gprs_dump_nsi(nsi);
670
671 /* Test 3GPP TS 08.16, 7.3.1, 5th paragraph. */
672
673 printf("--- Unexpected RESET_ACK VC 1, BSS -> SGSN ---\n\n");
674
675 send_ns_reset_ack(nsi, nse[0], 0x1001, 0x1000);
676 gprs_dump_nsi(nsi);
677
678 /* Test 3GPP TS 08.16, 7.3.1, 4th paragraph. */
679
680 printf("--- RESET_ACK with invalid NSEI, BSS -> SGSN ---\n\n");
681
682 nsvc = gprs_nsvc_by_nsvci(nsi, 0x1001);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200683 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200684 send_ns_reset_ack(nsi, nse[0], 0x1001, 0xf000);
685 gprs_dump_nsi(nsi);
686
687 /* Test 3GPP TS 08.16, 7.3.1, 4th paragraph. */
688
689 printf("--- RESET_ACK with invalid NSVCI, BSS -> SGSN ---\n\n");
690
691 nsvc = gprs_nsvc_by_nsvci(nsi, 0x1001);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200692 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200693 send_ns_reset_ack(nsi, nse[0], 0xf001, 0x1000);
694 gprs_dump_nsi(nsi);
695
696 gprs_ns_destroy(nsi);
697 nsi = NULL;
698}
699
700
701static void test_sgsn_reset()
702{
703 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
704 struct sockaddr_in sgsn_peer= {0};
705 struct gprs_nsvc *nsvc;
706
707 sgsn_peer.sin_family = AF_INET;
708 sgsn_peer.sin_port = htons(32000);
709 sgsn_peer.sin_addr.s_addr = htonl(REMOTE_SGSN_ADDR);
710
711 gprs_dump_nsi(nsi);
712
713 printf("--- Setup SGSN connection, BSS -> SGSN ---\n\n");
714
715 gprs_ns_nsip_connect(nsi, &sgsn_peer, SGSN_NSEI, SGSN_NSEI+1);
716 send_ns_reset_ack(nsi, &sgsn_peer, SGSN_NSEI+1, SGSN_NSEI);
717 send_ns_alive_ack(nsi, &sgsn_peer);
718 send_ns_unblock_ack(nsi, &sgsn_peer);
719 gprs_dump_nsi(nsi);
720
721 printf("--- RESET, SGSN -> BSS ---\n\n");
722
723 send_ns_reset(nsi, &sgsn_peer, NS_CAUSE_OM_INTERVENTION,
724 SGSN_NSEI+1, SGSN_NSEI);
725 gprs_dump_nsi(nsi);
726
727 /* Test 3GPP TS 08.16, 7.3.1, 3rd paragraph. */
728
729 printf("--- RESET with invalid NSEI, SGSN -> BSS ---\n\n");
730
731 send_ns_reset(nsi, &sgsn_peer, NS_CAUSE_OM_INTERVENTION,
732 SGSN_NSEI+1, 0xf000);
733 gprs_dump_nsi(nsi);
734
735 /* Test 3GPP TS 08.16, 7.3.1, 2nd paragraph. */
736
737 printf("--- RESET with invalid NSVCI, SGSN -> BSS ---\n\n");
738
739 send_ns_reset(nsi, &sgsn_peer, NS_CAUSE_OM_INTERVENTION,
740 0xf001, SGSN_NSEI);
741 gprs_dump_nsi(nsi);
742
743 printf("--- RESET, SGSN -> BSS ---\n\n");
744
745 send_ns_reset(nsi, &sgsn_peer, NS_CAUSE_OM_INTERVENTION,
746 SGSN_NSEI+1, SGSN_NSEI);
747 gprs_dump_nsi(nsi);
748
749 /* Test 3GPP TS 08.16, 7.3.1, 5th paragraph. */
750
751 printf("--- Unexpected RESET_ACK VC 1, BSS -> SGSN ---\n\n");
752
753 send_ns_reset_ack(nsi, &sgsn_peer, SGSN_NSEI+1, SGSN_NSEI);
754 gprs_dump_nsi(nsi);
755
756 /* Test 3GPP TS 08.16, 7.3.1, 4th paragraph. */
757
758 printf("--- RESET_ACK with invalid NSEI, BSS -> SGSN ---\n\n");
759
760 nsvc = gprs_nsvc_by_nsvci(nsi, SGSN_NSEI+1);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200761 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200762 send_ns_reset_ack(nsi, &sgsn_peer, SGSN_NSEI+1, 0xe000);
763 gprs_dump_nsi(nsi);
764
765 /* Test 3GPP TS 08.16, 7.3.1, 4th paragraph. */
766
767 printf("--- RESET_ACK with invalid NSVCI, BSS -> SGSN ---\n\n");
768
769 nsvc = gprs_nsvc_by_nsvci(nsi, SGSN_NSEI+1);
Jacob Erlbeckf357c872014-10-07 13:52:07 +0200770 gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION);
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200771 send_ns_reset_ack(nsi, &sgsn_peer, 0xe001, SGSN_NSEI);
772 gprs_dump_nsi(nsi);
773
774
775 gprs_ns_destroy(nsi);
776 nsi = NULL;
777}
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200778
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200779static void test_sgsn_output()
780{
781 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
782 struct sockaddr_in sgsn_peer= {0};
783
784 sgsn_peer.sin_family = AF_INET;
785 sgsn_peer.sin_port = htons(32000);
786 sgsn_peer.sin_addr.s_addr = htonl(REMOTE_SGSN_ADDR);
787
788 gprs_dump_nsi(nsi);
789
790 printf("--- Send message to SGSN ---\n\n");
791
792 gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0,
793 gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4);
794
795 printf("--- Setup dead connection to SGSN ---\n\n");
796
797 gprs_ns_nsip_connect(nsi, &sgsn_peer, SGSN_NSEI, SGSN_NSEI+1);
798 gprs_dump_nsi(nsi);
799
800 printf("--- Send message to SGSN ---\n\n");
801
802 gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0,
803 gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4);
804
805 printf("--- Make connection to SGSN alive ---\n\n");
806
807 send_ns_reset_ack(nsi, &sgsn_peer, SGSN_NSEI+1, SGSN_NSEI);
808 send_ns_alive_ack(nsi, &sgsn_peer);
809 gprs_dump_nsi(nsi);
810
811 printf("--- Send message to SGSN ---\n\n");
812
813 gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0,
814 gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4);
815
816 printf("--- Unblock connection to SGSN ---\n\n");
817
818 send_ns_unblock_ack(nsi, &sgsn_peer);
819 send_ns_alive(nsi, &sgsn_peer);
820 gprs_dump_nsi(nsi);
821
822 printf("--- Send message to SGSN ---\n\n");
823
824 gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0,
825 gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4);
826
827 printf("--- Send empty message with BVCI to SGSN ---\n\n");
828
829 gprs_send_message(nsi, "[empty]", SGSN_NSEI, 0x0102,
830 gprs_bssgp_reset, 0);
831
832
833 gprs_ns_destroy(nsi);
834 nsi = NULL;
835}
836
837
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200838int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
839{
840 return -1;
841}
842
843static struct log_info info = {};
844
845int main(int argc, char **argv)
846{
847 osmo_init_logging(&info);
848 log_set_use_color(osmo_stderr_target, 0);
849 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200850 osmo_signal_register_handler(SS_L_NS, &test_signal, NULL);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200851
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200852 log_set_print_filename(osmo_stderr_target, 0);
853 log_set_log_level(osmo_stderr_target, LOGL_INFO);
854
855 setlinebuf(stdout);
856
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200857 printf("===== NS protocol test START\n");
Jacob Erlbecka52ba012013-10-24 01:33:21 +0200858 test_nsvc();
Jacob Erlbecke7c42712013-10-28 13:29:10 +0100859 test_ignored_messages();
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200860 test_bss_port_changes();
Jacob Erlbeck81d7c142013-10-24 01:33:22 +0200861 test_bss_reset_ack();
862 test_sgsn_reset();
Jacob Erlbeck0d4e9492013-10-17 13:58:34 +0200863 test_sgsn_output();
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200864 printf("===== NS protocol test END\n\n");
865
866 exit(EXIT_SUCCESS);
867}