blob: d7b769dfd2926b0143d6d0b2d75929eae71a6499 [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>
11#include <stdint.h>
12#include <string.h>
13#include <getopt.h>
14#include <dlfcn.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17
18#include <osmocom/core/msgb.h>
19#include <osmocom/core/application.h>
20#include <osmocom/core/utils.h>
21#include <osmocom/core/logging.h>
22#include <osmocom/core/talloc.h>
Jacob Erlbeck34fc4702013-10-09 11:27:04 +020023#include <osmocom/core/signal.h>
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +020024#include <osmocom/core/rate_ctr.h>
Jacob Erlbeckdcce1962013-10-08 12:04:43 +020025#include <osmocom/gprs/gprs_msgb.h>
26#include <osmocom/gprs/gprs_ns.h>
27#include <osmocom/gprs/gprs_bssgp.h>
28
Jacob Erlbeckdcce1962013-10-08 12:04:43 +020029/* GPRS Network Service, PDU type: NS_RESET,
30 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x1122
31 */
32static const unsigned char gprs_ns_reset[12] = {
33 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
34 0x04, 0x82, 0x11, 0x22
35};
36
37/* GPRS Network Service, PDU type: NS_RESET,
38 * Cause: O&M intervention, NS VCI: 0x3344, NSEI 0x1122
39 */
40static const unsigned char gprs_ns_reset_vci2[12] = {
41 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x33, 0x44,
42 0x04, 0x82, 0x11, 0x22
43};
44
45/* GPRS Network Service, PDU type: NS_RESET,
46 * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x3344
47 */
48static const unsigned char gprs_ns_reset_nsei2[12] = {
49 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22,
50 0x04, 0x82, 0x33, 0x44
51};
52
53/* GPRS Network Service, PDU type: NS_ALIVE */
54static const unsigned char gprs_ns_alive[1] = {
55 0x0a
56};
57
58/* GPRS Network Service, PDU type: NS_STATUS,
59 * Cause: PDU not compatible with the protocol state
60 * PDU: NS_ALIVE
61 */
62static const unsigned char gprs_ns_status_invalid_alive[7] = {
63 0x08, 0x00, 0x81, 0x0a, 0x02, 0x81, 0x0a
64};
65
66/* GPRS Network Service, PDU type: NS_ALIVE_ACK */
67static const unsigned char gprs_ns_alive_ack[1] = {
68 0x0b
69};
70
71/* GPRS Network Service, PDU type: NS_UNBLOCK */
72static const unsigned char gprs_ns_unblock[1] = {
73 0x06
74};
75
76
77/* GPRS Network Service, PDU type: NS_STATUS,
78 * Cause: PDU not compatible with the protocol state
79 * PDU: NS_RESET_ACK, NS VCI: 0x1122, NSEI 0x1122
80 */
81static const unsigned char gprs_ns_status_invalid_reset_ack[15] = {
82 0x08, 0x00, 0x81, 0x0a, 0x02, 0x89, 0x03, 0x01,
83 0x82, 0x11, 0x22, 0x04, 0x82, 0x11, 0x22
84};
85
86/* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0 */
87static const unsigned char gprs_bssgp_reset[22] = {
88 0x00, 0x00, 0x00, 0x00, 0x22, 0x04, 0x82, 0x4a,
89 0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20,
90 0x30, 0x40, 0x50, 0x60, 0x10, 0x00
91};
92
93int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
94 struct sockaddr_in *saddr, enum gprs_ns_ll ll);
95
96/* override */
97int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
98 struct msgb *msg, uint16_t bvci)
99{
100 printf("CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n",
101 event, msgb_bssgp_len(msg), bvci,
102 osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg)));
103 return 0;
104}
105
106/* override */
107ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
108 const struct sockaddr *dest_addr, socklen_t addrlen)
109{
110 typedef ssize_t (*sendto_t)(int, const void *, size_t, int,
111 const struct sockaddr *, socklen_t);
112 static sendto_t real_sendto = NULL;
113
114 if (!real_sendto)
115 real_sendto = dlsym(RTLD_NEXT, "sendto");
116
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200117 if (((struct sockaddr_in *)dest_addr)->sin_addr.s_addr != htonl(0x01020304))
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200118 return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen);
119
120 printf("RESPONSE, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len));
121
122 return len;
123}
124
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200125static void dump_rate_ctr_group(FILE *stream, const char *prefix,
126 struct rate_ctr_group *ctrg)
127{
128 unsigned int i;
129
130 for (i = 0; i < ctrg->desc->num_ctr; i++) {
131 struct rate_ctr *ctr = &ctrg->ctr[i];
132 if (ctr->current && !strchr(ctrg->desc->ctr_desc[i].name, '.'))
133 fprintf(stream, " %s%s: %llu%s",
134 prefix, ctrg->desc->ctr_desc[i].description,
135 (long long)ctr->current,
136 "\n");
137 };
138}
139
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200140/* Signal handler for signals from NS layer */
141static int test_signal(unsigned int subsys, unsigned int signal,
142 void *handler_data, void *signal_data)
143{
144 struct ns_signal_data *nssd = signal_data;
145
146 if (subsys != SS_L_NS)
147 return 0;
148
149 switch (signal) {
150 case S_NS_RESET:
151 printf("==> got signal NS_RESET, NS-VC 0x%04x/%s\n",
152 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200153 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200154 break;
155
156 case S_NS_ALIVE_EXP:
157 printf("==> got signal NS_ALIVE_EXP, NS-VC 0x%04x/%s\n",
158 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200159 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200160 break;
161
162 case S_NS_BLOCK:
163 printf("==> got signal NS_BLOCK, NS-VC 0x%04x/%s\n",
164 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200165 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200166 break;
167
168 case S_NS_UNBLOCK:
169 printf("==> got signal NS_UNBLOCK, NS-VC 0x%04x/%s\n",
170 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200171 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200172 break;
173
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200174 case S_NS_REPLACED:
175 printf("==> got signal NS_REPLACED: 0x%04x/%s",
176 nssd->nsvc->nsvci,
177 gprs_ns_ll_str(nssd->nsvc));
178 printf(" -> 0x%04x/%s\n",
179 nssd->old_nsvc->nsvci,
180 gprs_ns_ll_str(nssd->old_nsvc));
181 break;
182
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200183 default:
184 printf("==> got signal %d, NS-VC 0x%04x/%s\n", signal,
185 nssd->nsvc->nsvci,
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200186 gprs_ns_ll_str(nssd->nsvc));
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200187 break;
188 }
189
190 return 0;
191}
192
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200193static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text, struct sockaddr_in *peer, const unsigned char* data, size_t data_len)
194{
195 struct msgb *msg;
196 int ret;
197 if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) {
198 fprintf(stderr, "message too long: %d\n", data_len);
199 return -1;
200 }
201
202 msg = gprs_ns_msgb_alloc();
203 memmove(msg->data, data, data_len);
204 msg->l2h = msg->data;
205 msgb_put(msg, data_len);
206
207 printf("PROCESSING %s from 0x%08x:%d\n%s\n\n",
208 text, ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port),
209 osmo_hexdump(data, data_len));
210
211 ret = gprs_ns_rcvmsg(nsi, msg, peer, GPRS_NS_LL_UDP);
212
213 printf("result (%s) = %d\n\n", text, ret);
214
215 msgb_free(msg);
216
217 return ret;
218}
219
220static void gprs_dump_nsi(struct gprs_ns_inst *nsi)
221{
222 struct gprs_nsvc *nsvc;
223
224 printf("Current NS-VCIs:\n");
225 llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) {
226 struct sockaddr_in *peer = &(nsvc->ip.bts_addr);
227 printf(" VCI 0x%04x, NSEI 0x%04x, peer 0x%08x:%d\n",
228 nsvc->nsvci, nsvc->nsei,
229 ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port)
230 );
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200231 dump_rate_ctr_group(stdout, " ", nsvc->ctrg);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200232 }
233 printf("\n");
234}
235
236static void test_ns()
237{
238 struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL);
239 struct sockaddr_in peer[4] = {{0},};
240
241 peer[0].sin_family = AF_INET;
242 peer[0].sin_port = htons(1111);
243 peer[0].sin_addr.s_addr = htonl(0x01020304);
244 peer[1].sin_family = AF_INET;
245 peer[1].sin_port = htons(2222);
246 peer[1].sin_addr.s_addr = htonl(0x01020304);
247 peer[2].sin_family = AF_INET;
248 peer[2].sin_port = htons(3333);
249 peer[2].sin_addr.s_addr = htonl(0x01020304);
250 peer[3].sin_family = AF_INET;
251 peer[3].sin_port = htons(4444);
252 peer[3].sin_addr.s_addr = htonl(0x01020304);
253
254 gprs_process_message(nsi, "RESET", &peer[0],
255 gprs_ns_reset, sizeof(gprs_ns_reset));
256 gprs_dump_nsi(nsi);
257 gprs_process_message(nsi, "ALIVE", &peer[0],
258 gprs_ns_alive, sizeof(gprs_ns_alive));
259 gprs_process_message(nsi, "UNBLOCK", &peer[0],
260 gprs_ns_unblock, sizeof(gprs_ns_unblock));
261 gprs_process_message(nsi, "BSSGP RESET", &peer[0],
262 gprs_bssgp_reset, sizeof(gprs_bssgp_reset));
263
264 printf("--- Peer port changes, RESET, message remains unchanged ---\n\n");
265
266 gprs_process_message(nsi, "RESET", &peer[1],
267 gprs_ns_reset, sizeof(gprs_ns_reset));
268 gprs_dump_nsi(nsi);
269
270 printf("--- Peer port changes, RESET, VCI changes ---\n\n");
271
272 gprs_process_message(nsi, "RESET", &peer[2],
273 gprs_ns_reset_vci2, sizeof(gprs_ns_reset_vci2));
274 gprs_dump_nsi(nsi);
275
276 printf("--- Peer port changes, RESET, NSEI changes ---\n\n");
277
278 gprs_process_message(nsi, "RESET", &peer[3],
279 gprs_ns_reset_nsei2, sizeof(gprs_ns_reset_nsei2));
280 gprs_dump_nsi(nsi);
281
282 printf("--- Peer port 3333, RESET, VCI is changed back ---\n\n");
283
284 gprs_process_message(nsi, "RESET", &peer[2],
285 gprs_ns_reset, sizeof(gprs_ns_reset));
286 gprs_dump_nsi(nsi);
287
288 printf("--- Peer port 4444, RESET, NSEI is changed back ---\n\n");
289
290 gprs_process_message(nsi, "RESET", &peer[3],
291 gprs_ns_reset, sizeof(gprs_ns_reset));
292 gprs_dump_nsi(nsi);
293
294 gprs_ns_destroy(nsi);
295 nsi = NULL;
296}
297
298
299int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
300{
301 return -1;
302}
303
304static struct log_info info = {};
305
306int main(int argc, char **argv)
307{
308 osmo_init_logging(&info);
309 log_set_use_color(osmo_stderr_target, 0);
310 log_set_print_filename(osmo_stderr_target, 0);
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200311 osmo_signal_register_handler(SS_L_NS, &test_signal, NULL);
Jacob Erlbeckdcce1962013-10-08 12:04:43 +0200312
313 printf("===== NS protocol test START\n");
314 test_ns();
315 printf("===== NS protocol test END\n\n");
316
317 exit(EXIT_SUCCESS);
318}