Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 1 | /* 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 Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 23 | #include <osmocom/core/signal.h> |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 24 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 25 | #include <osmocom/gprs/gprs_msgb.h> |
| 26 | #include <osmocom/gprs/gprs_ns.h> |
| 27 | #include <osmocom/gprs/gprs_bssgp.h> |
| 28 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 29 | #define REMOTE_BSS_ADDR 0x01020304 |
| 30 | #define REMOTE_SGSN_ADDR 0x05060708 |
| 31 | |
| 32 | #define SGSN_NSEI 0x0100 |
| 33 | |
| 34 | static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text, |
| 35 | struct sockaddr_in *peer, const unsigned char* data, |
| 36 | size_t data_len); |
| 37 | |
| 38 | static void send_ns_reset(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 39 | enum ns_cause cause, uint16_t nsvci, uint16_t nsei) |
| 40 | { |
| 41 | /* GPRS Network Service, PDU type: NS_RESET, |
| 42 | */ |
| 43 | unsigned char msg[12] = { |
| 44 | 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22, |
| 45 | 0x04, 0x82, 0x11, 0x22 |
| 46 | }; |
| 47 | |
| 48 | msg[3] = cause; |
| 49 | msg[6] = nsvci / 256; |
| 50 | msg[7] = nsvci % 256; |
| 51 | msg[10] = nsei / 256; |
| 52 | msg[11] = nsei % 256; |
| 53 | |
| 54 | gprs_process_message(nsi, "RESET", src_addr, msg, sizeof(msg)); |
| 55 | } |
| 56 | |
| 57 | static void send_ns_reset_ack(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 58 | uint16_t nsvci, uint16_t nsei) |
| 59 | { |
| 60 | /* GPRS Network Service, PDU type: NS_RESET_ACK, |
| 61 | */ |
| 62 | unsigned char msg[9] = { |
| 63 | 0x03, 0x01, 0x82, 0x11, 0x22, |
| 64 | 0x04, 0x82, 0x11, 0x22 |
| 65 | }; |
| 66 | |
| 67 | msg[3] = nsvci / 256; |
| 68 | msg[4] = nsvci % 256; |
| 69 | msg[7] = nsei / 256; |
| 70 | msg[8] = nsei % 256; |
| 71 | |
| 72 | gprs_process_message(nsi, "RESET_ACK", src_addr, msg, sizeof(msg)); |
| 73 | } |
| 74 | |
| 75 | static void send_ns_alive(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr) |
| 76 | { |
| 77 | /* GPRS Network Service, PDU type: NS_ALIVE */ |
| 78 | unsigned char msg[1] = { |
| 79 | 0x0a |
| 80 | }; |
| 81 | |
| 82 | gprs_process_message(nsi, "ALIVE", src_addr, msg, sizeof(msg)); |
| 83 | } |
| 84 | |
| 85 | static void send_ns_alive_ack(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr) |
| 86 | { |
| 87 | /* GPRS Network Service, PDU type: NS_ALIVE_ACK */ |
| 88 | unsigned char msg[1] = { |
| 89 | 0x0b |
| 90 | }; |
| 91 | |
| 92 | gprs_process_message(nsi, "ALIVE_ACK", src_addr, msg, sizeof(msg)); |
| 93 | } |
| 94 | |
| 95 | static void send_ns_unblock(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr) |
| 96 | { |
| 97 | /* GPRS Network Service, PDU type: NS_UNBLOCK */ |
| 98 | unsigned char msg[1] = { |
| 99 | 0x06 |
| 100 | }; |
| 101 | |
| 102 | gprs_process_message(nsi, "UNBLOCK", src_addr, msg, sizeof(msg)); |
| 103 | } |
| 104 | |
| 105 | static void send_ns_unblock_ack(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr) |
| 106 | { |
| 107 | /* GPRS Network Service, PDU type: NS_UNBLOCK_ACK */ |
| 108 | unsigned char msg[1] = { |
| 109 | 0x07 |
| 110 | }; |
| 111 | |
| 112 | gprs_process_message(nsi, "UNBLOCK_ACK", src_addr, msg, sizeof(msg)); |
| 113 | } |
| 114 | |
| 115 | static void send_ns_unitdata(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 116 | uint16_t nsbvci, |
| 117 | const unsigned char *bssgp_msg, size_t bssgp_msg_size) |
| 118 | { |
| 119 | /* GPRS Network Service, PDU type: NS_UNITDATA */ |
| 120 | unsigned char msg[4096] = { |
| 121 | 0x00, 0x00, 0x00, 0x00 |
| 122 | }; |
| 123 | |
| 124 | OSMO_ASSERT(bssgp_msg_size <= sizeof(msg) - 4); |
| 125 | |
| 126 | msg[2] = nsbvci / 256; |
| 127 | msg[3] = nsbvci % 256; |
| 128 | memcpy(msg + 4, bssgp_msg, bssgp_msg_size); |
| 129 | |
| 130 | gprs_process_message(nsi, "UNITDATA", src_addr, msg, bssgp_msg_size + 4); |
| 131 | } |
| 132 | |
| 133 | static void send_bssgp_reset(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 134 | uint16_t bvci) |
| 135 | { |
| 136 | /* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0 |
| 137 | * BSSGP RESET */ |
| 138 | unsigned char msg[22] = { |
| 139 | 0x22, 0x04, 0x82, 0x4a, |
| 140 | 0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20, |
| 141 | 0x30, 0x40, 0x50, 0x60, 0x10, 0x00 |
| 142 | }; |
| 143 | |
| 144 | msg[3] = bvci / 256; |
| 145 | msg[4] = bvci % 256; |
| 146 | |
| 147 | send_ns_unitdata(nsi, src_addr, 0, msg, sizeof(msg)); |
| 148 | } |
| 149 | |
| 150 | static void setup_ns(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 151 | uint16_t nsvci, uint16_t nsei) |
| 152 | { |
| 153 | printf("Setup NS-VC: remote 0x%08x:%d, " |
| 154 | "NSVCI 0x%04x(%d), NSEI 0x%04x(%d)\n\n", |
| 155 | ntohl(src_addr->sin_addr.s_addr), ntohs(src_addr->sin_port), |
| 156 | nsvci, nsvci, nsei, nsei); |
| 157 | |
| 158 | send_ns_reset(nsi, src_addr, NS_CAUSE_OM_INTERVENTION, nsvci, nsei); |
| 159 | send_ns_alive(nsi, src_addr); |
| 160 | send_ns_unblock(nsi, src_addr); |
| 161 | send_ns_alive_ack(nsi, src_addr); |
| 162 | } |
| 163 | |
| 164 | static void setup_bssgp(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 165 | uint16_t bvci) __attribute__((__unused__)); |
| 166 | |
| 167 | static void setup_bssgp(struct gprs_ns_inst *nsi, struct sockaddr_in *src_addr, |
| 168 | uint16_t bvci) |
| 169 | { |
| 170 | printf("Setup BSSGP: remote 0x%08x:%d, " |
| 171 | "BVCI 0x%04x(%d)\n\n", |
| 172 | ntohl(src_addr->sin_addr.s_addr), ntohs(src_addr->sin_port), |
| 173 | bvci, bvci); |
| 174 | |
| 175 | send_bssgp_reset(nsi, src_addr, bvci); |
| 176 | } |
| 177 | |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 178 | /* GPRS Network Service, PDU type: NS_RESET, |
| 179 | * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x1122 |
| 180 | */ |
| 181 | static const unsigned char gprs_ns_reset[12] = { |
| 182 | 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22, |
| 183 | 0x04, 0x82, 0x11, 0x22 |
| 184 | }; |
| 185 | |
| 186 | /* GPRS Network Service, PDU type: NS_RESET, |
| 187 | * Cause: O&M intervention, NS VCI: 0x3344, NSEI 0x1122 |
| 188 | */ |
| 189 | static const unsigned char gprs_ns_reset_vci2[12] = { |
| 190 | 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x33, 0x44, |
| 191 | 0x04, 0x82, 0x11, 0x22 |
| 192 | }; |
| 193 | |
| 194 | /* GPRS Network Service, PDU type: NS_RESET, |
| 195 | * Cause: O&M intervention, NS VCI: 0x1122, NSEI 0x3344 |
| 196 | */ |
| 197 | static const unsigned char gprs_ns_reset_nsei2[12] = { |
| 198 | 0x02, 0x00, 0x81, 0x01, 0x01, 0x82, 0x11, 0x22, |
| 199 | 0x04, 0x82, 0x33, 0x44 |
| 200 | }; |
| 201 | |
| 202 | /* GPRS Network Service, PDU type: NS_ALIVE */ |
| 203 | static const unsigned char gprs_ns_alive[1] = { |
| 204 | 0x0a |
| 205 | }; |
| 206 | |
| 207 | /* GPRS Network Service, PDU type: NS_STATUS, |
| 208 | * Cause: PDU not compatible with the protocol state |
| 209 | * PDU: NS_ALIVE |
| 210 | */ |
| 211 | static const unsigned char gprs_ns_status_invalid_alive[7] = { |
| 212 | 0x08, 0x00, 0x81, 0x0a, 0x02, 0x81, 0x0a |
| 213 | }; |
| 214 | |
| 215 | /* GPRS Network Service, PDU type: NS_ALIVE_ACK */ |
| 216 | static const unsigned char gprs_ns_alive_ack[1] = { |
| 217 | 0x0b |
| 218 | }; |
| 219 | |
| 220 | /* GPRS Network Service, PDU type: NS_UNBLOCK */ |
| 221 | static const unsigned char gprs_ns_unblock[1] = { |
| 222 | 0x06 |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | /* GPRS Network Service, PDU type: NS_STATUS, |
| 227 | * Cause: PDU not compatible with the protocol state |
| 228 | * PDU: NS_RESET_ACK, NS VCI: 0x1122, NSEI 0x1122 |
| 229 | */ |
| 230 | static const unsigned char gprs_ns_status_invalid_reset_ack[15] = { |
| 231 | 0x08, 0x00, 0x81, 0x0a, 0x02, 0x89, 0x03, 0x01, |
| 232 | 0x82, 0x11, 0x22, 0x04, 0x82, 0x11, 0x22 |
| 233 | }; |
| 234 | |
| 235 | /* GPRS Network Service, PDU type: NS_UNITDATA, BVCI 0 */ |
| 236 | static const unsigned char gprs_bssgp_reset[22] = { |
| 237 | 0x00, 0x00, 0x00, 0x00, 0x22, 0x04, 0x82, 0x4a, |
| 238 | 0x2e, 0x07, 0x81, 0x08, 0x08, 0x88, 0x10, 0x20, |
| 239 | 0x30, 0x40, 0x50, 0x60, 0x10, 0x00 |
| 240 | }; |
| 241 | |
| 242 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 243 | struct sockaddr_in *saddr, enum gprs_ns_ll ll); |
| 244 | |
| 245 | /* override */ |
| 246 | int gprs_ns_callback(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
| 247 | struct msgb *msg, uint16_t bvci) |
| 248 | { |
| 249 | printf("CALLBACK, event %d, msg length %d, bvci 0x%04x\n%s\n\n", |
| 250 | event, msgb_bssgp_len(msg), bvci, |
| 251 | osmo_hexdump(msgb_bssgph(msg), msgb_bssgp_len(msg))); |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* override */ |
| 256 | ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, |
| 257 | const struct sockaddr *dest_addr, socklen_t addrlen) |
| 258 | { |
| 259 | typedef ssize_t (*sendto_t)(int, const void *, size_t, int, |
| 260 | const struct sockaddr *, socklen_t); |
| 261 | static sendto_t real_sendto = NULL; |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 262 | uint32_t dest_host = htonl(((struct sockaddr_in *)dest_addr)->sin_addr.s_addr); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 263 | |
| 264 | if (!real_sendto) |
| 265 | real_sendto = dlsym(RTLD_NEXT, "sendto"); |
| 266 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 267 | if (dest_host == REMOTE_BSS_ADDR) |
| 268 | printf("MESSAGE to BSS, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len)); |
| 269 | else if (dest_host == REMOTE_SGSN_ADDR) |
| 270 | printf("MESSAGE to SGSN, msg length %d\n%s\n\n", len, osmo_hexdump(buf, len)); |
| 271 | else |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 272 | return real_sendto(sockfd, buf, len, flags, dest_addr, addrlen); |
| 273 | |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 274 | return len; |
| 275 | } |
| 276 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 277 | /* override */ |
| 278 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
| 279 | { |
| 280 | typedef int (*gprs_ns_sendmsg_t)(struct gprs_ns_inst *nsi, struct msgb *msg); |
| 281 | static gprs_ns_sendmsg_t real_gprs_ns_sendmsg = NULL; |
| 282 | uint16_t bvci = msgb_bvci(msg); |
| 283 | uint16_t nsei = msgb_nsei(msg); |
| 284 | |
| 285 | unsigned char *buf = msg->data; |
| 286 | size_t len = msg->len; |
| 287 | |
| 288 | if (!real_gprs_ns_sendmsg) |
| 289 | real_gprs_ns_sendmsg = dlsym(RTLD_NEXT, "gprs_ns_sendmsg"); |
| 290 | |
| 291 | if (nsei == SGSN_NSEI) |
| 292 | printf("NS UNITDATA MESSAGE to SGSN, BVCI 0x%04x, msg length %d\n%s\n\n", |
| 293 | bvci, len, osmo_hexdump(buf, len)); |
| 294 | else |
| 295 | printf("NS UNITDATA MESSAGE to BSS, BVCI 0x%04x, msg length %d\n%s\n\n", |
| 296 | bvci, len, osmo_hexdump(buf, len)); |
| 297 | |
| 298 | return real_gprs_ns_sendmsg(nsi, msg); |
| 299 | } |
| 300 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 301 | static void dump_rate_ctr_group(FILE *stream, const char *prefix, |
| 302 | struct rate_ctr_group *ctrg) |
| 303 | { |
| 304 | unsigned int i; |
| 305 | |
| 306 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 307 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 308 | if (ctr->current && !strchr(ctrg->desc->ctr_desc[i].name, '.')) |
| 309 | fprintf(stream, " %s%s: %llu%s", |
| 310 | prefix, ctrg->desc->ctr_desc[i].description, |
| 311 | (long long)ctr->current, |
| 312 | "\n"); |
| 313 | }; |
| 314 | } |
| 315 | |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 316 | /* Signal handler for signals from NS layer */ |
| 317 | static int test_signal(unsigned int subsys, unsigned int signal, |
| 318 | void *handler_data, void *signal_data) |
| 319 | { |
| 320 | struct ns_signal_data *nssd = signal_data; |
| 321 | |
| 322 | if (subsys != SS_L_NS) |
| 323 | return 0; |
| 324 | |
| 325 | switch (signal) { |
| 326 | case S_NS_RESET: |
| 327 | printf("==> got signal NS_RESET, NS-VC 0x%04x/%s\n", |
| 328 | nssd->nsvc->nsvci, |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 329 | gprs_ns_ll_str(nssd->nsvc)); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case S_NS_ALIVE_EXP: |
| 333 | printf("==> got signal NS_ALIVE_EXP, NS-VC 0x%04x/%s\n", |
| 334 | nssd->nsvc->nsvci, |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 335 | gprs_ns_ll_str(nssd->nsvc)); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 336 | break; |
| 337 | |
| 338 | case S_NS_BLOCK: |
| 339 | printf("==> got signal NS_BLOCK, NS-VC 0x%04x/%s\n", |
| 340 | nssd->nsvc->nsvci, |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 341 | gprs_ns_ll_str(nssd->nsvc)); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | case S_NS_UNBLOCK: |
| 345 | printf("==> got signal NS_UNBLOCK, NS-VC 0x%04x/%s\n", |
| 346 | nssd->nsvc->nsvci, |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 347 | gprs_ns_ll_str(nssd->nsvc)); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 348 | break; |
| 349 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 350 | case S_NS_REPLACED: |
| 351 | printf("==> got signal NS_REPLACED: 0x%04x/%s", |
| 352 | nssd->nsvc->nsvci, |
| 353 | gprs_ns_ll_str(nssd->nsvc)); |
| 354 | printf(" -> 0x%04x/%s\n", |
| 355 | nssd->old_nsvc->nsvci, |
| 356 | gprs_ns_ll_str(nssd->old_nsvc)); |
| 357 | break; |
| 358 | |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 359 | default: |
| 360 | printf("==> got signal %d, NS-VC 0x%04x/%s\n", signal, |
| 361 | nssd->nsvc->nsvci, |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 362 | gprs_ns_ll_str(nssd->nsvc)); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 363 | break; |
| 364 | } |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 369 | static int gprs_process_message(struct gprs_ns_inst *nsi, const char *text, struct sockaddr_in *peer, const unsigned char* data, size_t data_len) |
| 370 | { |
| 371 | struct msgb *msg; |
| 372 | int ret; |
| 373 | if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) { |
| 374 | fprintf(stderr, "message too long: %d\n", data_len); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | msg = gprs_ns_msgb_alloc(); |
| 379 | memmove(msg->data, data, data_len); |
| 380 | msg->l2h = msg->data; |
| 381 | msgb_put(msg, data_len); |
| 382 | |
| 383 | printf("PROCESSING %s from 0x%08x:%d\n%s\n\n", |
| 384 | text, ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port), |
| 385 | osmo_hexdump(data, data_len)); |
| 386 | |
| 387 | ret = gprs_ns_rcvmsg(nsi, msg, peer, GPRS_NS_LL_UDP); |
| 388 | |
| 389 | printf("result (%s) = %d\n\n", text, ret); |
| 390 | |
| 391 | msgb_free(msg); |
| 392 | |
| 393 | return ret; |
| 394 | } |
| 395 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 396 | static int gprs_send_message(struct gprs_ns_inst *nsi, const char *text, |
| 397 | uint16_t nsei, uint16_t bvci, |
| 398 | const unsigned char* data, size_t data_len) |
| 399 | { |
| 400 | struct msgb *msg; |
| 401 | int ret; |
| 402 | if (data_len > NS_ALLOC_SIZE - NS_ALLOC_HEADROOM) { |
| 403 | fprintf(stderr, "message too long: %d\n", data_len); |
| 404 | return -1; |
| 405 | } |
| 406 | |
| 407 | msg = gprs_ns_msgb_alloc(); |
| 408 | memmove(msg->data, data, data_len); |
| 409 | msg->l2h = msg->data; |
| 410 | msgb_put(msg, data_len); |
| 411 | |
| 412 | msgb_nsei(msg) = nsei; |
| 413 | msgb_bvci(msg) = bvci; |
| 414 | |
| 415 | printf("SENDING %s to NSEI 0x%04x, BVCI 0x%04x\n", text, nsei, bvci); |
| 416 | |
| 417 | ret = gprs_ns_sendmsg(nsi, msg); |
| 418 | |
| 419 | printf("result (%s) = %d\n\n", text, ret); |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 424 | static void gprs_dump_nsi(struct gprs_ns_inst *nsi) |
| 425 | { |
| 426 | struct gprs_nsvc *nsvc; |
| 427 | |
| 428 | printf("Current NS-VCIs:\n"); |
| 429 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 430 | struct sockaddr_in *peer = &(nsvc->ip.bts_addr); |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 431 | printf(" VCI 0x%04x, NSEI 0x%04x, peer 0x%08x:%d%s%s\n", |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 432 | nsvc->nsvci, nsvc->nsei, |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 433 | ntohl(peer->sin_addr.s_addr), ntohs(peer->sin_port), |
| 434 | nsvc->state & NSE_S_BLOCKED ? ", blocked" : "", |
| 435 | nsvc->state & NSE_S_ALIVE ? "" : ", dead" |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 436 | ); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 437 | dump_rate_ctr_group(stdout, " ", nsvc->ctrg); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 438 | } |
| 439 | printf("\n"); |
| 440 | } |
| 441 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 442 | static void test_bss_port_changes() |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 443 | { |
| 444 | struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL); |
| 445 | struct sockaddr_in peer[4] = {{0},}; |
| 446 | |
| 447 | peer[0].sin_family = AF_INET; |
| 448 | peer[0].sin_port = htons(1111); |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 449 | peer[0].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 450 | peer[1].sin_family = AF_INET; |
| 451 | peer[1].sin_port = htons(2222); |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 452 | peer[1].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 453 | peer[2].sin_family = AF_INET; |
| 454 | peer[2].sin_port = htons(3333); |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 455 | peer[2].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 456 | peer[3].sin_family = AF_INET; |
| 457 | peer[3].sin_port = htons(4444); |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 458 | peer[3].sin_addr.s_addr = htonl(REMOTE_BSS_ADDR); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 459 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 460 | printf("--- Setup, send BSSGP RESET ---\n\n"); |
| 461 | |
| 462 | setup_ns(nsi, &peer[0], 0x1122, 0x1122); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 463 | gprs_dump_nsi(nsi); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 464 | gprs_process_message(nsi, "BSSGP RESET", &peer[0], |
| 465 | gprs_bssgp_reset, sizeof(gprs_bssgp_reset)); |
| 466 | |
| 467 | printf("--- Peer port changes, RESET, message remains unchanged ---\n\n"); |
| 468 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 469 | send_ns_reset(nsi, &peer[1], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x1122); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 470 | gprs_dump_nsi(nsi); |
| 471 | |
| 472 | printf("--- Peer port changes, RESET, VCI changes ---\n\n"); |
| 473 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 474 | send_ns_reset(nsi, &peer[2], NS_CAUSE_OM_INTERVENTION, 0x3344, 0x1122); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 475 | gprs_dump_nsi(nsi); |
| 476 | |
| 477 | printf("--- Peer port changes, RESET, NSEI changes ---\n\n"); |
| 478 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 479 | send_ns_reset(nsi, &peer[3], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x3344); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 480 | gprs_dump_nsi(nsi); |
| 481 | |
| 482 | printf("--- Peer port 3333, RESET, VCI is changed back ---\n\n"); |
| 483 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 484 | send_ns_reset(nsi, &peer[2], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x1122); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 485 | gprs_dump_nsi(nsi); |
| 486 | |
| 487 | printf("--- Peer port 4444, RESET, NSEI is changed back ---\n\n"); |
| 488 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 489 | send_ns_reset(nsi, &peer[3], NS_CAUSE_OM_INTERVENTION, 0x1122, 0x1122); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 490 | gprs_dump_nsi(nsi); |
| 491 | |
| 492 | gprs_ns_destroy(nsi); |
| 493 | nsi = NULL; |
| 494 | } |
| 495 | |
| 496 | |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 497 | static void test_sgsn_output() |
| 498 | { |
| 499 | struct gprs_ns_inst *nsi = gprs_ns_instantiate(gprs_ns_callback, NULL); |
| 500 | struct sockaddr_in sgsn_peer= {0}; |
| 501 | |
| 502 | sgsn_peer.sin_family = AF_INET; |
| 503 | sgsn_peer.sin_port = htons(32000); |
| 504 | sgsn_peer.sin_addr.s_addr = htonl(REMOTE_SGSN_ADDR); |
| 505 | |
| 506 | gprs_dump_nsi(nsi); |
| 507 | |
| 508 | printf("--- Send message to SGSN ---\n\n"); |
| 509 | |
| 510 | gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0, |
| 511 | gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4); |
| 512 | |
| 513 | printf("--- Setup dead connection to SGSN ---\n\n"); |
| 514 | |
| 515 | gprs_ns_nsip_connect(nsi, &sgsn_peer, SGSN_NSEI, SGSN_NSEI+1); |
| 516 | gprs_dump_nsi(nsi); |
| 517 | |
| 518 | printf("--- Send message to SGSN ---\n\n"); |
| 519 | |
| 520 | gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0, |
| 521 | gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4); |
| 522 | |
| 523 | printf("--- Make connection to SGSN alive ---\n\n"); |
| 524 | |
| 525 | send_ns_reset_ack(nsi, &sgsn_peer, SGSN_NSEI+1, SGSN_NSEI); |
| 526 | send_ns_alive_ack(nsi, &sgsn_peer); |
| 527 | gprs_dump_nsi(nsi); |
| 528 | |
| 529 | printf("--- Send message to SGSN ---\n\n"); |
| 530 | |
| 531 | gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0, |
| 532 | gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4); |
| 533 | |
| 534 | printf("--- Unblock connection to SGSN ---\n\n"); |
| 535 | |
| 536 | send_ns_unblock_ack(nsi, &sgsn_peer); |
| 537 | send_ns_alive(nsi, &sgsn_peer); |
| 538 | gprs_dump_nsi(nsi); |
| 539 | |
| 540 | printf("--- Send message to SGSN ---\n\n"); |
| 541 | |
| 542 | gprs_send_message(nsi, "BSSGP RESET", SGSN_NSEI, 0, |
| 543 | gprs_bssgp_reset+4, sizeof(gprs_bssgp_reset)-4); |
| 544 | |
| 545 | printf("--- Send empty message with BVCI to SGSN ---\n\n"); |
| 546 | |
| 547 | gprs_send_message(nsi, "[empty]", SGSN_NSEI, 0x0102, |
| 548 | gprs_bssgp_reset, 0); |
| 549 | |
| 550 | |
| 551 | gprs_ns_destroy(nsi); |
| 552 | nsi = NULL; |
| 553 | } |
| 554 | |
| 555 | |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 556 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 557 | { |
| 558 | return -1; |
| 559 | } |
| 560 | |
| 561 | static struct log_info info = {}; |
| 562 | |
| 563 | int main(int argc, char **argv) |
| 564 | { |
| 565 | osmo_init_logging(&info); |
| 566 | log_set_use_color(osmo_stderr_target, 0); |
| 567 | log_set_print_filename(osmo_stderr_target, 0); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 568 | osmo_signal_register_handler(SS_L_NS, &test_signal, NULL); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 569 | |
| 570 | printf("===== NS protocol test START\n"); |
Jacob Erlbeck | 0d4e949 | 2013-10-17 13:58:34 +0200 | [diff] [blame^] | 571 | test_bss_port_changes(); |
| 572 | test_sgsn_output(); |
Jacob Erlbeck | dcce196 | 2013-10-08 12:04:43 +0200 | [diff] [blame] | 573 | printf("===== NS protocol test END\n\n"); |
| 574 | |
| 575 | exit(EXIT_SUCCESS); |
| 576 | } |