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